In order for the Highcharts chart data to be retrieved dynamically from the background page, we usually use Ajax to get the data asynchronously, returning the data needed by the front end through its callback function. There are a number of data formats returned: JSON data and pure string data.
When we return data as strings, such as: "7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6"
We get this data that needs to be dynamically assigned to the Data property value of the first sequence in the Highcharts chart, which we will often do:
var"7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6";//定义一个数组varnewArray();//借用js的split()方法直接将字符串转换为数组pDataArr = pStr.split(‘,‘);//然后赋值于serise内的datachart.series[0].update({ data:pDataArr;});
or simply:
//然后赋值于serise内的datachart.series[0].update({ data:pStr;});
Both of these methods are wrong, and the chart simply cannot load the data in.
Cause Analysis:
1, for the first method of assignment, against the data of the principle of non-string, we convert its pstr into an array after you debugger found that each value is actually a string, so with the string array assigned to the data will be an error. The solution is to convert each string in the array to an integer. The sample code looks like this:
var"7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6";vData = str.split(‘,‘);for (var0; i < vData.length; i++) { parseInt//转换数据}//赋值给serieschart.series[0].update({ data:vData;});
2, for the second to directly assign a string to the series data is a big mistake, the data of the series is to be an object, so we have to find a way to format the string as a data object can be. This time we thought of the eval () function, as shown in the example code:
var"7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6";chart.series[0].update({ eval("["+pStr+"]")});
Learn more about Highcharts
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Highcharts Chart Error: What needs to be done before the array directly assigned to data in series by the front end via String.Split ()