1

我有一个堆积条形图,其 x 轴标签将是日期,标签重叠我如何排列它们,使其不会在 x 轴的下一个标签上重叠。

我不知道如何安排他们改变标签的角度,谁能帮我解决这个问题。

当前图形图像

在此处输入图像描述

胜利原住民

反应原生SVG

const myDataset = [
  [
      { x: "20/10/2020", y: 18653 },
      { x: "21/10/2020", y: 20000 },
      { x: "23/10/2020", y: 97345 },
      { x: "24/10/2020", y: 25687 },
      { x: "25/10/2020", y: 89761 }
  ],
  [
      { x: "20/10/2020", y: 4566 },
      { x: "21/10/2020", y: 3888 },
      { x: "23/10/2020", y: 4975 },
      { x: "24/10/2020", y: 5965 },
      { x: "25/10/2020", y: 5768 }
  ],
];

class App extends React.Component {
  // This is an example of a function you might use to transform your data to make 100% data
  transformData(dataset) {
    const totals = dataset[0].map((data, i) => {
      return dataset.reduce((memo, curr) => {
        return memo + curr[i].y;
      }, 0);
    });
    return dataset.map((data) => {
      return data.map((datum, i) => {
        return { x: datum.x, y: (datum.y / totals[i]) * 100 };
      });
    });
  }

  render() {
    const dataset = this.transformData(myDataset);
    return (
      <div>
        <VictoryChart height={400} width={400}
          domain={{ x: [0,5], y: [0, 100000] }}
          domainPadding={{ x: 30, y: 20 }}
        >
           <VictoryLegend x={280} y={0}
              gutter={50}
              style={{title: {fontSize: 20 } }}
              data={[
                { name: "Tax", symbol: { fill: "blue" } },
                { name: "Amount", symbol: { fill: "black" } }
              ]}
            />
            <VictoryStack
              colorScale={["black", "blue"]}
            >
              {myDataset.map((data, i) => {
                return <VictoryBar barWidth={20}
             data={data} key={i} labelComponent={<VictoryLabel y={250} verticalAnchor={"start"}/>}/>;
              })}
            </VictoryStack>
            <VictoryAxis dependentAxis />
            <VictoryAxis 
            padding={{ left: 80, right: 60 }}
            axisLabelComponent={<VictoryLabel angle={20}/>}
            tickFormat={["20/oct/20","21/oct/20", "23/oct/20","24/oct/20","25/10/20"]}/>
        </VictoryChart>
      </div>
    );
  }
}

ReactDOM.render(<App/>, mountNode);

上面的代码可以复制粘贴在下面的链接中,并且可以编辑 https://formidable.com/open-source/victory/gallery/100-column-chart/

有什么办法可以像下面这样安排它们。

在此处输入图像描述

4

2 回答 2

2

我已经通过将VictoryLabel带有-45度角的a 传递给tickLabelComponent您的独立轴上的道具来使其工作:

<VictoryAxis tickLabelComponent={<VictoryLabel angle={-45} y={350} />} />

因此,与其将 传递给VictoryLabel上的axisLabelComponent道具VictoryAxis,不如将其传递给tickLabelComponent道具。

https://formidable.com/open-source/victory/docs/victory-axis#ticklabelcomponent


VictoryChart如果标签被切断,您可能还需要为组件添加一些填充:

padding={{top: 100, bottom: 90, left: 70, right: 80}}

https://formidable.com/open-source/victory/docs/victory-axis#padding

于 2020-11-22T23:09:58.557 回答
0

这是我使用的解决方案。我必须在 x 轴上显示时间。

在您的情况下,您可以从 VictoryAxis 取消刻度格式,并让 VictoryChart 通过添加属性scale={{x: 'time'}}来进行日期格式VictoryChart

然后添加fixLabelOverlap修复VictoryAxis重叠问题。

于 2021-04-05T19:54:10.970 回答