2

I am using google timeline chart. Is that possible to make the first column clickable by making it as an html?

I try to make the column(User Name) to allow html, so that when I try add the row, I can make it as a link like this <a href="http://www.google.com">Google</a>

dataTable.addColumn({type: 'string', id: 'User Name', p: {html: true}});
dataTable.addColumn({type: 'string', id: 'User Id'});
dataTable.addColumn({type: 'string', role: 'tooltip', p: {html: true}});
dataTable.addColumn({type: 'string', id: 'style', role: 'style'});
dataTable.addColumn({type: 'date', id: 'Start'});
dataTable.addColumn({type: 'date', id: 'End'});

But this doesn't seem to work, it is rendered as a plain text.

chrisTina
  • 2,298
  • 9
  • 40
  • 74

1 Answers1

2

there aren't any click events provided by the chart,
but we can add regular events using javascript / jquery.

we just need to wait for the chart's 'ready' event before assigning the event listener.

google.visualization.events.addListener(chart, 'ready', function () {
  $(chart.getContainer()).find('text[text-anchor="end"]').on('click', onLabelClick);
});

here, we use the <text> element filter [text-anchor="end"]
to only assign the event to the row labels, and not the x-axis labels.

see following working snippet,
which uses a cell property to store the url,
to be retrieved when clicked.

google.charts.load('current', {
  packages: ['timeline']
}).then(function () {
  var container = document.getElementById('timeline');
  var chart = new google.visualization.Timeline(container);
  var dataTable = new google.visualization.DataTable();
  dataTable.addColumn({type: 'string', id: 'User Name'});
  dataTable.addColumn({type: 'string', id: 'User Id'});
  dataTable.addColumn({type: 'string', role: 'tooltip', p: {html: true}});
  dataTable.addColumn({type: 'string', id: 'style', role: 'style'});
  dataTable.addColumn({type: 'date', id: 'Start'});
  dataTable.addColumn({type: 'date', id: 'End'});
  dataTable.addRows([
    [{v: 'User A', p: {url: 'https://www.google.com'}}, '1', null, 'cyan', new Date(2020, 0, 1), new Date(2020, 0, 31)],
    [{v: 'User B', p: {url: 'https://www.yahoo.com'}}, '2', null, 'magenta', new Date(2020, 1, 1), new Date(2020, 1, 14)],
    [{v: 'User C', p: {url: 'https://www.bing.com'}}, '3', null, 'lime', new Date(2020, 2, 1), new Date(2020, 2, 10)]
  ]);

  google.visualization.events.addListener(chart, 'ready', function () {
    $(chart.getContainer()).find('text[text-anchor="end"]').on('click', onLabelClick);
  });

  chart.draw(dataTable);

  function onLabelClick(sender) {
    var label = $(sender.target).text();
    var rowIndex = dataTable.getFilteredRows([{
      column: 0,
      value: label
    }])[0];
    var url = dataTable.getProperty(rowIndex, 0, 'url');
    console.log(label, url);
    // window.open(url, '_blank');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="timeline" style="height: 180px;"></div>
WhiteHat
  • 59,912
  • 7
  • 51
  • 133