我有一个简单的三部分水平条形图,如下所示:
您可以在CodeSandbox上进行检查,或尝试使用以下代码:
function App() {
return (
<VictoryStack colorScale={['#D0021B', '#F5A623', '#00C16F']}>
<VictoryBar horizontal data={[{ x: 'progress', y: 50 }]} />
<VictoryBar horizontal data={[{ x: 'progress', y: 25 }]} />
<VictoryBar horizontal data={[{ x: 'progress', y: 25 }]} />
</VictoryStack>
)
}
我遇到了两个部分的问题:
在查看VictoryStack
文档和VictoryBar
图表时,我尝试了很多方法,但是我无法使其工作。任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
这是一个可行的解决方案,其中包括:
barWidth
栏高度的道具
添加了style
道具以模拟带有边框的边距
https://codesandbox.io/s/7y2ym084o6
import React from "react";
import ReactDOM from "react-dom";
import { VictoryStack, VictoryBar } from "victory";
function App() {
return (
<VictoryStack
style={{
data: {
stroke: "rgba(255,255,255,1)",
strokeWidth: 2
}
}}
colorScale={["#D0021B", "#F5A623", "#00C16F"]}
>
<VictoryBar barWidth={30} horizontal data={[{ x: "progress", y: 50 }]} />
<VictoryBar barWidth={30} horizontal data={[{ x: "progress", y: 25 }]} />
<VictoryBar barWidth={30} horizontal data={[{ x: "progress", y: 25 }]} />
</VictoryStack>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
答案 1 :(得分:1)
barWidth
来设置条形的宽度,如下所示:<VictoryBar horizontal barWidth={30} data={[{ x: 'progress', y: 50 }]} />
或者,还有另一种方法可以通过其style
:<VictoryBar horizontal style={ { data: { width:30 } } } data={[{ x: 'progress', y: 25 }]} />
y0
属性,如下所示:<VictoryBar horizontal data={[{ x: 'progress', y: 25, y0: 77 }]} />
在这里描绘整个解决方案的是您略微完善的Sandbox。
答案 2 :(得分:1)
您可以使用barWidth和/或style道具使用此处提出的其他解决方案。
但是,我认为硬编码barWidth并不是一个好主意,这有两个原因。首先,barWidth应该响应于您用来查看图表的设备的显示尺寸。其次,随着图表变得越来越复杂,您可能会有可变数量的VictoryBars。
我认为还有一个更优雅的解决方案:barRatio道具。如here所述,这将相对于条之间的可用空间动态设置barWidth。
如果您想填充整个空间并使条形图尽可能宽,则barRatio为1是有意义的,例如:
<VictoryBar data={data} key={outcome} barRatio={1} alignment="start" />