सर्वर-साइड कोड का इस्तेमाल करके डेटा अपने-आप भरना

चार्ट पाने के लिए, सर्वर साइड कोड का इस्तेमाल करके डेटा पाया जा सकता है. आपका सर्वर-साइड कोड एक स्थानीय फ़ाइल लोड कर सकता है, डेटाबेस से क्वेरी कर सकता है या डेटा को किसी दूसरे तरीके से पा सकता है. नीचे दिए गए उदाहरण में, पेज का अनुरोध करने पर स्थानीय टेक्स्ट फ़ाइल से चार्ट पढ़ने का डेटा दिखाया गया है. अगर यह फ़ाइल PHP के साथ काम करती है, तो आप इसे अपने सर्वर पर कॉपी कर सकते हैं.

ध्यान दें: इस नमूने के लिए, jQuery 1.6.2 या इसके बाद का वर्शन ज़रूरी है.

examplePHP.html फ़ाइल का इस्तेमाल करना

इस फ़ाइल को उपयोगकर्ता ब्राउज़ करता है. Draw चार्ट() फ़ंक्शन यूआरएल को क्वेरी भेजने और JSON स्ट्रिंग वापस पाने के लिए jQuery ajax() फ़ंक्शन को कॉल करता है. यह यूआरएल स्थानीय getData.php फ़ाइल का है. दिया गया डेटा असल में DataTable होता है. इसके बारे में, localsampleData.json फ़ाइल में बताया जाता है. DataTable का इस्तेमाल, पाई चार्ट को पॉप्युलेट करने के लिए किया जाता है. इसके बाद, पेज पर रेंडर हो जाता है.

<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 फ़ाइल

जब इस फ़ाइल को कोई अनुरोध मिलता है, तो यह localsampleData.json फ़ाइल की कॉपी दिखाता है.

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

?>

sampleData.json फ़ाइल

छोटे DataTable का JSON वर्शन.

{
  "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}]}
      ]
}

ज़्यादा जानकारी: