Skip to content
This repository was archived by the owner on Sep 17, 2021. It is now read-only.

Commit bbc881d

Browse files
Guangyao Miaomarzolfb
authored andcommitted
feature: add interaction on line chart. (#221)
1 parent f77d86d commit bbc881d

File tree

5 files changed

+454
-16
lines changed

5 files changed

+454
-16
lines changed

example/src/App.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import StockLineChartBasic from './stockline/StockLineChartBasic'
3131
import StockLineChartStaticTickLabels from './stockline/StockLineChartStaticTickLabels'
3232
import StockLineChartDynamicTickLabels from './stockline/StockLineChartDynamicTickLabels'
3333
import StockLineChartDynamicLineRendering from './stockline/StockLineChartDynamicLineRendering'
34+
import StockLineChartGesture from './stockline/StockLineChartGesture'
3435

3536
import SmoothLineChartBasic from './smoothline/SmoothLineChartBasic'
3637
import SmoothLineChartRegions from './smoothline/SmoothLineChartRegions'
@@ -86,6 +87,10 @@ class HomeScreen extends React.Component {
8687
onPress={() => navigate('StockLineChartDynamicLineRendering')}
8788
title="StockLine - Dynamic Line Rendering"
8889
/>
90+
<Button
91+
onPress={() => navigate('StockLineChartGesture')}
92+
title="StockLine - Gesture"
93+
/>
8994
<Button
9095
onPress={() => navigate('SmoothLineChartBasic')}
9196
title="SmoothLine - Basic"
@@ -123,6 +128,7 @@ const App = StackNavigator({
123128
StockLineChartStaticTickLabels: { screen: StockLineChartStaticTickLabels },
124129
StockLineChartDynamicTickLabels: { screen: StockLineChartDynamicTickLabels },
125130
StockLineChartDynamicLineRendering: { screen: StockLineChartDynamicLineRendering },
131+
StockLineChartGesture: { screen: StockLineChartGesture },
126132
SmoothLineChartBasic: { screen: SmoothLineChartBasic },
127133
SmoothLineChartRegions: { screen: SmoothLineChartRegions },
128134
SmoothLineChartRegionsExtended: { screen: SmoothLineChartRegionsExtended },
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
/*
2+
Copyright 2016 Capital One Services, LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and limitations under the License.
14+
15+
SPDX-Copyright: Copyright (c) Capital One Services, LLC
16+
SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
'use strict';
20+
21+
import React, { Component } from 'react';
22+
import { View, StyleSheet, Text } from 'react-native';
23+
24+
import { StockLine } from 'react-native-pathjs-charts';
25+
26+
const data = [
27+
[{
28+
"x": 0,
29+
"y": 47782
30+
}, {
31+
"x": 1,
32+
"y": 48497
33+
}, {
34+
"x": 2,
35+
"y": 77128
36+
}, {
37+
"x": 3,
38+
"y": 73413
39+
}, {
40+
"x": 4,
41+
"y": 58257
42+
}, {
43+
"x": 5,
44+
"y": 40579
45+
}, {
46+
"x": 6,
47+
"y": 72893
48+
}, {
49+
"x": 7,
50+
"y": 60663
51+
}, {
52+
"x": 8,
53+
"y": 15715
54+
}, {
55+
"x": 9,
56+
"y": 40305
57+
}, {
58+
"x": 10,
59+
"y": 68592
60+
}, {
61+
"x": 11,
62+
"y": 95664
63+
}, {
64+
"x": 12,
65+
"y": 17908
66+
}, {
67+
"x": 13,
68+
"y": 22838
69+
}, {
70+
"x": 14,
71+
"y": 32153
72+
}, {
73+
"x": 15,
74+
"y": 56594
75+
}, {
76+
"x": 16,
77+
"y": 76348
78+
}, {
79+
"x": 17,
80+
"y": 46222
81+
}, {
82+
"x": 18,
83+
"y": 59304
84+
}],
85+
[{
86+
"x": 0,
87+
"y": 132189
88+
}, {
89+
"x": 1,
90+
"y": 61705
91+
}, {
92+
"x": 2,
93+
"y": 154976
94+
}, {
95+
"x": 3,
96+
"y": 81304
97+
}, {
98+
"x": 4,
99+
"y": 172572
100+
}, {
101+
"x": 5,
102+
"y": 140656
103+
}, {
104+
"x": 6,
105+
"y": 148606
106+
}, {
107+
"x": 7,
108+
"y": 53010
109+
}, {
110+
"x": 8,
111+
"y": 110783
112+
}, {
113+
"x": 9,
114+
"y": 196446
115+
}, {
116+
"x": 10,
117+
"y": 117057
118+
}, {
119+
"x": 11,
120+
"y": 186765
121+
}, {
122+
"x": 12,
123+
"y": 174908
124+
}, {
125+
"x": 13,
126+
"y": 75247
127+
}, {
128+
"x": 14,
129+
"y": 192894
130+
}, {
131+
"x": 15,
132+
"y": 150356
133+
}, {
134+
"x": 16,
135+
"y": 180360
136+
}, {
137+
"x": 17,
138+
"y": 175697
139+
}, {
140+
"x": 18,
141+
"y": 114967
142+
}],
143+
[{
144+
"x": 0,
145+
"y": 125797
146+
}, {
147+
"x": 1,
148+
"y": 256656
149+
}, {
150+
"x": 2,
151+
"y": 222260
152+
}, {
153+
"x": 3,
154+
"y": 265642
155+
}, {
156+
"x": 4,
157+
"y": 263902
158+
}, {
159+
"x": 5,
160+
"y": 113453
161+
}, {
162+
"x": 6,
163+
"y": 289461
164+
}, {
165+
"x": 7,
166+
"y": 293850
167+
}, {
168+
"x": 8,
169+
"y": 206079
170+
}, {
171+
"x": 9,
172+
"y": 240859
173+
}, {
174+
"x": 10,
175+
"y": 152776
176+
}, {
177+
"x": 11,
178+
"y": 297282
179+
}, {
180+
"x": 12,
181+
"y": 175177
182+
}, {
183+
"x": 13,
184+
"y": 169233
185+
}, {
186+
"x": 14,
187+
"y": 237827
188+
}, {
189+
"x": 15,
190+
"y": 242429
191+
}, {
192+
"x": 16,
193+
"y": 218230
194+
}, {
195+
"x": 17,
196+
"y": 161511
197+
}, {
198+
"x": 18,
199+
"y": 153227
200+
}]
201+
];
202+
203+
const styles = StyleSheet.create({
204+
container: {
205+
flex: 1,
206+
justifyContent: 'center',
207+
alignItems: 'center',
208+
backgroundColor: '#f7f7f7',
209+
},
210+
});
211+
212+
class StockLineChartBasic extends Component {
213+
214+
constructor(props) {
215+
super(props);
216+
this.state = {};
217+
}
218+
219+
componentWillMount () {
220+
this._panHandlerStart = this._panHandlerStart.bind(this);
221+
this._panHandlerMove = this._panHandlerStart.bind(this);
222+
this._panHandlerEnd = this._panHandlerEnd.bind(this);
223+
}
224+
225+
static navigationOptions = ({ navigation }) => ({
226+
title: `StockLine - Gesture`,
227+
});
228+
229+
_panHandlerStart(cursorPositionX) {
230+
this.setState({
231+
selectedDataPointPosition: String(Math.floor(cursorPositionX * (data[0].length - 1)))
232+
});
233+
}
234+
_panHandlerMove(cursorPositionX) {
235+
this.setState({
236+
selectedDataPointPosition: String(Math.floor(cursorPositionX * (data[0].length - 1)))
237+
});
238+
}
239+
_panHandlerEnd(cursorPositionX) {
240+
this.setState({
241+
selectedDataPointPosition: ""
242+
});
243+
}
244+
245+
render() {
246+
247+
const options = {
248+
width: 250,
249+
height: 250,
250+
color: '#2980B9',
251+
margin: {
252+
top: 10,
253+
left: 35,
254+
bottom: 30,
255+
right: 10
256+
},
257+
animate: {
258+
type: 'delayed',
259+
duration: 200
260+
},
261+
axisX: {
262+
showAxis: true,
263+
showLines: true,
264+
showLabels: true,
265+
showTicks: true,
266+
zeroAxis: false,
267+
orient: 'bottom',
268+
tickValues: [],
269+
label: {
270+
fontFamily: 'Arial',
271+
fontSize: 8,
272+
fontWeight: true,
273+
fill: '#34495E'
274+
}
275+
},
276+
axisY: {
277+
showAxis: true,
278+
showLines: true,
279+
showLabels: true,
280+
showTicks: true,
281+
zeroAxis: false,
282+
orient: 'left',
283+
tickValues: [],
284+
label: {
285+
fontFamily: 'Arial',
286+
fontSize: 8,
287+
fontWeight: true,
288+
fill: '#34495E'
289+
}
290+
},
291+
interaction: true,
292+
cursorLine: {
293+
stroke: 'white',
294+
strokeWidth: 2
295+
}
296+
};
297+
298+
return (
299+
<View style={styles.container}>
300+
<Text> Data point index: { this.state.selectedDataPointPosition }</Text>
301+
<StockLine
302+
panHandlerStart={this._panHandlerStart}
303+
panHandlerMove={this._panHandlerMove}
304+
panHandlerEnd={this._panHandlerEnd}
305+
data={data}
306+
options={options}
307+
xKey='x'
308+
yKey='y' />
309+
</View>
310+
);
311+
}
312+
}
313+
314+
export default StockLineChartBasic;

0 commit comments

Comments
 (0)