我正在尝试在水平滚动中制作一个折线图,其中心值位于屏幕中间,当x点碰到折线时会更新。问题在于,计算出的增量不准确,因此您在图表中滚动的次数越多,中心点最终就会偏离直线。
我在水平ScrollView中使用react-native-svg-graphs和d3形状。
我已经使用屏幕的宽度(一次要在屏幕上获得多少个数据点)和数据集的.length来计算所有值
用于计算每个增量之间的距离
const { dataPointsOnScreen } = this.state;
const width = (DATA.length / dataPointsOnScreen) * Dimensions.get('window').width
this.setState({ incrementWidth: width / DATA.length })
用于计算何时更新值
if (event.nativeEvent.contentOffset.x > incrementWidth * (pinIndex + 1)) {
this.setState({ pinIndex: this.state.pinIndex + 1 }) }
else if (event.nativeEvent.contentOffset.x < incrementWidth * (pinIndex)) {
this.setState({ pinIndex: this.state.pinIndex -1 })
}
此处包含代码库-https://github.com/eikhunter/horizontal-line-chart-react-native
该行在到达每个数据点时进行更新,或者有一种更好的方法来计算数字何时应更新或增量计算错误。我的假设是因为每个增量都不是一个整数javascript正在用数字做一些有趣的事情。我问过图书馆的作者,是否也可以指定增量距离。