dongmeng9048 2014-12-09 10: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 11: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 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?