Google使用数字绘制时间表

时间:2015-02-18 13:51:15

标签: javascript google-visualization

我正在尝试使用Google图表库中的时间轴,而不是日期。当我更改提供的基本示例以使用数字而不是日期时,我在页面上获得以下输出,而控制台中没有错误。 Cannot read property 'v' of undefined

以下是我修改https://developers.google.com/chart/interactive/docs/gallery/timeline#SimpleExample

的基本示例

只需将开始和结束类型从日期更改为数字,然后将每行的开始和结束值更改为数字似乎不起作用。

这是一个显示问题http://jsfiddle.net/t26yu0w8/

的jsfiddle

3 个答案:

答案 0 :(得分:2)

我想指出当前答案是针对annotatedtimeline的,这与OP询问的timeline不同。 Time line要求数据格式与annotated time line不同。

Start类型中的Endnumber列实际上是指在unix时间内的日期。例如。 new Date(2016,0,1).getTime()

这是有效的OP's jsfiddle

答案 1 :(得分:1)

我修改了你的脚本。

<html>
  <head>
    <script type='text/javascript' src='http://www.google.com/jsapi'></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {'packages':['annotatedtimeline']});
      google.setOnLoadCallback(drawChart);
function drawChart() {
        var dataTable = new google.visualization.DataTable();
        dataTable.addColumn('date', 'Date');
        dataTable.addColumn( 'number', 'Start' );
        dataTable.addColumn( 'number', 'End' );
        dataTable.addColumn( 'string', 'President' );
        dataTable.addRows([
          [new Date(2015, 1 ,1), 5 , 9,'Washington' ],
          [new Date(2015, 1 ,1),      10,  12,  'Adams' ],
          [new Date(2015, 1 ,1),  1,  7, 'Jefferson' ]]);




        var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
        chart.draw(dataTable, {displayAnnotations: true});
      }
    </script>
  </head>

  <body>
    // Note how you must specify the size of the container element explicitly!
    <div id='chart_div' style='width: 700px; height: 240px;'></div>

  </body>
</html>

如果您使用此图表 第一个数据必须是日期。

我可以为你举个例子。

<html>
  <head>
    <script type='text/javascript' src='http://www.google.com/jsapi'></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {'packages':['annotatedtimeline']});
      google.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', 'Sold Pencils');
        data.addColumn('string', 'title1');
        data.addColumn('string', 'text1');
        data.addColumn('number', 'Sold Pens');
        data.addColumn('string', 'title2');
        data.addColumn('string', 'text2');
        data.addRows([
          [new Date(2008, 1 ,1), 30000, undefined, undefined, 40645, undefined, undefined],
          [new Date(2008, 1 ,2), 14045, undefined, undefined, 20374, undefined, undefined],
          [new Date(2008, 1 ,3), 55022, undefined, undefined, 50766, undefined, undefined],
          [new Date(2008, 1 ,4), 75284, undefined, undefined, 14334, 'Out of Stock','Ran out of stock on pens at 4pm'],
          [new Date(2008, 1 ,5), 41476, 'Bought Pens','Bought 200k pens', 66467, undefined, undefined],
          [new Date(2008, 1 ,6), 33322, undefined, undefined, 39463, undefined, undefined]
        ]);
        var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
        chart.draw(data, {displayAnnotations: true});
      }
    </script>
  </head>

  <body>
    // Note how you must specify the size of the container element explicitly!
    <div id='chart_div' style='width: 700px; height: 240px;'></div>

  </body>
</html>

答案 2 :(得分:1)

通过使用数字作为列类型而不是日期,Google会正确或错误地假设您使用毫秒。

如果您按如下方式更新数据:
    [new Date(2015, 1 ,1), 5000, 9000,'Washington' ],
    [new Date(2015, 1 ,1),10000,12000,'Adams' ],
    [new Date(2015, 1 ,1), 1000, 7000,'Jefferson' ]]);

它会显示一些东西。