在图表中切换图表和数据表

时间:2019-02-18 08:51:43

标签: javascript jquery highcharts

我正在研究图表之间的切换,我已经编写了一些代码,但是当我单击特定图表上方的开关时,它不起作用,应该用数据表替换该图表。

JsFiddle链接:jsfiddle.net/GnanaSagar/psnh87ud/61 /

使用js代码的HTML文件:

function switchToDataTable(id) {
  var chart = $('#' + id).Highchart(),
    chartDiv = $(chart.renderTo);
  if (chartDiv.is(":visible")) {
    chartDiv.hide();
    if (!chart.dataTableDiv) {
      chart.update({
        exporting: {
          showTable: true
        }
      });
    } else {
      $(chart.dataTableDiv).show();
    }
  } else {
    chartDiv.show();
    $(chart.dataTableDiv).hide();
  }

}

Highcharts.chart('chart1', {
  chart: {
    type: 'column'
  },
  title: {
    text: ''
  },
  subtitle: {
    text: ''
  },
  xAxis: {
    categories: [
      'Jan',
      'Feb',
      'Mar',
      'Apr',
      'May',
      'Jun',
      'Jul',
      'Aug',
      'Sep',
      'Oct',
      'Nov',
      'Dec'
    ],
    crosshair: true
  },
  yAxis: {
    min: 0,
    title: {
      text: 'Rainfall (mm)'
    }
  },
  tooltip: {
    headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
    pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
      '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
    footerFormat: '</table>',
    shared: true,
    useHTML: true
  },
  plotOptions: {
    column: {
      pointPadding: 0.2,
      borderWidth: 0
    }
  },
  series: [{
    name: 'Tokyo',
    data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]

  }, {
    name: 'New York',
    data: [83.6, 78.8, 98.5, 93.4, 106.0, 84.5, 105.0, 104.3, 91.2, 83.5, 106.6, 92.3]

  }, {
    name: 'London',
    data: [48.9, 38.8, 39.3, 41.4, 47.0, 48.3, 59.0, 59.6, 52.4, 65.2, 59.3, 51.2]

  }, {
    name: 'Berlin',
    data: [42.4, 33.2, 34.5, 39.7, 52.6, 75.5, 57.4, 60.4, 47.6, 39.1, 46.8, 51.1]

  }]
});

Highcharts.chart('chart2', {
  chart: {
    plotBackgroundColor: null,
    plotBorderWidth: null,
    plotShadow: false,
    type: 'pie'
  },
  title: {
    text: 'Browser market shares in January, 2018'
  },
  tooltip: {
    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
  },
  plotOptions: {
    pie: {
      allowPointSelect: true,
      cursor: 'pointer',
      dataLabels: {
        enabled: false
      },
      showInLegend: true
    }
  },
  series: [{
    name: 'Brands',
    colorByPoint: true,
    legendType: 'point',
    data: [{
      name: 'Chrome',
      y: 61.41,
      sliced: true,
      selected: true
    }, {
      name: 'Internet Explorer',
      y: 11.84
    }, {
      name: 'Firefox',
      y: 10.85
    }, {
      name: 'Edge',
      y: 4.67
    }, {
      name: 'Safari',
      y: 4.18
    }, {
      name: 'Other',
      y: 7.05
    }]
  }]
});


Highcharts.chart('chart3', {
  chart: {
    plotBackgroundColor: null,
    plotBorderWidth: null,
    plotShadow: false,
    type: 'pie'
  },
  title: {
    text: 'Browser market shares in January, 2018'
  },
  tooltip: {
    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
  },
  plotOptions: {
    pie: {
      allowPointSelect: true,
      cursor: 'pointer',
      dataLabels: {
        enabled: false
      },
      showInLegend: true
    }
  },
  series: [{
    name: 'Brands',
    colorByPoint: true,
    legendType: 'point',
    data: [{
      name: 'Chrome',
      y: 61.41,
      sliced: true,
      selected: true
    }, {
      name: 'Internet Explorer',
      y: 11.84
    }, {
      name: 'Firefox',
      y: 10.85
    }, {
      name: 'Edge',
      y: 4.67
    }, {
      name: 'Safari',
      y: 4.18
    }, {
      name: 'Other',
      y: 7.05
    }]
  }]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/7.0.2/highcharts.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="col-md-6">
  <div class="panel">
    <button onclick="switchToDataTable('chart1')">Switch</button>
  </div>
  <div class="panel-body">
    <div id="chart1" style="height: 342px;"></div>
  </div>
</div>

<div class="col-md-6">
  <div class="panel">
    <button onclick="switchToDataTable('chart2')">Switch</button>
    <div class="panel-body">
      <div id="chart2" style="height: 342px;"></div>
    </div>
  </div>
</div>

<div class="col-md-6">
  <div class="panel">
    <button onclick="switchToDataTable('chart3')">Switch</button>
    <div class="panel-body">
      <div id="chart3" style="height: 342px;"></div>
    </div>
  </div>
</div>

我正在研究海图之间的切换,我已经写了一些代码,但是当我单击垂直图表上方的开关时,它没有工作,应该用数据表替换图表。

1 个答案:

答案 0 :(得分:1)

获取图表的方式是错误的。

您应该替换为:

var chart = $('#'+id).Highchart()

与此:

var chart = $("#" + id).highcharts();