将信息添加到Google时间轴栏上

时间:2018-07-15 20:57:51

标签: html charts google-visualization google-timeline-chart

我正在尝试将三个新的部分添加到Google时间线图表中的一个栏上的悬停弹出窗口上。

我尝试使用google timeline帮助,但找不到解决方法

默认值为标题/时间/持续时间,但是我要添加竞技场/网站

我为此创建了以下代码。

<DIV>
  <p><font face="verdana" size="6" color="Black">Thursday</font></p>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  <script type="text/javascript">
  google.charts.load("current", {packages:["timeline"]});
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {
    var container = document.getElementById('example5.1');
    var chart = new google.visualization.Timeline(container);
    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn({ type: 'string', id: 'Federation' });
    dataTable.addColumn({ type: 'string', id: 'Event' });
    dataTable.addColumn({ type: 'date', id: 'Start' });
    dataTable.addColumn({ type: 'date', id: 'End' });
    dataTable.addRows([
      [ 'WWE / NXT',  'AXXESS',    new Date(0,0,0,18,0,0),  new Date(0,0,0,22,0,0)],
      [ 'WWN',  'Matt Riddles Bloodsport',    new Date(0,0,0,15,0,0),  new Date(0,0,0,18,0,0)],
      [ 'WrestleCon',  'Wildkat Sports', new Date(0,0,0,18,0,0), new Date(0,0,0,21,0,0)],
      [ 'WWN', 'Evolve',   new Date(0,0,0,20,00,0), new Date(0,0,0,23,0,0)],
      [ 'WrestleCon', 'WrestleCon Supershow',       new Date(0,0,0,21,30,0), new Date(0,0,0,23,30,0)],
      [ 'Knockout', 'Knockout in NOLA',       new Date(0,0,0,17,00,0), new Date(0,0,0,20,00,0)],
      [ 'ROH', 'RoH Supercard of Honor',       new Date(0,0,0,19,30,0), new Date(0,0,0,22,30,0)],
      [ 'WWN', 'Beyond Wrestling',        new Date(0,0,0,20,55,0), new Date(0,0,0,23,55,0)]]);
    var options = {
      timeline: { colorByRowLabel: true },
      tooltip: {isHtml: true},
      legend: 'none',
      backgroundColor: '#ffd'
    };
    chart.draw(dataTable, options);
  }
</script>
<div id="example5.1" style="height: 300px;"></div>
</DIV>

1 个答案:

答案 0 :(得分:1)

您可以添加自己的自定义工具提示,请参见时间轴参考中的Customizing tooltips

工具提示列将只是一个字符串,可以是简单值或html

请参阅以下工作片段,
在这里,DataView用于添加工具提示列。
这样可以根据数据表中的数据动态构建工具提示

还将竞技场添加到原始数据表中,以方便参考,
但已从数据视图中排除...

google.charts.load('current', {
  packages: ['timeline']
}).then(function () {
  var container = document.getElementById('example5.1');
  var chart = new google.visualization.Timeline(container);
  var dataTable = new google.visualization.DataTable();
  dataTable.addColumn({ type: 'string', id: 'Federation' });
  dataTable.addColumn({ type: 'string', id: 'Event' });
  dataTable.addColumn({ type: 'date', id: 'Start' });
  dataTable.addColumn({ type: 'date', id: 'End' });
  dataTable.addColumn({ type: 'string', id: 'Arena' });
  dataTable.addRows([
    ['WWE / NXT', 'AXXESS', new Date(0,0,0,18,0,0), new Date(0,0,0,22,0,0), 'Arena A'],
    ['WWN', 'Matt Riddles Bloodsport', new Date(0,0,0,15,0,0), new Date(0,0,0,18,0,0), 'Arena B'],
    ['WrestleCon', 'Wildkat Sports', new Date(0,0,0,18,0,0), new Date(0,0,0,21,0,0), 'Arena C'],
    ['WWN', 'Evolve', new Date(0,0,0,20,00,0), new Date(0,0,0,23,0,0), 'Arena D'],
    ['WrestleCon', 'WrestleCon Supershow', new Date(0,0,0,21,30,0), new Date(0,0,0,23,30,0), 'Arena E'],
    ['Knockout', 'Knockout in NOLA', new Date(0,0,0,17,00,0), new Date(0,0,0,20,00,0), 'Arena F'],
    ['ROH', 'RoH Supercard of Honor', new Date(0,0,0,19,30,0), new Date(0,0,0,22,30,0), 'Arena G'],
    ['WWN', 'Beyond Wrestling', new Date(0,0,0,20,55,0), new Date(0,0,0,23,55,0), 'Arena H']]);
  var options = {
    timeline: { colorByRowLabel: true },
    tooltip: {isHtml: true},
    legend: 'none',
    backgroundColor: '#ffd'
  };

  var formatTime = new google.visualization.DateFormat({
    pattern: 'HH:mm:ss a'
  });

  var view = new google.visualization.DataView(dataTable);
  view.setColumns([0, 1, {
    role: 'tooltip',
    type: 'string',
    calc: function (dt, row) {
      // build tooltip
      var dateBegin = dt.getValue(row, 2);
      var dateEnd = dt.getValue(row, 3);
      var oneHour = (60 * 60 * 1000);
      var duration = (dateEnd.getTime() - dateBegin.getTime()) / oneHour;

      var tooltip = '<div><div class="ggl-tooltip"><span>';
      tooltip += dt.getValue(row, 0) + ':</span>&nbsp;' + dt.getValue(row, 1) + '</div>';
      tooltip += '<div class="ggl-tooltip"><div>' + formatTime.formatValue(dateBegin) + ' - ';
      tooltip += formatTime.formatValue(dateEnd) + '</div>';
      tooltip += '<div><span>Duration: </span>' + duration.toFixed(0) + ' hours</div></div>';
      tooltip += '<div class="ggl-tooltip"><span>Arena: </span>' + dt.getValue(row, 4) + '</div></div>';

      return tooltip;
    },
    p: {html: true}
  }, 2, 3]);

  chart.draw(view.toDataTable(), options);  // <-- use data view to draw chart
});
.ggl-tooltip {
  background-color: #ffffff;
  border: 1px solid #e0e0e0;
  font-family: Arial, Helvetica;
  font-size: 14px;
  padding: 8px;
}

.ggl-tooltip span {
  font-weight: bold;
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="example5.1"></div>