4

I generate an array like so in php with json nested in the 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 access the chartData json within this array and insert it into a HighCharts series.

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

and also a forEach loop:

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 don't show any errors in the console and the values don't show up on the chart. If I console.log(dataPoint); I get what looks like the correct output: [[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]

How would I insert the chartData json into a highchart series?

2 Answers 2

1

My issue was the JSON was not being parsed by jQuery on the view and essentially passing it to the highcharts series raw. By adding jQuery.parseJSON(chartData); It was able to parse correctly and display on the chart.

Sign up to request clarification or add additional context in comments.

Comments

0

With jQuery you can try like this:

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

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

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

Please make sure you change chart id i.e. #chart2 correctly as per your code.

7 Comments

Thanks @vijayP I tried this works without any issue if I set the dataPoint var in jquery but if I try to set it using using the array like var dataPoint = chartData; It draws the chart like so oi68.tinypic.com/29djria.jpg
image you posted is not accessible to me as of now. can you console.log(chartData) before statement dataPoint = chartData; and check it.
I console.logged the data and the output is [[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]
its same as other one right...? any error in console? Logically it should work.
Added jQuery.parseJSON(chartData); and that fixed it. Thanks for your help Vijay
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.