【问题标题】:Is there any way to set background-color by label in google chart?有没有办法在谷歌图表中按标签设置背景颜色?
【发布时间】:2019-10-16 14:38:48
【问题描述】:

我正在使用 react google 时间线图来显示数据。

我想要标签背景,例如: Thomas Jefferson -> 红色,George Washington -> 绿色,John Jay -> 黄色,John Adams -> 橙色

我正在使用图表的颜色选项,例如:

options={{
    colors: ['red', 'orange', 'yellow', 'green'],

}}

我的图表代码如下:

<Chart
width={'100%'}
height={'400px'}
chartType="Timeline"
loader={<div>Loading Chart</div>}
data={[
    [
        { type: 'string', id: 'Position' },
        { type: 'string', id: 'Name' },
        { type: 'date', id: 'Start' },
        { type: 'date', id: 'End' },
    ],
    [
        'President',
        'George Washington',
        new Date(2019, 2, 4),
        new Date(2019, 3, 30),
    ],
    [
        'President',
        'Thomas Jefferson',
        new Date(2019, 2, 4),
        new Date(2019, 4, 4),
    ],
    [
        'Vice President',
        'John Adams',
        new Date(2019, 3, 21),
        new Date(2019, 6, 4),
    ],
    [
        'Secretary of State',
        'John Jay',
        new Date(2019, 5, 4),
        new Date(2019, 8, 20),
    ],
    [
        'President',
        'John Adams',
        new Date(2019, 2, 25),
        new Date(2019, 8, 22),
    ],
    [
        'Vice President',
        'George Washington',
        new Date(2019, 7, 22),
        new Date(2019, 11, 31),
    ],
]}
options={{
    colors: ['red', 'orange', 'yellow', 'green'],

}}
rootProps={{ 'data-testid': '7' }}
/>

有人可以帮忙吗?提前致谢。

【问题讨论】:

  • 使用选项 = {{ 颜色: ['#FF0000', '#FFA500', '#FFFF00', '#00FF00'], }};
  • 感谢您的回复。试过这个但没有用。顺便说一句,我找到了解决方案。

标签: reactjs google-visualization


【解决方案1】:

这是我找到的解决方案。

<Chart
width={'100%'}
height={'400px'}
chartType="Timeline"
loader={<div>Loading Chart</div>}
data={[
    [
        { type: 'string', id: 'Position' },
        { type: 'string', id: 'Name' },
        { type: 'string', id: 'style', role: 'style' },
        { type: 'date', id: 'Start' },
        { type: 'date', id: 'End' },
    ],
    [
        'President',
        'George Washington',
        'green'
        new Date(2019, 2, 4),
        new Date(2019, 3, 30),
    ],
    [
        'President',
        'Thomas Jefferson',
        'red',
        new Date(2019, 2, 4),
        new Date(2019, 4, 4),
    ],
    [
        'Vice President',
        'John Adams',
        'orange',
        new Date(2019, 3, 21),
        new Date(2019, 6, 4),
    ],
    [
        'Secretary of State',
        'John Jay',
        'yellow',
        new Date(2019, 5, 4),
        new Date(2019, 8, 20),
    ],
    [
        'President',
        'John Adams',
        'orange',
        new Date(2019, 2, 25),
        new Date(2019, 8, 22),
    ],
    [
        'Vice President',
        'George Washington',
        'green',
        new Date(2019, 7, 22),
        new Date(2019, 11, 31),
    ],
]}
rootProps={{ 'data-testid': '7' }}
/>

【讨论】:

  • 是的!你也可以这样做,也可以像我更新的答案一样调用一个函数来返回颜色数组。
【解决方案2】:

您的颜色数组顺序必须与您提供的数据数组相同,第一个数据名称将采用 colorArray 中的第一个颜色。

import React from "react";
import ReactDOM from "react-dom";
import Chart from "react-google-charts";
class App extends React.Component {
 getColors() {
  let colorArray = [];
   rows.forEach((item, index) => {
    // logic to push color name in colorArray as as data index
   })
   return colorArray;
 }
 render() {
  return (
    <div className="App">
    <Chart
      width={"100%"}
      height={"400px"}
      chartType="Timeline"
      loader={<div>Loading Chart</div>}
      data={[
        [
          { type: "string", id: "Position" },
          { type: "string", id: "Name" },
          { type: "date", id: "Start" },
          { type: "date", id: "End" }
        ],
        [
          "President",
          "Thomas Jefferson",
          new Date(2019, 2, 4),
          new Date(2019, 5, 4)
        ],
        [
          "Vice President",
          "John Adams",
          new Date(2019, 3, 21),
          new Date(2019, 6, 4)
        ],
        [
          "Secretary of State",
          "John Jay",
          new Date(2019, 5, 4),
          new Date(2019, 8, 20)
        ],
        [
          "Vice President",
          "George Washington",
          new Date(2019, 7, 22),
          new Date(2019, 11, 31)
        ]
      ]}
      options={{
        colors: this.getColors()
      }}
      rootProps={{ "data-testid": "7" }}
    />
  </div>
  );
 }
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

【讨论】:

  • 那你想要什么?
  • 我不希望它与标签相关,请参阅我在这里发布的答案。
  • 检查我更新的答案,而不是静态颜色,您可以根据您的数据安排颜色顺序。
  • 你能帮我改一下其他样式吗,我想去掉表格边框和背景
  • 请访问react-google-charts.com/timeline-chart 进行任何更改。如果您觉得我的答案合适,请投票并标记为答案。
【解决方案3】:

使用颜色代码代替颜色名称。 https://react-google-charts.com/timeline-chart#setting-individual-bar-colors

options = {{ colors: ['#FF0000', '#FFA500', '#FFFF00', '#00FF00'] }};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 2011-08-14
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2012-12-15
    相关资源
    最近更新 更多