dongmeng9048 2014-12-09 02:12
浏览 71
已采纳

找不到Highcharts的正确JSON数组语法

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.

展开全部

  • 写回答

3条回答 默认 最新

  • dsx58940 2014-12-09 03:15
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部