【问题标题】:Highcharts oncahnge event It works on the first try but not on the second tryHighcharts onchange事件它在第一次尝试时有效,但在第二次尝试时无效
【发布时间】:2020-11-21 13:13:50
【问题描述】:

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

我想通过select标签改变图形。

所以,我使用 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>

【问题讨论】:

    标签: javascript html highcharts


    【解决方案1】:

    if 格式错误:

    if (x = 0){
    

    应该是

    if (x == 0){
    

    现在可以JSFiddle

    【讨论】:

    • 哦...我很惭愧。谢谢!
    【解决方案2】:

    而不是 if (x=0) use if (x===0) 和其余的 if

    如下:

    if (x = 0){   // Use if (x === 0){ 
       ...
    else if (x = 1){  // use else if (x===1)
        const series = chart.series[0];
       ...
      }else if (x = 2){// use else if (x===2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-13
      • 2016-10-17
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2013-11-17
      相关资源
      最近更新 更多