我有一個Highcharts Column圖,我正在使用由用戶選定的下拉菜單觸發的setData()進行更新。我確信之前的代碼工作正常,但現在它不能正確觸發setData()。請參閱我的JS小提琴這裏Highcharts - setData()不更新列圖正確 - 難倒
http://jsfiddle.net/mshirk/6QYzD/18/
這裏是我的JavaScript和HTML
$(function() {
var chartBench;
$(document).ready(function() {
chartBench = new Highcharts.Chart({
chart: {
renderTo: 'containerYo',
type: 'column'
},
title: {
text: ''
},
credits: {
enabled: false
},
legend: {},
plotOptions: {
series: {
shadow: false,
borderWidth: 0
}
},
xAxis: {
lineColor: '#999',
lineWidth: 1,
tickColor: '#666',
tickLength: 3,
categories: ['2011', '2012', '2013', '2014'],
title: {
text: 'Years'
}
},
yAxis: {
lineColor: '#999',
lineWidth: 1,
tickColor: '#666',
tickWidth: 1,
tickLength: 3,
gridLineColor: '#ddd',
labels: {
format: '$ {value}'
},
title: {
text: ''
}
},
series: [{
name: 'Value A',
data: [
[201],
[211],
[221],
[131]
]
}, {
name: 'Value B',
data: [
[192],
[202],
[212],
[122]
]
}, {
name: 'Value C',
data: [
[183],
[193],
[203],
[113]
]
}, {
name: 'Value D',
data: [
[174],
[184],
[194],
[104]
]
}]
});
});
$("#list").on('change', function() {
//alert('f')
var selVal = $("#list").val();
var selValB = $("#listb").val();
var selValBB = $("#listc").val();
if (selVal == "a" && selValB == "a" && selValBB == "a") {
chartBench.series[0].setData(
[
[201],
[211],
[221],
[131]
]);
chartBench.series[1].setData(
[
[192],
[202],
[212],
[122]
]);
chartBench.series[2].setData(
[
[183],
[193],
[203],
[113]
]);
chartBench.series[3].setData(
[
[174],
[184],
[194],
[104]
]);
} else if (selVal == "a" && selValB == "a" && selValBB == "b") {
chartBench.series[0].setData(
[
[201],
[211],
[221],
[131]
]);
chartBench.series[1].setData(
[
[192],
[202],
[212],
[122]
]);
chartBench.series[2].setData(
[
[183],
[193],
[203],
[113]
]);
chartBench.series[3].setData(
[
[174],
[184],
[194],
[104]
]);
} else if (selVal == "b" && selValB == "a" && selValBB == "a") {
chartBench.series[0].setData(
[
[201],
[211],
[221],
[131]
]);
chartBench.series[1].setData(
[
[192],
[202],
[212],
[122]
]);
chartBench.series[2].setData(
[
[183],
[193],
[203],
[113]
]);
chartBench.series[3].setData(
[
[174],
[184],
[194],
[104]
]);
} else if (selVal == "b" && selValB == "a" && selValBB == "b") {
chartBench.series[0].setData(
[
[141],
[111],
[121],
[131]
]);
chartBench.series[1].setData(
[
[132],
[102],
[112],
[122]
]);
chartBench.series[2].setData(
[
[123],
[93],
[103],
[113]
]);
chartBench.series[3].setData(
[
[114],
[84],
[94],
[104]
]);
} else if (selVal == "b" && selValB == "a" && selValBB == "a") {
chartBench.series[0].setData(
[
[201],
[211],
[221],
[131]
]);
chartBench.series[1].setData(
[
[192],
[202],
[212],
[122]
]);
chartBench.series[2].setData(
[
[183],
[193],
[203],
[113]
]);
chartBench.series[3].setData(
[
[174],
[184],
[194],
[104]
]);
} else if (selVal == "b" && selValB == "a" && selValBB == "b") {
chartBench.series[0].setData(
[
[141],
[111],
[121],
[131]
]);
chartBench.series[1].setData(
[
[132],
[102],
[112],
[122]
]);
chartBench.series[2].setData(
[
[123],
[93],
[103],
[113]
]);
chartBench.series[3].setData(
[
[114],
[84],
[94],
[104]
]);
} else if (selVal == "a" && selValB == "b" && selValBB == "a") {
chartBench.series[0].setData(
[
[151],
[251],
[221],
[231]
]);
chartBench.series[1].setData(
[
[172],
[242],
[212],
[222]
]);
chartBench.series[2].setData(
[
[183],
[233],
[203],
[213]
]);
chartBench.series[3].setData(
[
[154],
[224],
[194],
[204]
]);
} else if (selVal == "a" && selValB == "b" && selValBB == "b") {
chartBench.series[0].setData(
[
[151],
[251],
[221],
[231]
]);
chartBench.series[1].setData(
[
[172],
[242],
[212],
[222]
]);
chartBench.series[2].setData(
[
[183],
[233],
[203],
[213]
]);
chartBench.series[3].setData(
[
[154],
[224],
[194],
[204]
]);
} else if (selVal == "b" && selValB == "b" && selValBB == "a") {
chartBench.series[0].setData(
[
[151],
[251],
[221],
[231]
]);
chartBench.series[1].setData(
[
[172],
[242],
[212],
[222]
]);
chartBench.series[2].setData(
[
[183],
[233],
[203],
[213]
]);
chartBench.series[3].setData(
[
[154],
[224],
[194],
[204]
]);
} else if (selVal == "b" && selValB == "b" && selValBB == "b") {
chartBench.series[0].setData(
[
[141],
[151],
[161],
[171]
]);
chartBench.series[1].setData(
[
[132],
[142],
[172],
[162]
]);
chartBench.series[2].setData(
[
[123],
[133],
[183],
[153]
]);
chartBench.series[3].setData(
[
[114],
[124],
[194],
[144]
]);
} else {
}
});
});
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div>
<div style="display:inline-block; padding-right:20px; padding-left:20px;">
<div>1: <select id="list">
<option id="optiona" value="a" selected=yes >Option A</option>
<option id="optionb" value="b">Option B</option>
</select></div>
</div>
</div>
<div id="containerYo" style="width: 528px; height: 400px; margin: 0 auto"></div>
我沒有收到任何錯誤,我完全難倒,任何幫助或洞察力將不勝感激!
你有沒有嘗試剝下你的代碼,看看你是否可以用最低限度的代碼重現錯誤?通常我會發現自己解決了自己的錯誤,而且在SO上發佈通常也是更好的選擇。 – sage88
感謝您的幫助,我在這裏和JSFiddle儘可能簡化了代碼。下拉菜單仍然不會觸發setData() – user3826864