首页 > 解决方案 > Highchart 的错误:Uncaught SyntaxError: Unexpected token for

问题描述

所以我想做的是我试图根据我在数组中的数据在特定的 X 值上创建 Plotline。

我有 2 个数据数组:

 featurex = [95,193,295,393,480,587,700,799,912,1015,1123,1230,1336,1443,1554] ;
 featurey = [0,0,0,0,0,3,0,0,0,0,0,0,0,0,0];

现在我想要做的是我试图在 X 轴上绘制一条情节线,其中我的 Y 轴为 3 如您在上面看到的。

我能够静态地做到这一点,但我希望它是动态的。

这是我正在使用的 Highchart 的代码。

Highcharts.chart('ppg', {
    chart: {
        type: 'line'
    },
    title: {
        text: 'ECG Data'
    },
    subtitle: {
        text: ''
    },
    xAxis: 

       for( i=0; i<featurex.length; i++)
     {
        if (featurey[i] == 3) {
            plotLines: [

                   {
                        value: featurex[i],
                        color: '#005a9b94',
                        dashStyle: 'shortdash',
                        width: 100,
                    },

        }


      },
      crosshair: false

    },
    yAxis: {
        title: {
            text: 'Peaks'

        }
    },
   tooltip: {
        enable: false
    },
    plotOptions: {
        column: {
            pointPadding: 0.2,
            borderWidth: 0
        }
    },
    series: [{
        name: '',
        lineWidth: 2,
        data: yData,
         animation: {
                duration: 5000
            }
         },
   {    type: 'scatter',
        name: 'Installation',
        data: zip(xPeak, yPeak),
        animation: {
                duration: 5200
            }
      },



     ]
});

});

});
</script>

这里的 Plotline 代码Xaxis是主要关注的领域。它给我一个错误:

未捕获的 SyntaxError:意外的令牌

请指导我做错了什么。

非常感谢任何帮助。谢谢。

标签: javascripthighcharts

解决方案


问题就在这里,

xAxis: 
    for( i=0; i<featurex.length; i++)

以上不是有效的 javascript 语法。

如果您需要动态的绘图线,请传递一个返回数组的函数

xAxis: {
  plotLines: generateDynamicPlotLines(),
}

你的功能会是这样的,

function generateDynamicPlotLines() {
    const plotLines = [];
    for(var i=0; i<featurex.length; i++) {
       if (featurey[i] == 3) {
           plotLines.push({
           value: featurex[i],
           color: '#005a9b94',
           dashStyle: 'shortdash',
           width: 100,
       });
     }
   }
   return plotLines;
}

推荐阅读