Chart data using Php & Json.
(Populating Data Using Server-Side
Code) – Installing XAMPP
Dr Manzur Ashraf
www.cic.vic.edu.au
Presentation title 2
www.cic.vic.edu.au
You can use server-side code to acquire data to populate your chart. Your
server-side code can load a local file, query a database, or get the data in
some other way. The following PHP example demonstrates reading chart
data from a local text file when a page is requested. You can copy these
files to your own server, if it supports PHP.
Presentation title 3
www.cic.vic.edu.au
Link: https://developers.google.com/chart/interactive/docs/php_example#exampleusingphp.html-file
Presentation title 4
www.cic.vic.edu.au
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getData.php",
dataType: "json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
Presentation title 5
www.cic.vic.edu.au
Link: https://developers.google.com/chart/interactive/docs/php_example#getdata.php-file
<?php
// This is just an example of reading server side data and sending it to the client.
// It reads a json formatted text file and outputs it.
$string = file_get_contents("sampleData.json");
echo $string;
// Instead you can query your database and parse into JSON etc etc
?>
Presentation title 6
www.cic.vic.edu.au
Link: https://developers.google.com/chart/interactive/docs/php_example#sampledata.json-file
{
"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}]}
]
}
Presentation title 7
www.cic.vic.edu.au
XAMPP
Presentation title 8
www.cic.vic.edu.au
Clicking it will open http://localhost...
Presentation title 9
www.cic.vic.edu.au
Put The FILES appropriately
Presentation title 10
www.cic.vic.edu.au
www.cic.vic.edu.au
Prepared by Dr Manzur Ashraf

Data Visualization-and-Data-Modelling-week-6-LAB.pdf

  • 1.
    Chart data usingPhp & Json. (Populating Data Using Server-Side Code) – Installing XAMPP Dr Manzur Ashraf www.cic.vic.edu.au
  • 2.
    Presentation title 2 www.cic.vic.edu.au Youcan use server-side code to acquire data to populate your chart. Your server-side code can load a local file, query a database, or get the data in some other way. The following PHP example demonstrates reading chart data from a local text file when a page is requested. You can copy these files to your own server, if it supports PHP.
  • 3.
    Presentation title 3 www.cic.vic.edu.au Link:https://developers.google.com/chart/interactive/docs/php_example#exampleusingphp.html-file
  • 4.
    Presentation title 4 www.cic.vic.edu.au <html> <head> <!--Loadthe AJAX API--> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> // Load the Visualization API and the piechart package. google.charts.load('current', {'packages':['corechart']}); // Set a callback to run when the Google Visualization API is loaded. google.charts.setOnLoadCallback(drawChart); function drawChart() { var jsonData = $.ajax({ url: "getData.php", dataType: "json", async: false }).responseText; // Create our data table out of JSON data loaded from server. var data = new google.visualization.DataTable(jsonData); // Instantiate and draw our chart, passing in some options. var chart = new google.visualization.PieChart(document.getElementById('chart_div')); chart.draw(data, {width: 400, height: 240}); } </script> </head> <body> <!--Div that will hold the pie chart--> <div id="chart_div"></div> </body> </html>
  • 5.
    Presentation title 5 www.cic.vic.edu.au Link:https://developers.google.com/chart/interactive/docs/php_example#getdata.php-file <?php // This is just an example of reading server side data and sending it to the client. // It reads a json formatted text file and outputs it. $string = file_get_contents("sampleData.json"); echo $string; // Instead you can query your database and parse into JSON etc etc ?>
  • 6.
    Presentation title 6 www.cic.vic.edu.au Link:https://developers.google.com/chart/interactive/docs/php_example#sampledata.json-file { "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}]} ] }
  • 7.
  • 8.
    Presentation title 8 www.cic.vic.edu.au Clickingit will open http://localhost...
  • 9.
  • 10.
  • 11.