jQuery 动态更改 Highcharts 数据系列类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15419979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Highcharts data series type dynamically
提问by agri
I have Highcharts with multiple series and 'select' for each series. My aim is to change series type via 'select'.
我有多个系列的 Highcharts,并为每个系列“选择”。我的目标是通过“选择”更改系列类型。
$('#ChType').change(function () {
var series = chart.series[0]
var newType = $('#ChType').val();
changeType(series, newType);
})
$('#ChType2').change(function () {
var series = chart.series[1]
var newType = $('#ChType2').val();
changeType(series, newType);
})
function changeType(series, newType) {
var dataArray = [],
points = series.data;
series.chart.addSeries({
type: newType,
name: series.name,
color: series.color,
data: series.options.data
}, false);
alert(series.name);
series.remove();
}
I understand that every time the type gets changed, series will get removed from it's current position in an array and added to the end. Currently I'm able to change only the series that is in the position series[0].
我知道每次更改类型时,系列都会从它在数组中的当前位置中删除并添加到末尾。目前,我只能更改位置 series[0] 中的系列。
How can I change any other series and not just the series[0]?
如何更改任何其他系列而不仅仅是系列 [0]?
I did see the similar question herebut couldn't quite get it to work.
My sample code in jsFiddle.
我在jsFiddle 中的示例代码。
回答by Sebastian Bochan
You can use v3.0 highcharts included series.update() method, which allows to change type of chart, dynamically.
您可以使用 v3.0 highcharts 包含的 series.update() 方法,该方法允许动态更改图表类型。
chart.series[0].update({
type: "column"
});
回答by Blake
Why didn't you include the loop that you linked to in the similar question?
你为什么不包括你在类似问题中链接的循环?
$('#ChType').change(function(){
var series;
for ( i = 0 ; i < chart.series.length ; i++ ) {
if ( chart.series[i].name == 'MyData1' ) {
series = chart.series[i];
}
}
//var series = chart.series[0]
var newType = series.type == $('#ChType').val() ? $('#ChType').val()
Put that in place for each type - http://jsfiddle.net/waBck/5/
为每种类型放置它 - http://jsfiddle.net/waBck/5/