1

Am using Highcharts for pie chart. Below is the JS code for it :

$(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 :

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:

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???

6
  • Did you try to put echo json_encode($data); ? Actually your data is an php array which has no value in js. Anyway, writting that in the value of the input is not the best way.
    – stefanz
    Jun 5, 2013 at 6:04
  • I tried echo json_encode($data); but it gives empty string ...
    – Arul
    Jun 5, 2013 at 6:20
  • any other formats to pass it to pie chart data??? [me] = 99.52 [www.quora.com] = 0.23 [www.slideshare.net] = 0.25
    – Arul
    Jun 5, 2013 at 6:25
  • 1
    Yes, the error is because of what you get from input value looks like an array but actually is a sample string.
    – stefanz
    Jun 5, 2013 at 6:35
  • even i tried to pass as JSON value like : {"www.quora.com":0.23,"www.slideshare.net":0.25,"me":99.52}... But that is also not working
    – Arul
    Jun 5, 2013 at 6:39

1 Answer 1

0

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. You could do that by converting your String using eval:

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:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.