Stay organized with collections
Save and categorize content based on your preferences.
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.
This is the file that the user browses to. The drawChart() function calls the jQuery ajax() function to send a query to a URL and get back a JSON string. The URL here is of the local getData.php file. The returned data is actually a DataTable defined in the local sampleData.json file. This DataTable is used to populate a pie chart, which is then rendered on the page.
<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>
getData.php File
When this file receives a request, it returns a copy of the local sampleData.json 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?>
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eServer-side code, like PHP, can be used to fetch data for your charts, retrieving it from sources like local files or databases.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example uses PHP to read chart data from a local JSON file (\u003ccode\u003esampleData.json\u003c/code\u003e) and sends it to the client-side.\u003c/p\u003e\n"],["\u003cp\u003eThe client-side JavaScript code then utilizes the Google Charts library and jQuery's AJAX function to request this data and render a pie chart.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eexampleUsingPHP.html\u003c/code\u003e, \u003ccode\u003egetData.php\u003c/code\u003e, and \u003ccode\u003esampleData.json\u003c/code\u003e files work together to display this interactive chart.\u003c/p\u003e\n"]]],[],null,["# Populating Data Using Server-Side Code\n\nYou 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.\n\n**Note:** This sample requires [jQuery version 1.6.2](http://docs.jquery.com/Downloading_jQuery) or later.\n\n### exampleUsingPHP.html file\n\nThis is the file that the user browses to. The drawChart() function calls the [jQuery ajax() function](http://api.jquery.com/jQuery.ajax/) to send a query to a URL and get back a JSON string. The URL here is of the local getData.php file. The returned data is actually a `DataTable` defined in the local sampleData.json file. This `DataTable` is used to populate a pie chart, which is then rendered on the page. \n\n```php\n\u003chtml\u003e\n \u003chead\u003e\n \u003c!--Load the AJAX API--\u003e\n \u003cscript type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"\u003e\u003c/script\u003e\n \u003cscript type=\"text/javascript\" src=\"//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js\"\u003e\u003c/script\u003e\n \u003cscript type=\"text/javascript\"\u003e\n \n // Load the Visualization API and the piechart package.\n google.charts.load('current', {'packages':['corechart']});\n \n // Set a callback to run when the Google Visualization API is loaded.\n google.charts.setOnLoadCallback(drawChart);\n \n function drawChart() {\n var jsonData = $.ajax({\n url: \"getData.php\",\n dataType: \"json\",\n async: false\n }).responseText;\n \n // Create our data table out of JSON data loaded from server.\n var data = new google.visualization.DataTable(jsonData);\n\n // Instantiate and draw our chart, passing in some options.\n var chart = new google.visualization.PieChart(document.getElementById('chart_div'));\n chart.draw(data, {width: 400, height: 240});\n }\n\n \u003c/script\u003e\n \u003c/head\u003e\n\n \u003cbody\u003e\n \u003c!--Div that will hold the pie chart--\u003e\n \u003cdiv id=\"chart_div\"\u003e\u003c/div\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### getData.php File\n\nWhen this file receives a request, it returns a copy of the local sampleData.json file. \n\n```php\n\u003c?php \n\n// This is just an example of reading server side data and sending it to the client.\n// It reads a json formatted text file and outputs it.\n\n$string = file_get_contents(\"sampleData.json\");\necho $string;\n\n// Instead you can query your database and parse into JSON etc etc\n\n?\u003e\n```\n\n### sampleData.json File\n\nA [JSON version of a small `DataTable`](/chart/interactive/docs/reference#dataparam). \n\n```javascript\n{\n \"cols\": [\n {\"id\":\"\",\"label\":\"Topping\",\"pattern\":\"\",\"type\":\"string\"},\n {\"id\":\"\",\"label\":\"Slices\",\"pattern\":\"\",\"type\":\"number\"}\n ],\n \"rows\": [\n {\"c\":[{\"v\":\"Mushrooms\",\"f\":null},{\"v\":3,\"f\":null}]},\n {\"c\":[{\"v\":\"Onions\",\"f\":null},{\"v\":1,\"f\":null}]},\n {\"c\":[{\"v\":\"Olives\",\"f\":null},{\"v\":1,\"f\":null}]},\n {\"c\":[{\"v\":\"Zucchini\",\"f\":null},{\"v\":1,\"f\":null}]},\n {\"c\":[{\"v\":\"Pepperoni\",\"f\":null},{\"v\":2,\"f\":null}]}\n ]\n}\n```\n\n**More Information:**\n\n- [Downloading jQuery](http://docs.jquery.com/Downloading_jQuery)\n- [`DataTable` JSON format](/chart/interactive/docs/reference#dataparam)"]]