How to insert json inside an array in highcharts?

I am generating such an array in php with json nested in an array under chartData:

array:1 [β–Ό
  0 => array:18 [β–Ό
    "id" => 1
    "chart_title" => "A title"
    "chart_type" => "line"
    "chartData" => "[[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]"
  ]
]

I want to access chartDatajson inside this array and insert it into the HighCharts series .

I tried using: window['chart' + chartData.id].series[0].addPoint(chartData, true, shift);

as well as the loop forEach:

chartData.forEach(function(dataPoint){
                        console.log(dataPoint);
                        window['chart' + chartData.id].series[0].addPoint(dataPoint[0], true);
                         dataPoint.slice(0,30).forEach(function(point){
                             window['chart' + chartData.id].series[0].addPoint(point, true, shift);
                         });
                    });

Both do not display errors in the console, and the values ​​do not appear on the chart. If I console.log(dataPoint);get what looks like the correct output: [[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]

How to insert chartDatajson into the highchart series ?

+4
source share
2 answers

, JSON jQuery , , highcharts raw. jQuery.parseJSON(chartData);, .

+1

jQuery :

    var dataPoint =   [[1470406561000,2116],
                       [1470406561000,2116],
                       [1470406562000,2116]];  

    var chart = $('#chart2').highcharts();
    chart.series[0].setData(dataPoint);

    $('#chart2').highcharts().redraw();

, , i.e. #chart2 .

0

Source: https://habr.com/ru/post/1652414/


All Articles