Google gauge chart

Joined
Jul 16, 2023
Messages
3
Reaction score
0
Hi, I'd like to understand how to edit the code below, in order to have a single static image,
with 2,3 or 4 gauges, each one with different scale, data, colors, everything.

The code come from here: https://developers.google.com/chart/interactive/docs/gallery/gauge


JavaScript:
<html>
  <head>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
      google.charts.load('current', {'packages':['gauge']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Memory', 80],
          ['CPU', 55],
          ['Network', 68]
        ]);

        var options = {
          width: 400, height: 120,
          redFrom: 90, redTo: 100,
          yellowFrom:75, yellowTo: 90,
          minorTicks: 5
        };

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

        chart.draw(data, options);

        setInterval(function() {
          data.setValue(0, 1, 40 + Math.round(60 * Math.random()));
          chart.draw(data, options);
        }, 13000);
        setInterval(function() {
          data.setValue(1, 1, 40 + Math.round(60 * Math.random()));
          chart.draw(data, options);
        }, 5000);
        setInterval(function() {
          data.setValue(2, 1, 60 + Math.round(20 * Math.random()));
          chart.draw(data, options);
        }, 26000);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 400px; height: 120px;"></div>
  </body>
</html>


My attempt to edit:
JavaScript:
<html>
  <head>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
      google.charts.load('current', {'packages':['gauge']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Avg QP', 16.82],
          ['Rate', 51],
          ['Remux', 17.51],
          ['Speed', 3.89]
        ]);

        var options = {
          width: 720, height: 360, min: 15, max: 26,
          redFrom: 25, redTo: 26,
          yellowFrom: 23, yellowTo: 25,
          greenFrom: 16, greenTo: 23,
          minorTicks: 10, majorTicks: ['15','16','17','18','19','20','21','22','23','24','25','26']
        };

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

        chart.draw(data, options);

        setInterval(function() {
          data.setValue('Avg QP');
          chart.draw(data, options);
        });
        setInterval(function() {
          data.setValue('Rate');
          chart.draw(data, options);
        });
        setInterval(function() {
          data.setValue('Remux');
          chart.draw(data, options);
        });
          setInterval(function() {
          data.setValue('Speed');
          chart.draw(data, options);
        });
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 800px; height: 240px;"></div>
  </body>
</html>
 
Joined
Jul 16, 2023
Messages
3
Reaction score
0
This is the first of four current scripts I use, but they are only for 1 gauge,
I need to modify them in a way that I can use one script only that
will display 4 gauges and I can edit each values, scale, colors and ticks for each gauge.


Code:
      google.charts.load('current', {'packages':['gauge']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Avg QP', 20],
        ]);

        var options = {
          width: 200, height: 200,
          min: 15, max: 26,
          redFrom: 25, redTo: 26,
          yellowFrom:23, yellowTo: 25,
          greenFrom:16, greenTo: 23,
          minorTicks: 5, majorTicks: ['15','16','17','18','19','20','21','22','23','24','25','26']
        };

        var chartA = new google.visualization.Gauge(document.getElementById('chart_div'));

        chartA.draw(data, options);
      }
 
Joined
Jul 16, 2023
Messages
3
Reaction score
0
I'm trying to edit again in this way unsucessfully.


Code:
<html>
  <head>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
      google.charts.load('current', {'packages':['gauge']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Avg QP', 20],
          ['Ratio', 60],
          ['Remux', 25],
          ['Speed', 2],         
        ]);

        var options1 = {
          width: 800, height: 200,
              min: 15, max: 26,
          redFrom: 25, redTo: 26,
          yellowFrom:23, yellowTo: 25,
              greenFrom:16, greenTo: 23,
          minorTicks: 5, majorTicks: ['15','16','17','18','19','20','21','22','23','24','25','26']
        };
        
        var options2 = {
          width: 800, height: 200,
              min: 0, max: 100,
          redFrom: 90, redTo: 100,
          yellowFrom:80, yellowTo: 90,
              greenFrom:20, greenTo: 80,
          minorTicks: 5, majorTicks: ['0','10','20','30','40','50','60','70','80','90','100']
        };
        
        var options3 = {
          width: 800, height: 200,
              min: 5, max: 35,
          redFrom: 30, redTo: 35,
          yellowFrom:25, yellowTo: 30,
              greenFrom:10, greenTo: 25,
          minorTicks: 5, majorTicks: ['5','10','15','20','25','30','35']
        };
        
        var options4 = {
          width: 800, height: 200,
              min: 0, max: 10,
          redFrom: 0, redTo: 2,
          yellowFrom:2, yellowTo: 3,
              greenFrom:3, greenTo: 10,
          minorTicks: 5, majorTicks: ['0','1','2','3','4','5','6','7','8','9','10']
        };

        var chart1 = new google.visualization.Gauge(document.getElementById('chart_div'));
        var chart2 = new google.visualization.Gauge(document.getElementById('chart_div'));
        var chart3 = new google.visualization.Gauge(document.getElementById('chart_div'));
        var chart4 = new google.visualization.Gauge(document.getElementById('chart_div'));

        chart1.draw(data, options1);
        chart2.draw(data, options2);
        chart3.draw(data, options3);
        chart4.draw(data, options4);
        
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 800px; height: 2000px;"></div>
  </body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top