如何在Victory Pie中指定数据对象内的色阶

Bol*_*boa 2 pie-chart reactjs victory-charts

我曾经VictoryChart实现过一个饼图,但效果很好...

render() {

    const data_distribution = this.state.pie_keys.map((d) => ({x:d, y:this.state.pie_data[d], label:d }));

    return (
      <div className="App">
        <div className="pie_wrapper">
          <VictoryPie
            labelComponent={<VictoryTooltip cornerRadius={0} />}   
            padAngle={0.5}
            innerRadius={100}
            width={400} height={400}
            style={{ 
              labels: { fontSize: 15, fill: "black"},
              data: {
                  fillOpacity: 0.9, stroke: "white", strokeWidth: 3
              }
            }}
            labelRadius={90}
            data = {data_distribution}
          />
        </div>
      </div>
    );
  }
Run Code Online (Sandbox Code Playgroud)

重要的是要注意,其data_distribution外观如下所示...

data={[
    { x: "Fac of Engineering & Appl Sci", y: 8.557902403495994 },
    { x: "Faculty of Arts and Science", y: 53.775188152464196 },
    { x: "Faculty of Education", y: 13.085700412721534 },
    ...
    ...
  ]}
Run Code Online (Sandbox Code Playgroud)

所以我想知道是否可以为数据对象内的每个饼图设置颜色。该文档指定...

色标:“灰度”,“定性”,“热图”,“温暖”,“凉爽”,“红色”,“绿色”,“蓝色”。VictoryPie将按索引为每个切片分配颜色,除非在数据对象中明确指定了颜色。

这说明您可以,但我不知道如何。我尝试了以下...

data={[
    { x: "Fac of Engineering & Appl Sci", y: 8.557902403495994, colorScale:"red" },
    { x: "Faculty of Arts and Science", y: 53.775188152464196, colorScale:"red" },
    { x: "Faculty of Education", y: 13.085700412721534, colorScale:"red" },
    ...
    ...
]}
Run Code Online (Sandbox Code Playgroud)

转换为...

const data_distribution = this.state.pie_keys.map((d) => ({x:d, y:this.state.pie_data[d], label:d, colorScale:"red"}));
Run Code Online (Sandbox Code Playgroud)

但是,它们不会变红。我找不到在线示例。这可能做吗?

我知道我可以定义,colorScale: {[...]}但这不是我要的。我希望能够从数据对象中设置每个馅饼。

Jon*_*ieb 5

我知道这个问题已经“回答”了,但是答案对我来说并不令人满意。自从我来到这里并感到沮丧之后,我决定发布我的解决方案,希望其他人可以从中受益。

您可以使用styleprop 覆盖各个切片的颜色:

<VictoryPie
  data={[
    { x: "Cats", y: 35 },
    { x: "Dogs", y: 40 },
    { x: "Birds", y: 55 }
  ]}
  style={{
    data: {
      fill: ({ y }) =>
        y > 49 ? 'green'
        : y > 39 ? 'yellow'
        : 'tomato'
    }
  }}
/>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您根本不应使用colorScale。我已经阅读了源代码,据我所知,这是在中进行基于数据的颜色映射的唯一方法VictoryPie


小智 5

您可以根据您的数据推导出填充颜色,但您必须提供该逻辑。您接受的答案根据 y 值应用颜色,但是您也可以在数据本身中指定颜色,只要您将样式设置为这样做:

<VictoryPie
  data={[
    { x: "Cats", y: 35, yourAttribute: "#0000FF" },
    { x: "Dogs", y: 40, yourAttribute: "#00FF00" },
    { x: "Birds", y: 55, yourAttribute:"#FF0000" }
  ]}
  style={{
    data: {
      fill: (d) => d.yourAttribute
    }
  }}
/>
Run Code Online (Sandbox Code Playgroud)

  • 在最新版本的 Victory 上,回调应该是 `(d) =&gt; d.slice.data.yourAttribute` (2认同)

por*_*ors 5

对于当前版本

"victory": "^35.4.0",
"victory-native": "^35.3.1"
Run Code Online (Sandbox Code Playgroud)

你可以做:

 <VictoryPie
        data={[
            { x: "Cats", y: 35, fill: "gold" },
            { x: "Dogs", y: 40, fill: "tomato" },
            { x: "Birds", y: 55, fill: "navy" }
        ]}
        style={{
            data: {
                fill: ({datum}) => datum.fill
            }
        }}
   />
Run Code Online (Sandbox Code Playgroud)

请参阅https://formidable.com/open-source/victory/docs/common-props/#data