एक से ज़्यादा चार्ट बनाएं

इस पेज पर, एक वेब पेज पर एक से ज़्यादा चार्ट बनाने का तरीका बताया गया है.

एक पेज पर एक से ज़्यादा चार्ट बनाना

अगर आपको एक वेब पेज पर एक से ज़्यादा चार्ट बनाने हैं, तो पेज के <head> में इन चीज़ों के लिए कोड शामिल करें:

  • google.charts.load() के लिए एक ही कॉल में, अपने चार्ट के लिए ज़रूरी सभी पैकेज लोड करें.
  • पेज पर मौजूद हर चार्ट के लिए, उस कॉलबैक वाले google.charts.setOnLoadCallback() को एक कॉल जोड़ें, जो चार्ट को इनपुट के रूप में दिखाता है - उदाहरण के लिए, google.charts.setOnLoadCallback(myPieChart).

उदाहरण के लिए, मान लें कि आपको दो पाई चार्ट बनाना है, जिसमें दिखाया गया है कि सारिका और एंथनी ने पिछली रात कितने पिज़्ज़ा खाते थे. नीचे दिए गए उदाहरण में दोनों चार्ट को एक साथ दिखाने का तरीका बताया गया है.

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">

      // Load Charts and the corechart package.
      google.charts.load('current', {'packages':['corechart']});

      // Draw the pie chart for Sarah's pizza when Charts is loaded.
      google.charts.setOnLoadCallback(drawSarahChart);

      // Draw the pie chart for the Anthony's pizza when Charts is loaded.
      google.charts.setOnLoadCallback(drawAnthonyChart);

      // Callback that draws the pie chart for Sarah's pizza.
      function drawSarahChart() {

        // Create the data table for Sarah's pizza.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
          ['Mushrooms', 1],
          ['Onions', 1],
          ['Olives', 2],
          ['Zucchini', 2],
          ['Pepperoni', 1]
        ]);

        // Set options for Sarah's pie chart.
        var options = {title:'How Much Pizza Sarah Ate Last Night',
                       width:400,
                       height:300};

        // Instantiate and draw the chart for Sarah's pizza.
        var chart = new google.visualization.PieChart(document.getElementById('Sarah_chart_div'));
        chart.draw(data, options);
      }

      // Callback that draws the pie chart for Anthony's pizza.
      function drawAnthonyChart() {

        // Create the data table for Anthony's pizza.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
          ['Mushrooms', 2],
          ['Onions', 2],
          ['Olives', 2],
          ['Zucchini', 0],
          ['Pepperoni', 3]
        ]);

        // Set options for Anthony's pie chart.
        var options = {title:'How Much Pizza Anthony Ate Last Night',
                       width:400,
                       height:300};

        // Instantiate and draw the chart for Anthony's pizza.
        var chart = new google.visualization.PieChart(document.getElementById('Anthony_chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <!--Table and divs that hold the pie charts-->
    <table class="columns">
      <tr>
        <td><div id="Sarah_chart_div" style="border: 1px solid #ccc"></div></td>
        <td><div id="Anthony_chart_div" style="border: 1px solid #ccc"></div></td>
      </tr>
    </table>
  </body>
</html>


एक से ज़्यादा चार्ट बनाने के लिए सिंगल कॉलबैक का इस्तेमाल करना

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

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      // Load Charts and the corechart and barchart packages.
      google.charts.load('current', {'packages':['corechart']});

      // Draw the pie chart and bar chart when Charts is loaded.
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Topping');
        data.addColumn('number', 'Slices');
        data.addRows([
          ['Mushrooms', 3],
          ['Onions', 1],
          ['Olives', 1],
          ['Zucchini', 1],
          ['Pepperoni', 2]
        ]);

        var piechart_options = {title:'Pie Chart: How Much Pizza I Ate Last Night',
                       width:400,
                       height:300};
        var piechart = new google.visualization.PieChart(document.getElementById('piechart_div'));
        piechart.draw(data, piechart_options);

        var barchart_options = {title:'Barchart: How Much Pizza I Ate Last Night',
                       width:400,
                       height:300,
                       legend: 'none'};
        var barchart = new google.visualization.BarChart(document.getElementById('barchart_div'));
        barchart.draw(data, barchart_options);
      }
    </script>
  </head>
<body>
    <!--Table and divs that hold the pie charts-->
    <table class="columns">
      <tr>
        <td><div id="piechart_div" style="border: 1px solid #ccc"></div></td>
        <td><div id="barchart_div" style="border: 1px solid #ccc"></div></td>
      </tr>
    </table>
  </body>
</html>