admin管理员组

文章数量:1405502

I have four highstock charts "Compare multiple series" in one view Sometimes they load all well, but sometimes random does not load and give error: "Cannot read property 'type' of undefined".

There is my code:

I dont have any idea what i'm doing wrong.

I have four highstock charts "Compare multiple series" in one view Sometimes they load all well, but sometimes random does not load and give error: "Cannot read property 'type' of undefined".

There is my code: http://pastebin./3He5ahzd

I dont have any idea what i'm doing wrong.

Share Improve this question asked Jan 25, 2017 at 23:11 user3723340user3723340 1612 gold badges2 silver badges6 bronze badges 2
  • How are you the loading the js file into your html document? Usually these types of error occur because things are loaded out of order. For example, you are trying to add a chart to a div that doesn't exist yet. – user2263572 Commented Jan 25, 2017 at 23:22
  • Hi, script is at the end of body section before function is: <script src="code.highcharts./stock/highstock.js"></script> <script src="code.highcharts./stock/modules/exporting.js"></…> <script type="text/javascript"> – user3723340 Commented Jan 26, 2017 at 9:15
Add a ment  | 

2 Answers 2

Reset to default 2

It is because you load series for a chart incorrectly.

The code for reproducing the error. Live example

var series = [];
series[1] = {
  data: [1,2,3]
};
Highcharts.chart('container', {
    series: series
});

The error is caused by the series array which is length = 2 but the first element is undefined. In your code it is caused by incorrect usage of ajax requests. The chart can be created before the first ajax request fill the series variable. You should nest the callbacks or use other methods for preventing the race of ajax requests.

        var seriesOptionsmaxload = [];
        $.getJSON('/test/statistics/5/list?parameter=maxLoad&type=wats', function (max) {
            seriesOptionsmaxload[0] = {
                name: 'Maksymalne obciążenie',
                color: '#ff0000',
                data: max
            };

          $.getJSON('/test/statistics/5/list?parameter=maxLoad&type=perKg', function (max) {
            seriesOptionsmaxload[1] = {
                name: 'W/kg',
                color: '#085fbc',
                data: max
            };
            Highcharts.stockChart('maxLoad', {
            ...
            });

          });

I've hit this a 2nd time and now I know what causes it.

When your graph data has series data like this:

series: [1, 2, null, 3, 4]

OR

series: [1, 2, undefined, 3, 4]

It will throw this error because highstock.src.js reveals that highstock iterates through each item in the array and checks the type property like so:

i = seriesOptions && seriesOptions.length;
while (!value && i--) {
  klass = seriesTypes[seriesOptions[i].type]; // ERROR THROWN HERE
  if (klass && klass.prototype[key]) {
    value = true;
  }
}

To prevent this error you can:

(1) remove all non objects from your array

OR

(2) use the alternative series format as follows:

series: [{
  data: [1, 2, undefined, 3, 4]
}]

which will avoid this error.

本文标签: javascriptCannot read property 39type39 of undefined highcharts highstockStack Overflow