Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Google Pie Chart based on JSON data using CodeIgniter

I tried to create a pie chart based on example here

In my controller (mycontroller/json) i have the following code:

public function json(){

$whatever = '{
"cols": [
    {"id":"","label":"Topping","pattern":"","type":"string"},
    {"id":"","label":"Slices","pattern":"","type":"number"}
  ],
"rows": [
    {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
    {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
    {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
  ]
}';
echo $whatever;
}

In my view i have the following code (partial code is shown)

var jsonData = $.ajax({
    url: "<?= base_url()?>index.php/bunker/json",
    dataType:"json",
    async: false
    }).responseText;

// Create and populate the data table.
var popularCategory = google.visualization.DataTable(jsonData);
new google.visualization.PieChart(document.getElementById('category')).draw(popularCategory,
    {title:"Popularity by Category"});

And lastly i have a div with an id called 'category'. However, i always get a message saying data is not defined.

A quick debug using firebug, i receive no error, the $whatever variable is also treated as a proper JSON format. What mistakes have i made here?

cheers,

like image 914
Jeremy Avatar asked Jun 07 '26 05:06

Jeremy


1 Answers

instead of:

var popularCategory = google.visualization.DataTable(jsonData);

you must have:

var popularCategory = new google.visualization.DataTable(jsonData);
like image 152
user2341416 Avatar answered Jun 09 '26 01:06

user2341416



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!