6

我正在使用 Victory 渲染数据集:

class App extends React.Component {
  render() {
    return (
      <div style={{ width: 600 }}>
        <VictoryChart domainPadding={30}>
          <VictoryAxis
            dependentAxis={true}
            style={{
              grid: { stroke: "grey" }
            }}
          />
          <VictoryAxis />
          <VictoryBar
            barWidth={20}
            style={{ data: { fill: "red" } }}
            data={[
              { x: new Date("2019-01-01"), y: 2 },
              { x: new Date("2019-02-01"), y: 3 },
              { x: new Date("2019-03-01"), y: 5 },
              { x: new Date("2019-04-01"), y: 4 },
              { x: new Date("2019-05-01"), y: 8 },
              { x: new Date("2019-06-01"), y: 2 },
              { x: new Date("2019-07-01"), y: 3 },
              { x: new Date("2019-08-01"), y: 5 },
              { x: new Date("2019-09-01"), y: 9 },
              { x: new Date("2019-10-01"), y: 3 },
              { x: new Date("2019-11-01"), y: 5 },
              { x: new Date("2019-12-01"), y: 6 }
            ]}
          />
        </VictoryChart>
      </div>
    );
  }
}

截屏

如何更改 x 轴以呈现每个月的刻度(“jan”、“feb”、“mar”等)?此外,我希望每个条的宽度为 40 像素(barWidth=40),但是当我这样做时,所有条都会合并 - 我也想控制条之间的边距/填充。这应该如何解决?

此沙箱中提供了一个示例: https ://codesandbox.io/s/victory-react-native-4t49q

4

2 回答 2

7

您可以将标签显示为 x 轴刻度。然后要控制边距/填充,您可以将容器的宽度设置得更宽一些以适应更粗的条。

import React from "react";
import { render } from "react-dom";
import { VictoryChart, VictoryBar, VictoryAxis, VictoryContainer, VictoryLabel } from "victory";

class App extends React.Component {
  render() {

    let month = new Array(12);
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";

    return (
      <div style={{ width: 600 }}>
        <VictoryChart domainPadding={30}
            width={900}
            containerComponent={<VictoryContainer responsive={false}/>}>
          <VictoryAxis
            dependentAxis={true}
            style={{
              grid: { stroke: "grey" }
            }}
          />
          <VictoryAxis 
            tickFormat={(x) => ``}
          />
          <VictoryBar
            barWidth={50}
            padding={{ left: 20, right: 60 }}
            style={{ data: { fill: "red" } }}
            data={[
              { x: new Date("2019-01-01"), y: 2 },
              { x: new Date("2019-02-01"), y: 3 },
              { x: new Date("2019-03-01"), y: 5 },
              { x: new Date("2019-04-01"), y: 4 },
              { x: new Date("2019-05-01"), y: 8 },
              { x: new Date("2019-06-01"), y: 2 },
              { x: new Date("2019-07-01"), y: 3 },
              { x: new Date("2019-08-01"), y: 5 },
              { x: new Date("2019-09-01"), y: 9 },
              { x: new Date("2019-10-01"), y: 3 },
              { x: new Date("2019-11-01"), y: 5 },
              { x: new Date("2019-12-01"), y: 6 }
            ]}
            labels={( datum ) => `${month[datum.x.getMonth()]}`}
            labelComponent={<VictoryLabel y={250} verticalAnchor={"start"}/>}
          />
        </VictoryChart>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

作为沙盒:链接

于 2019-09-16T11:03:17.533 回答
0

你可以试试。添加了需要更改的注释

class App extends React.Component {
  render() {
    return (
      <div>
      <VictoryChart
          domainPadding={30}
          width={700} // Width should be more as we are updating barWidth
        >
         <VictoryAxis
            dependentAxis={true}
            style={{
              grid: { stroke: "grey" }
            }}
          />
          <VictoryAxis  tickFormat={x => ''} /> // remove existing labels
          <VictoryBar
            barWidth={40}
            style={{ data: { fill: "red" } }}
            labels={( datum ) => {  return `${datum.x.toLocaleString('default', { month: 'short' })}`}} // get labels as month name
            labelComponent={
              <VictoryLabel y={270} 
                verticalAnchor={"start"} 
                angle={90}
              />
          }
            data={[
              { x: new Date("2019-01-01"), y: 2 },
              { x: new Date("2019-02-01"), y: 3 },
              { x: new Date("2019-03-01"), y: 5 },
              { x: new Date("2019-04-01"), y: 4 },
              { x: new Date("2019-05-01"), y: 8 },
              { x: new Date("2019-06-01"), y: 2 },
              { x: new Date("2019-07-01"), y: 3 },
              { x: new Date("2019-08-01"), y: 5 },
              { x: new Date("2019-09-01"), y: 9 },
              { x: new Date("2019-10-01"), y: 3 },
              { x: new Date("2019-11-01"), y: 5 },
              { x: new Date("2019-12-01"), y: 6 }
            ]}
          />
        </VictoryChart>
      </div>
    );
  }
}
于 2019-09-23T06:05:49.187 回答