chartsgoogle-visualizationreact-google-charts

How to color every bar in google timeline chart?


I have read some other answers here and accordingly added the role property but it did not work.

<Chart
  width={'500px'}
  height={'300px'}
  chartType="Timeline"
  loader={<div>Loading Chart</div>}
  data={[
    [
      { type: 'string', id: 'President' },
      { type: 'date', id: 'Start' },
      { type: 'date', id: 'End' },
      {type: 'string', role: 'style'}
    ],
    ['Washington', new Date(1789, 3, 30), new Date(1797, 2, 4), 'gold'],
    ['Adams', new Date(1797, 2, 4), new Date(1801, 2, 4), 'color: #ccc'],
    ['Jefferson', new Date(1801, 2, 4), new Date(1809, 2, 4), 'gold'],
  ]}
  options={{
    showRowNumber: true,
  }}
  rootProps={{ 'data-testid': '1' }}
/>

refer: https://react-google-charts.com/timeline-chart


Solution

  • on the timeline chart, the style role will only work when used as the third column.

    so you will also need to add the second column for bar label.
    but you can use null values for that column, if needed...

    <Chart
      width={'500px'}
      height={'300px'}
      chartType="Timeline"
      loader={<div>Loading Chart</div>}
      data={[
        [
          { type: 'string', id: 'President' },
          { type: 'string', id: 'Bar' },        // <-- add bar label here...
          { type: 'string', role: 'style' },    // <-- add style role here...
          { type: 'date', id: 'Start' },
          { type: 'date', id: 'End' }
        ],
        ['Washington', null, 'gold', new Date(1789, 3, 30), new Date(1797, 2, 4)],
        ['Adams', null, '#ccc', new Date(1797, 2, 4), new Date(1801, 2, 4)],
        ['Jefferson', null, 'gold', new Date(1801, 2, 4), new Date(1809, 2, 4)],
      ]}
      options={{
        showRowNumber: true,
      }}
      rootProps={{ 'data-testid': '1' }}
    />