谷歌图表如何显示显示时间统计

时间:2014-02-20 09:27:20

标签: time charts google-visualization

您好我想在谷歌图表中显示两列。首先是时间(例如06:02),其次是值(例如,3)。

我试试:

<script type="text/javascript">
  google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
  function drawVisualization() {

  var data = new google.visualization.DateFormat();

  // Declare columns
  data.addColumn('datetime', 'Time of Day');
  data.addColumn('number', 'Some Measurement');

  // Add data.
  data.addRows([
    [06:02,1],
    [07:02,1],
    [08:02,2],
    [12:02,15],
    [13:02,2],
    [14:02,1],
    [15:02,1],
    [16:02,1],
    [21:02,2],

  ]);

    // Create and draw the visualization.
    new google.visualization.LineChart(document.getElementById('visualization')).
        draw(data, {curveType: "function",
                    width: 500, height: 400,
                    vAxis: {maxValue: 10}}
            );
  }


  google.setOnLoadCallback(drawVisualization);

我尝试google doc但我必须做错事...... 它确实无效......任何想法......

1 个答案:

答案 0 :(得分:1)

数据的类型和格式错误,应为timeofday[hour, min, sec, milli]。似乎可以省略最后一个毫秒。此外,还必须创建DataTable

<script type="text/javascript">

google.load("visualization", "1", {packages:["corechart"]});

function drawVisualization() {

    var data = new google.visualization.DataTable();

    // Declare columns
    data.addColumn('timeofday', 'Time of Day');
    data.addColumn('number', 'Some Measurement');

    // Add data.
    data.addRows([
        [[6, 2, 0], 1],
        [[7, 2, 0], 1],
        [[8, 2, 0], 2],
        [[12, 2, 0], 15],
        [[13, 2, 0], 2],
        [[14, 2, 0], 1],
        [[15, 2, 0], 1],
        [[16, 2, 0], 1],
        [[21, 2, 0], 2]
    ]);

    // Create and draw the visualization.
    new google.visualization.LineChart(document.getElementById('visualization')).
        draw(data, {
                curveType: "function",
                width: 500, height: 400,
                vAxis: {maxValue: 10}
            }
    );
}

google.setOnLoadCallback(drawVisualization);

</script>

如果您的测量结果超过几天,则必须根据Google charts, timeofday starts at 8:00 and ends at 7:45

中的说明更改图表