[英]how to display vertical grid with time series data using react-native-svg-charts
The problem?问题? This is probably a lack of understanding from my side rather than an issue with "react-native-svg-charts", but perhaps others are having a similar issue and it'll be worth having the solution here.
这可能是我缺乏理解,而不是“react-native-svg-charts”的问题,但也许其他人也有类似的问题,值得在这里找到解决方案。 I can't get vertical gridlines to display correctly when using time series data, the only vertical gridline shown is the Y-Axis of the chart.
使用时间序列数据时,我无法正确显示垂直网格线,显示的唯一垂直网格线是图表的 Y 轴。
There isn't much documentation on how the custom grid works, so it is hard to know where I'm going wrong with this.关于自定义网格如何工作的文档并不多,因此很难知道我哪里出错了。 If I use the index (rather than value) of the data.map function in the custom grid I don't even get the vertical line in the Y-Axis.
如果我在自定义网格中使用 data.map 函数的索引(而不是值),我什至没有得到 Y 轴中的垂直线。 What am I doing wrong?
我究竟做错了什么?
React Native version: "dependencies": { React Native 版本:“依赖项”:{
} }
``` ``
javascript
import React from "react";
import { Dimensions, StyleSheet, View } from "react-native";
import { LineChart, YAxis, XAxis } from "react-native-svg-charts";
import { G, Line } from "react-native-svg";
import * as shape from "d3-shape";
import * as scale from "d3-scale";
import moment from "moment";
const data = [
{
value: 50,
date: new Date(2018, 0, 1, 2)
},
{
value: 10,
date: new Date(2018, 0, 1, 9)
},
{
value: 150,
date: new Date(2018, 0, 1, 10)
},
{
value: 10,
date: new Date(2018, 0, 1, 13)
},
{
value: 100,
date: new Date(2018, 0, 1, 21)
},
{
value: 20,
date: new Date(2018, 0, 2, 0)
},
{
value: 115,
date: new Date(2018, 0, 2, 8)
},
{
value: 75,
date: new Date(2018, 0, 2, 10)
},
{
value: 25,
date: new Date(2018, 0, 2, 16)
},
{
value: 125,
date: new Date(2018, 0, 2, 17)
},
{
value: 66,
date: new Date(2018, 0, 2, 19)
},
{
value: 85,
date: new Date(2018, 0, 2, 23)
}
];
export default class ChartTest extends React.Component {
renderChart() {
const xAxisHeight = 30;
const verticalContentInset = { top: 10, bottom: 10 };
const CustomGrid = ({ x, y, data, ticks }) => (
<G>
{// Horizontal grid
ticks.map(tick => (
<Line key={tick} x1={"0%"} x2={"100%"} y1={y(tick)} y2={y(tick)} stroke={"rgba(0,0,0,0.2)"} />
))}
{// Vertical grid
data.map((value, index) => (
<Line key={index} y1={"0%"} y2={"100%"} x1={x(value)} x2={x(value)} stroke={"red"} />
))}
</G>
);
return (
<View style={{ height: 250, padding: 20, width: "90%", flexDirection: "row" }}>
<YAxis
style={{ marginBottom: xAxisHeight }}
data={data}
contentInset={verticalContentInset}
yAccessor={({ item }) => item.value}
xAccessor={({ item }) => item.date}
svg={{
fill: "#FFFFFF"
}}
numberOfTicks={5}
formatLabel={value => `${value} ºC`}
/>
<View style={{ flex: 1, marginLeft: 10 }}>
<LineChart
style={{ flex: 1 }}
data={data}
contentInset={verticalContentInset}
yAccessor={({ item }) => item.value}
xAccessor={({ item }) => item.date}
svg={{
stroke: "#81B0C0"
}}
scale={scale.scaleTime}
numberOfTicks={10}
>
<CustomGrid belowChart={true} />
</LineChart>
<XAxis
data={data}
svg={{
fill: "#FFFFFF",
fontSize: 8,
fontWeight: "bold",
rotation: 20,
originY: 30,
y: 5
}}
xAccessor={({ item }) => item.date}
scale={scale.scaleTime}
numberOfTicks={10}
style={{ marginHorizontal: -10, height: xAxisHeight }}
contentInset={verticalContentInset}
formatLabel={value => moment(value).format("HH:mm")}
/>
</View>
</View>
);
}
render() {
return <View style={styles.container}>{this.renderChart()}</View>;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#343334",
alignItems: "center",
justifyContent: "center"
}
});
This is what I get:这就是我得到的:
I've found a way.我找到了办法。 Although I do believe is not the optimal way.
虽然我相信这不是最佳方式。
I have to say I couldn't figure out what they do, but they helped me render the vertical lines:我不得不说我无法弄清楚他们做了什么,但他们帮助我渲染了垂直线:
gridMin={0}
This should work:这应该有效:
class BarChartHorizontalWithLabels extends React.PureComponent {
render() {
const data = [ 50, 10, 40, 95, 85 ]
const CUT_OFF = 50
const Labels = ({ x, y, bandwidth, data }) => (
data.map((value, index) => (
<Text
key={ index }
x={ value > CUT_OFF ? x(0) + 10 : x(value) + 10 }
y={ y(index) + (bandwidth / 2) }
fontSize={ 14 }
fill={ value > CUT_OFF ? 'white' : 'black' }
alignmentBaseline={ 'middle' }
>
{value}
</Text>
))
)
return (
<View style={{ flexDirection: 'row', height: 200, paddingVertical: 16 }}>
<BarChart
style={{ flex: 1, marginLeft: 8 }}
data={data}
horizontal={true}
svg={{ fill: 'rgba(134, 65, 244, 0.8)' }}
contentInset={{ top: 10, bottom: 10 }}
spacing={0.2}
gridMin={0}
>
<Grid direction={Grid.Direction.VERTICAL}/>
<Labels/>
</BarChart>
</View>
)
}
}
export default BarChartHorizontalWithLabels
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.