gpt4 book ai didi

javascript - Highcharts - setData() 未正确更新柱形图 - 难住了

转载 作者:行者123 更新时间:2023-12-02 17:09:56 25 4
gpt4 key购买 nike

我有一个 Highcharts 柱形图,正在使用由用户选择的下拉菜单触发的 setData() 进行更新。我确信代码之前工作正常,但现在它不能正确触发 setData() 。请在此处查看我的 JS Fiddle

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>

我没有收到任何错误,并且完全陷入困境,任何帮助或见解将不胜感激!

最佳答案

运行 JSFiddle 并更改为“选项 B”并返回“选项 A”不会执行任何操作,因为数据没有更改。这两个选项(在“选项 C”和“选项 E”的情况下)将数据设置为与创建图表时显示的值完全相同的值。

如果您改为运行 JSFiddle,请在开始在“选项 A”和“选项 B”之间切换之前设置“选项 D”和“选项 F”,您将看到代码显然按预期工作。

这可以通过更改此 if 分支中的任意一个数字来确认:

if (selVal == "a" && selValB == "a" && selValBB == "a") { ...

和/或这个 else if 分支:

else if (selVal == "b" && selValB == "a" && selValBB == "a") { ...

JSFiddle example所示,在所描述的情况下仅更改了两个数字。

话虽这么说,这清楚地说明了这段代码需要整理。

关于javascript - Highcharts - setData() 未正确更新柱形图 - 难住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24922800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com