首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在谷歌图表中按标签设置背景色?

有没有办法在谷歌图表中按标签设置背景色?
EN

Stack Overflow用户
提问于 2019-10-16 05:43:41
回答 3查看 365关注 0票数 1

我正在使用时间表来显示数据。

我想要标签背景,比如:Thomas Jefferson -> red,George Washington -> Green,John Jay -> -> ,John Adams -> Orange

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

代码语言:javascript
复制
options={{
    colors: ['red', 'orange', 'yellow', 'green'],

}}

我的图表代码如下:

代码语言:javascript
复制
<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' }}
/>

有人能帮忙吗?提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-10-16 06:35:13

这是我找到的解决办法。

代码语言:javascript
复制
<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' }}
/>
票数 0
EN

Stack Overflow用户

发布于 2019-10-16 06:26:24

您的颜色数组序列必须与提供的数据数组相同,第一个数据名将从colorArray获得第一个颜色。

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2019-10-16 06:21:03

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

代码语言:javascript
复制
options = {{ colors: ['#FF0000', '#FFA500', '#FFFF00', '#00FF00'] }};
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58406446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档