2014-12-09 10:12


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: '{}: <b>{point.percentage:.1f}%</b>'
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [

... 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!


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dsx58940 dsx58940 7年前

    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.
    点赞 评论 复制链接分享
  • dragon8899 dragon8899 7年前

    You should generate source array like this:

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

    or like this

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

    点赞 评论 复制链接分享
  • douwen7331 douwen7331 7年前

    Try this jQuery.parseJSON


    var stack = <?php echo json_encode( $stack ) ?>;
    stack  = jQuery.parseJSON(stack);
    点赞 评论 复制链接分享