简体   繁体   English

Highcharts饼图数据通过php设置格式

[英]Highcharts Pie chart data Format via php

Am using Highcharts for pie chart. 我正在使用Highcharts绘制饼图。 Below is the JS code for it : 下面是它的JS代码:

$(document).ready(function () {
var chart;

var graphData = $("#graphContentdata").val();
console.log(graphData);
var options = {
        chart: {
            renderTo: 'graphContainer',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'SOV'
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
            }
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
           data: []
        }]
    }

    chart = new Highcharts.Chart(options);
    chart.series[0].setData("["+graphData+"]");

 });

And PHP Code for generating the Data : 以及用于生成数据的PHP代码:

foreach ($sumArray as $key=>$value)
    {
        $sumArray[$key] = array_sum($value);
        $percent = (number_format(($sumArray[$key]/$sovGrandTotal)*100,2));

        $data[] =  "['$key', $percent]";            
    }
    $data = join(",", $data);

?>               
  <input type='hidden' id='graphContentdata' value="<?php echo $data; ?>">  

The Data format Am getting is : 我得到的数据格式是:

['www.quora.com', 0.23],['www.slideshare.net', 0.25],['me', 99.52]

Problem is : 问题是:

chart.series[0].setData("["+graphData+"]");

not creating graph, but if i copy paste the value from console at palce of graphData variable, its working like: 不创建图形,但是如果我从控制台复制值, graphDatagraphDatagraphData变量的位置,其工作方式如下:

chart.series[0].setData([['www.quora.com', 0.23],['www.slideshare.net', 0.25],['me', 99.52]]);

is working. 正在工作。

What's the mistake am doing here??? 这是怎么了?

The problem is your are passing your data as a String when you are using 问题是您在使用时将数据作为字符串传递

chart.series[0].setData("["+graphData+"]");

You need to set your data as an Array . 您需要将数据设置为Array You could do that by converting your String using eval : 您可以通过使用eval转换String来实现:

chart.series[0].setData(eval("["+graphData+"]"));

You could also use jQuery to convert your String to JSON , but in that case you must use double quotes to open and close Strings: 您还可以使用jQuery将String转换为JSON ,但是在这种情况下, 必须使用双引号打开和关闭String:

$.parseJSON("[[\"www.quora.com\", 0.23],[\"www.slideshare.net\", 0.25],[\"me\", 99.52]]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM