0

Hope all is well. I am running into a little trouble with setting up a JSON array via PHP and pushing it into Highcharts.

At the moment I generate the array like this:

    $stack[] = array($commname => $countit);
    $stack = json_encode($stack);

When I print_r the array I get the following:

[{"Crude Oil":69},{"Natural Gas":554},{"Liquid Natural Gas":152},{"Power":40},{"Coal":10},{"Weather":21},{"Macroeconomics":67},{"Miscellaneous":45},{"Prices":50},{"Freight":14},{"Forecasts":16}]

I then pass the array to javascript like this:

var stack = <?php echo json_encode( $stack ) ?>;

.. and then pass it into the following highcharts array like this:

var text = {
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: 1,//null,
            plotShadow: false
        },
        title: {
            text: 'Browser market shares at a specific website, 2014'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
                ]
            }]
        };
text.series[0].data.push(stack);

... But this does not work. I think my array 'stack' is not prepared properly, because highcharts wants it to be in this format: [["Crude oil", 35],["Natural Gas", 45] etc...]

Any pointers as to what I am doing wrong? Thank you!

G.

2
  • Convert your JSON to an array in the js part. Commented Dec 9, 2014 at 10:15
  • What error do you get ? Commented Dec 9, 2014 at 10:16

3 Answers 3

1

Try this jQuery.parseJSON

jQuery.parseJSON()

var stack = <?php echo json_encode( $stack ) ?>;
stack  = jQuery.parseJSON(stack);
Sign up to request clarification or add additional context in comments.

1 Comment

Arun, I have found that when I loop through my array like so then it generates the graph properly (see code below). Can I not do the same thing with just a simple line? var i; for (i = 0; i < stack.length; ++i) { text.series[0].data.push(stack[i]); }
0

You have two ways - form json to this form:

{name:"Crude Oil", y:69}
  • get JSON then use loop and push to new series data array and then refer to it in the highcharts option.

1 Comment

This is the correct answer -only when I output the array as follows did it work for me: $stack[] = array('name' => $commname, 'y' => $countit);
0

You should generate source array like this:

$stack[] = array($commname, $countit);

or like this

$stack[] = array('name' => $commname, 'y' => $countit);

Comments

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.