Format time in google gauge chart

2019-07-25 15:38发布

问题:

Is there anyway to format Google Gauge chart from seconds to time like this?

回答1:

using object notation, you can provide a value (v:) and a formatted value (f:)

{v: 4.2, f: '01:16:04'}

the gauge chart will display the formatted value

see following working snippet...

google.charts.load('current', {
  callback: function () {
    var data = google.visualization.arrayToDataTable([
      ['Label', 'Value'],
      [' ', {v: 4.2, f: '01:16:04'}]
    ]);

    var options = {
      height: 240,
      max: 5,
      minorTicks: 5,
      redFrom: 3.25,
      redTo: 5,
      yellowFrom: 1.65,
      yellowTo: 3.25,
      width: 240
    };

    var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
    chart.draw(data, options);
  },
  packages:['gauge']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>