-
Notifications
You must be signed in to change notification settings - Fork 1
/
SvgChartDemo2.js
133 lines (119 loc) · 3.52 KB
/
SvgChartDemo2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React from 'react';
import {
ScrollView,
StyleSheet,
Dimensions,
SafeAreaView,
View,
} from 'react-native';
import {LineChart} from 'react-native-svg-charts';
import GraphStatBox from './GraphStatBox';
import LinearGradient from 'react-native-linear-gradient';
import {DATA} from './Data';
import * as shape from 'd3-shape';
class SvgChartDemo2 extends React.Component {
_scrollRef;
constructor() {
super();
this.state = {
data: [],
dataPointsOnScreen: 6,
pinIndex: 0,
pinPosition: 0,
incrementWidth: 0,
};
}
componentWillMount() {
const {dataPointsOnScreen} = this.state;
const width =
(DATA.length / dataPointsOnScreen) * Dimensions.get('window').width;
this.setState({incrementWidth: width / DATA.length});
}
_onScrollEnd = (event) => {
const {pinIndex, incrementWidth} = this.state;
let arr = [];
const currentPos = event.nativeEvent.contentOffset.x;
const previousIndex = pinIndex;
const nextIndex = pinIndex + 1;
arr.push(previousIndex * incrementWidth);
arr.push(nextIndex * incrementWidth);
const closest = arr.reduce((prev, curr) => {
return Math.abs(curr - currentPos) < Math.abs(prev - currentPos)
? curr
: prev;
});
console.log(this._scrollRef);
this._scrollRef && this._scrollRef.scrollTo({x: closest, duration: 300});
};
_updateDataPointOnGraph = (event) => {
const {pinIndex, incrementWidth} = this.state;
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});
}
};
render() {
const {pinIndex, dataPointsOnScreen} = this.state;
const width =
(DATA.length / dataPointsOnScreen) * Dimensions.get('window').width;
return (
<SafeAreaView style={styles.container}>
<LinearGradient
style={styles.gradient}
colors={['#a9f3ff', '#ffb2f6', '#ffd0bd']}
start={{x: 0, y: 1}}
end={{x: 1, y: 0}}
/>
<View style={styles.body}>
<View style={styles.stats}>
<GraphStatBox
total={DATA[pinIndex + 5].value}
date={DATA[pinIndex + 5].date}
/>
</View>
<ScrollView
bounce={false}
ref={(ref) => (this._scrollRef = ref)}
style={styles.scrollView}
scrollEventThrottle={16}
horizontal={true}
onScroll={this._updateDataPointOnGraph}
onScrollEndDrag={this._onScrollEnd}
showsHorizontalScrollIndicator={false}>
<LineChart
curve={shape.curveCatmullRomOpen}
style={{height: 140, width: width}}
data={DATA}
yAccessor={({item}) => item.value}
svg={{stroke: '#000', strokeWidth: 2}}
contentInset={{
top: 30,
left: 0,
right: 0,
bottom: 10,
}}></LineChart>
</ScrollView>
</View>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingVertical: 40,
},
stats: {
alignItems: 'center',
marginBottom: 40,
},
gradient: {
...StyleSheet.absoluteFillObject,
opacity: 0.5,
},
scrollView: {
marginLeft: -Dimensions.get('window').width / 2 + 44,
},
});
export default SvgChartDemo2;