Jump to content

Json_encode format problem with Google Charts


bambinou1980

Recommended Posts

Hello,

 

 

I currently have this code:

 




<?php
$query = "SELECT
SUM(cust_order_total) AS label,
due_date AS y
FROM orders
WHERE YEAR(due_date) = YEAR(CURDATE())
AND MONTH(due_date) = MONTH(CURDATE())
GROUP BY due_date
ORDER BY label";
$rows = [];
$result = mysqli_query($connection,$query);
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
?>


 

The output in json format is:

 



[{"label":"95.00","y":"2015-09-01"},{"label":"112.50","y":"2015-09-10"},{"label":"150.00","y":"2015-09-23"}]


 

I am trying to get this data into a line chart from the Google chart API but I just cannot get it to work.

There is an example in jsfiddle here:

 


 

Here is what I tried(Which gives me a blank graph):

 

    

<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart', 'line']});
google.setOnLoadCallback(drawBasic);

function drawBasic() {

var data = new google.visualization.DataTable();
data.addColumn('number', 'Sales');
data.addColumn('date', 'Time');

data.addRows(<?php echo json_encode($rows); ?>);

var options = {
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Popularity'
}
};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

chart.draw(data, options);
}
</script>
<div id="chart_div"></div>



I believe the problem comes from the way my json_encode formats the output, but how to change this to get it to work with the Google Chart API please?

 

 

Thank you,

Link to comment
Share on other sites

I gave you the code for using your query with Google charts two days ago

 

http://forums.phpfreaks.com/topic/297975-php-encode-to-json-help-with-morrisjs-please/?do=findComment&comment=1520002

 

Thank you, yes you are right and I forgot about it....

I had to come back to google charts as I could not parse the output with the other libraries...I did not know it was so complicated to get those charts working properly..

Link to comment
Share on other sites

 

Interesting post but I still do not understand why would a hacker cross site script a graph data, what would be the point if the data is useless to use on another website?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.