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

How to specify the color scale within the data object in Victory Pie

我用 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>
    );
  }

重要的是要注意 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 },
    ...
    ...
  ]}

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

color scales: "grayscale", "qualitative", "heatmap", "warm", "cool", "red", "green", "blue". VictoryPie will assign a color to each slice by index, unless they are explicitly specified in the data object.

这说你可以,但我不知道怎么做。我尝试了以下...

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" },
    ...
    ...
]}

转化为...

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

但是,它们不会变红。我无法在网上找到示例。这可不可以?

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

如果您知道数据渲染顺序,那么您可以按相同顺序使用颜色,它会自动按顺序使用颜色

<VictoryPie
  colorScale={["black", "red", "green", "#4cc9ff", "navy" ]}
  data={sampleData}
/>

我手动添加了一个函数来随机生成试试如果这对你有帮助`var stats = this.state.statistics;

var stat = this.state.statistic;
        if (stats.length>0) {
            if(this.colors.length<stats.length){
                for(let i=0;i<stats.length;i++){
                    this.colors.push(getRandomColor());
                }
            }
        }
        const color = ['#9167f1', '#f26893', '#99cfef', '#99ef9a'];
        const colors = this.colors;`

function getRandomColor() {
    var letters = '0123456789abcdef';
    var color = '#';
    for (var i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}

我认为您希望在数据对象中使用 fill: "red" 而不是 colorScale: "red",因为 Victory 的 Slice 基元只是 svg <Path> 元素。

我知道这个问题已经"answered",但答案对我来说并不令人满意。自从我来到这里并沮丧地离开后,我决定 post 我的解决方案,希望其他人可以从中得到一些用处。

您可以使用 style 属性覆盖单个切片的颜色:

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

在这种情况下,您根本不应该使用 colorScale。我已经阅读了源代码,据我所知,这是在 VictoryPie.

中进行基于数据的颜色映射的唯一方法

您可以根据您的数据导出填充颜色,但您必须提供该逻辑。您接受的答案基于 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
    }
  }}
/>

对于当前版本

"victory": "^35.4.0",
"victory-native": "^35.3.1"

你可以做到:

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

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