Bagaimana cara mewarnai setiap batang di grafik timeline Google?

Saya telah membaca beberapa jawaban lain di sini dan menambahkan properti role tetapi tidak berhasil.

<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' }}
/>

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


person Rounak Jain    schedule 26.08.2020    source sumber


Jawaban (1)


pada bagan garis waktu, peran gaya hanya akan berfungsi bila digunakan sebagai kolom ketiga.

jadi Anda juga perlu menambahkan kolom kedua untuk label batang.
tetapi Anda dapat menggunakan nilai null untuk kolom itu, jika diperlukan...

<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' }}
/>
person WhiteHat    schedule 26.08.2020