Highcharts - 使用jQuery动态更改图表类型和数据

时间:2018-03-18 13:04:50

标签: javascript jquery dynamic highcharts

可以找到大量关于动态更改图表类型或数据输入的示例。如果您想为同一个图表应用两个输入但又希望彼此分开执行,那么遗憾的是没有示例。

this示例中,有两个下拉列表,其中一个下拉列表可以选择图表类型,另一个下拉列表可以选择数据。我希望当您选择不同的图表类型时,图表会再次进行动画处理。选择新数据时也是如此。此外,当您首先必须启动时,标准变量现在已填充,以便您始终可以看到图形。

数据下拉列表有效。不幸的是,图表类型不起作用。这是唯一不起作用的东西。

HTML code:

<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>

<body>
<div class="container">
  <div class="panel panel-info">
    <div class="panel-heading">
      <select class="selectpicker" id="chartType">
    <optgroup label="select chart type">
    <option value="line">line</option>
    <option value="bar">bar</option>     
    <option value="line">line</option>
    <option value="pie">pie</option>  
</optgroup>        
</select>
      <select class="selectpicker" id="data">
    <optgroup label="select chart type">
    <option value="data1">data1</option>
    <option value="data2">data2</option>     
     <option value="data3">data3</option>
     <option value="data4">data4</option> 
</optgroup>        
</select>
    </div>
    <div class="panel-body">
      <div id="grafiek_bench_rendement"></div>
    </div>
  </div>
</div>
</body>

JavaScript代码:

$(function() {

//create a variable so we can pass the value dynamically
var chartype = 'line';
var chartTitle = 'Browser Chart';
var chartCategories = ['Africa', 'America', 'Asia', 'Europe', 'Oceania'];
var chartData = [{
name: 'Year 1800',
data: [107, 31, 635, 203, 2]
}, {
name: 'Year 1900',
data: [133, 156, 947, 408, 6]
}, {
name: 'Year 2008',
data: [973, 914, 4054, 732, 34]
}];

//On page load call the function setDynamicChart
setDynamicChart(chartype, chartTitle, chartCategories, chartData);

//jQuery part - On Click call the function setDynamicChart(dynval) and pass the chart type
$("#chartType").change(function() {
//get the value from 'a' tag
var chartype = this.value;
setDynamicChart(chartype);
});

$("#data").change(function() {
//get the value from 'a' tag
var data = this.value;

if (data == 'data1') {
  chartype = chartype;
  chartTitle = 'Browser Chart';
  chartCategories = ['Africa', 'America', 'Asia', 'Europe', 'Oceania'];
  chartData = [{
    name: 'Year 1800',
    data: [107, 31, 635, 203, 2]
  }, {
    name: 'Year 1900',
    data: [133, 156, 947, 408, 6]
  }, {
    name: 'Year 2008',
    data: [973, 914, 4054, 732, 34]
  }];
  setDynamicChart(chartype, chartTitle, chartCategories, chartData);
  } else if (data == 'data2') {
  chartTitle = 'Monthly Average Temperature';
  chartCategories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  chartData = [{
    name: 'Tokyo',
    data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
  }, {
    name: 'New York',
    data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
  }, {
    name: 'Berlin',
    data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
  }, {
    name: 'London',
    data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
  }];
  setDynamicChart(chartype, chartTitle, chartCategories, chartData);
  } else if (data == 'data3') {
  chartTitle = 'Monthly Average Rainfall';
  chartCategories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  chartData = [{
    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]
  }];
  setDynamicChart(chartype, chartTitle, chartCategories, chartData);
  } else if (data == 'data4') {
  chartTitle = 'Monthly Average Temperature';
  chartData = [{
    type: 'pie',
    name: 'Browser share',
    data: [
      ['Firefox', 45.0],
      ['IE', 26.8], {
        name: 'Chrome',
        y: 12.8,
        sliced: true,
        selected: true
      },
      ['Safari', 8.5],
      ['Opera', 6.2],
      ['Others', 0.7]
    ]
  }];
  setDynamicChart(chartype, chartTitle, chartCategories, chartData);
  }
});

//function is created so we pass the value dynamically and be able to refresh the HighCharts on every click

function setDynamicChart(chartype, chartTitle, chartCategories, chartData) {
$('#grafiek_bench_rendement').highcharts({
  chart: {
    type: chartype
  },
  title: {
    text: chartTitle
  },
  xAxis: {
    categories: chartCategories
  },
  yAxis: {
    min: 0,
    title: {
      text: 'Total fruit consumption'
    }
  },
  plotOptions: {
    //this need only for pie chart
    pie: {
      allowPointSelect: true,
      cursor: 'pointer'
    }
  },
  series: chartData
});
}
});

我使用以下两个示例作为指南: Example 1 Example 2

希望你能帮助我解决这个问题!

0 个答案:

没有答案