javascriptcsschartsgoogle-visualization

google charts timelines element border-radius


I have a problem figuring out how to set the border-radius on elements in a google charts Timeline. I have looked through all the options but there doesn't seem to be one for that. I have tried manually setting it but without any luck. Does anyone have a solution to this problem?

thx in advance


Solution

  • the chart elements can be modified when the chart's 'ready' event fires

    however, the chart will revert back to the original style on any interactivity

    a MutationObserver can be used to know when the chart has been modified
    in order to re-apply the custom style / border radius

    the chart is drawn using svg, to change the border radius on a rect element,
    set attributes 'rx' and 'ry'

    see the following working snippet...

    google.charts.load('current', {
      callback: drawChart,
      packages: ['timeline']
    });
    
    function drawChart() {
      var container = document.getElementById('timeline');
      var chart = new google.visualization.Timeline(container);
      var dataTable = new google.visualization.DataTable();
    
      dataTable.addColumn({ type: 'string', id: 'President' });
      dataTable.addColumn({ type: 'date', id: 'Start' });
      dataTable.addColumn({ type: 'date', id: 'End' });
      dataTable.addRows([
        [ 'Washington', new Date(1789, 3, 30), new Date(1797, 2, 4) ],
        [ 'Adams',      new Date(1797, 2, 4),  new Date(1801, 2, 4) ],
        [ 'Jefferson',  new Date(1801, 2, 4),  new Date(1809, 2, 4) ]
      ]);
    
      var observer = new MutationObserver(setBorderRadius);
      google.visualization.events.addListener(chart, 'ready', function () {
        setBorderRadius();
        observer.observe(container, {
          childList: true,
          subtree: true
        });
      });
    
      function setBorderRadius() {
        Array.prototype.forEach.call(container.getElementsByTagName('rect'), function (rect) {
          if (parseFloat(rect.getAttribute('x')) > 0) {
            rect.setAttribute('rx', 20);
            rect.setAttribute('ry', 20);
          }
        });
      }
    
      chart.draw(dataTable);
    }
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="timeline"></div>