Google charts, timeofday starts at 8:00 and ends at 7:45

喜夏-厌秋 提交于 2020-01-15 09:45:11

问题


I'm having some trouble with Google charts. I have a Google area chart:

<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable();
     data.addColumn('timeofday','time');
     data.addColumn('number','temp');
     data.addRows([
       [[8,0,0],7.875],
       [[8,15,0],7.9399996],
       [[8,30,0],8.1],
       [[8,45,0],8.160001],
       [[9,0,0],8.139999],
       // data every quarter of an hour
       [[7,15,0],9.26],
       [[7,30,0],9.26],
       [[7,45,0],9.18]
     ]);

    var options = {
      title: 'Title',
      vAxis: {
        title: 'AvgTemp',
        titleTextStyle: {color: 'red'},
      }
      hAxis: {
        title: 'Time',
        titleTextStyle: {color: 'red'},
      }
    };

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div2'));
    chart.draw(data, options);
  }
</script>

Now, when I load my page, the hAxis of the chart starts at 0:00 and ends at 24:00, and it destroys the chart. How can I make it so that the chart starts at 8:00 and ends at 7:45?

I've tried some things with view window but I can't seem to make it work.

Thanks.


回答1:


The answer was to use a datetime instead of a time of the day, this way you need to add a date and thats how the api knows which time comes first.

like this:

<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable();
     data.addColumn('datetime','time');
     data.addColumn('number','temp');
     data.addRows([

    [new Date(2012,11,3,8,0,0),7.875],
    [new Date(2012,11,3,8,15,0),7.9399996],
    [new Date(2012,11,3,8,30,0),8.1],
    [new Date(2012,11,3,8,45,0),8.160001],
    [new Date(2012,11,3,9,0,0),8.139999],
    //new data every quarter of an hour
    [new Date(2012,11,4,7,0,0),9.42],
    [new Date(2012,11,4,7,15,0),9.26],
    [new Date(2012,11,4,7,30,0),9.26],
    [new Date(2012,11,4,7,45,0),9.18]

     ]);

    var options = {
      title: 'Title',
      vAxis: {  title: 'AvgTemp',
                titleTextStyle: {color: 'red'},

            },
      hAxis: {  title: 'Time',
                titleTextStyle: {color: 'red'},                 
            }

    };

    var chart = new google.visualization.AreaChart(document.getElementById('chart_div2'));
    chart.draw(data, options);
  }
</script>



回答2:


@oaklodge
producerCountDatatable = new google.visualization.DataTable();
// producerCountDatatable.addColumn('timeofday', 'Timestamp');
producerCountDatatable.addColumn('datetime', 'Time of day(Hours:Minutes)');
producerCountDatatable.addColumn('number', 'ProducerCount');
  for(var i = 0; i < hours.length; i++){
    //If you use timeofday          //producerCountDatatable.addRow([[parseInt(hours[i]),parseInt(minutes[i]),parseInt(seconds[i])],newProducerData[i]]);

//the following is for datetime 
producerCountDatatable.addRow([new Date(timeStampData[i]),newProducerData[i]]);
}


来源:https://stackoverflow.com/questions/13219985/google-charts-timeofday-starts-at-800-and-ends-at-745

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!