react-native - 如何在 React 原生 svg 图表中添加背景颜色?
问题描述
我正在使用这个包来创建一个自定义的多个LineChart
.
我面临的唯一问题是我需要在每个 2 y 值之间添加背景颜色(我确定只有 0 到 4 的值)。所以我必须在“0-1”、“1-2”和“2-3”之间添加一种颜色。
这是当前的结果
这就是我需要实现的
这是我的代码
<YAxis
data={dataAxeY}
style={{ marginBottom: xAxisHeight }}
contentInset={verticalContentInset}
svg={{ fill: "rgba(134, 65, 244, 0.5)", fontSize: 16 }}
numberOfTicks={4}
/>
<View style={{ flex: 1, marginLeft: 10, borderWidth: 2 }}>
<LineChart
yAccessor={({ item }) => item.value}
xAccessor={({ item }) => item.date}
style={{ flex: 1 }}
data={data}
contentInset={verticalContentInset}
svg={{
strokeWidth: 3,
stroke: "url(#gradient)",
}}
svg={{ stroke: "rgb(134, 65, 244)" }}
curve={shape.curveNatural}
>
</LineChart>
<XAxis
data={dataAxeX}
formatLabel={(_, index) => dataAxeX[index].dateName}
contentInset={{ left: 10, right: 10 }}
svg={{ fill: "rgba(134, 65, 244, 0.5)" }}
/>
</View>
</View>
我试图添加一个Rect
我这样指定填充颜色的
const ColoredRect = (({ y }) => (
<Rect
x="0"
y="0"
width="100%"
height="33%"
fill="rgb(0,0,255)"
/>
))
它已添加,但我想要一个更通用的解决方案。
我希望你能帮我解决这个问题。
解决方案
Starting from the version 5.0, you can now create children SVG to the chart and place it below using belowChart= {true}
.
So for example you can do something like this:
<View style={{flex: 1}}>
<LineChart
style={{flex: 1}}
data={yAxisValues}
yMin={-10}
yMax={10}
contentInset={verticalContentInset}
svg={{
stroke: strokeColorLeft,
strokeWidth: '2',
}}>
<Svg belowChart={true}>
<Rect
x="0"
y="40%"
width="100%"
height="20%"
fill="rgb(144,238,144,0.3)"
/>
<Rect
x="0"
y="0"
width="100%"
height="20%"
fill="rgb(173,216,230,0.3)"
/>
</Svg>
<Grid />
</LineChart>
<XAxis
data={xAxisValues}
formatLabel={(value, index) => index}
contentInset={verticalContentInset}
svg={axesSvg}
/>
</View>
That gives you this result :
推荐阅读
- java - 如何使用泛型从 Java 调用 Scala curry 函数
- actions-on-google - 如何在 Google Actions 中创建意图实现?
- python - 如何通过大写分解字符串并生成更多行[Pandas]
- node.js - 如果字段可以为空,则验证期间的装饰器仍然有效
- javascript - 如何在按下按钮时将 ID 存储在数组中?
- c# - 在这个相同的字符串数据中搜索数据并相互合并
- asp.net-mvc - 如何在引导程序中创建两色横幅
- excel - 使用VBA在嵌套xml中的一行中打印数组
- node.js - Firestore 查询 endBefore 和 endAt 未按预期工作
- android - avdmanager 的设备列表与 Android Studio 的不同