首页 > 解决方案 > Highcharts oncahnge 事件它在第一次尝试时有效,但在第二次尝试时无效

问题描述

我正在编写一个表示每日图表的图表。

我想通过选择标签更改图表。

所以,我使用javascript onchange。

但是,它在第​​一次尝试时有效,但在第二次尝试时无效。

https://jsfiddle.net/7eqn02yu/

这是我的代码 JS 小提琴。这是代码。该函数每次如何运行?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>

<button id="export-pdf">Export PDF</button>
<div id="map"></div>
<script src="./index.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>

<select id="mySelect">
<option value="0">mon</option>
<option value="1">tues</option>
<option value="2">wed</option>
<option value="3">thr</option>
</select>

<div id="container" style="height: 400px; width: 500px"></div>

<script type="text/javascript">
const chart = Highcharts.chart('container', {
    plotOptions: {
        series: {
            point: {
            }
        }
    },
    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
});
document.getElementById("mySelect").onchange = function () { myFunction() };
function myFunction() {
    var x = document.getElementById("mySelect").value;
    if (x = 0) {
        const series = chart.series[0];
        if (series.data.length) {
            chart.series[0].remove();
        }
        chart.addSeries({
            data: [100, 100, 100, 100, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5]
        });
    } else if (x = 1) {
        const series = chart.series[0];
        if (series.data.length) {
            chart.series[0].remove();
        }
        chart.addSeries({
            data: [200, 200, 200, 200, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5]
        });
    } else if (x = 2) {
        const series = chart.series[0];
        if (series.data.length) {
            chart.series[0].remove();
        }
        chart.addSeries({
            data: [200, 200, 200, 200]
        });
    }
}
</script>

标签: javascripthtmlhighcharts

解决方案


if格式错误:

if (x = 0){

应该

if (x == 0){

现在它可以工作JSFiddle


推荐阅读