更改图表类型给出:未捕获的TypeError:无法读取未定义的Highcharts的属性'length'

时间:2019-06-06 09:53:51

标签: javascript jquery highcharts

我最初是在烛台模式下加载图表的。

然后单击按钮将其更改为 line 模式。

烛台和折线图数据都有不同的数据点

candlestick = [[datetime, open, high, low, close], ....]
line = [[datetime, close], ....]

点击按钮

    if (requiredType == "candlestick") {
        chart.series[seriesindex].update({
            type: "candlestick",
            keys: ['x', 'open', 'high', 'low', 'close'],
            data: candlestick,
        }, true);

    } else if (requiredType == "line") {
        chart.series[seriesindex].update({
            type: "line",
            keys: ['x', 'y'],
            data: line,
        }, true);
    }

最初,图表以烛台模式加载。 单击更改类型按钮后,它会失败并显示以下错误。

Uncaught TypeError: Cannot read property 'length' of undefined
    at Object.high (highstock.src.js:47746)
    at v.ohlc (highstock.src.js:47764)
    at v.l (highstock.src.js:47865)
    at v.t.processData (highstock.src.js:48205)
    at v.F.processData (highstock.src.js:53103)
    at highstock.src.js:16601
    at Array.forEach (<anonymous>)
    at G.setTickInterval (highstock.src.js:16600)
    at G.setScale (highstock.src.js:16936)
    at highstock.src.js:24043
high    @   highstock.src.js:47746
ohlc    @   highstock.src.js:47764
l   @   highstock.src.js:47865
t.processData   @   highstock.src.js:48205
F.processData   @   highstock.src.js:53103
(anonymous) @   highstock.src.js:16601
setTickInterval @   highstock.src.js:16600
setScale    @   highstock.src.js:16936
(anonymous) @   highstock.src.js:24043
redraw  @   highstock.src.js:24041
update  @   highstock.src.js:34110
toggleChart @   fno.js:336
(anonymous) @   fno.js:907
dispatch    @   jquery.js:4435
r.handle    @   jquery.js:4121

但是,如果最初我以行模式加载图表,然后切换到烛台模式,则它不会失败,但是将图表留为空白而不绘制任何内容。

如果我在图表上只有很少的点,例如说10点,并且图表处于烛台模式,并且我将其切换到线形模式,那么它可以正常工作,但是如果有更多的数据说模式超过500点,它将无法工作。

1 个答案:

答案 0 :(得分:1)

您还需要更新dt.Columns.Remove("Date"); dt.Columns.Remove("Time"); ,对于approximation系列,默认情况下将其设置为'average',对于line系列,其默认设置为'ohlc'

ohlc

实时演示: http://jsfiddle.net/BlackLabel/qh7wLc94/

API参考: https://api.highcharts.com/highstock/series.ohlc.dataGrouping.approximation