-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
145 lines (129 loc) · 3.32 KB
/
App.tsx
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
134
135
136
137
138
139
140
141
142
143
144
145
import React from 'react';
import {
Canvas,
Line,
Path,
runTiming,
Skia,
SkPath,
useComputedValue,
useValue,
vec,
} from '@shopify/react-native-skia';
import {animatedData, DataPoint, originalData} from './Data';
import {curveBasis, line, scaleLinear, scaleTime} from 'd3';
import {Easing, View, Pressable, Text, StyleSheet} from 'react-native';
interface GraphData {
min: number;
max: number;
curve: SkPath;
}
const App = () => {
const transition = useValue(1);
const state = useValue({
current: 0,
next: 1,
});
const GRAPH_HEIGHT = 400;
const GRAPH_WIDTH = 360;
const makeGraph = (data: DataPoint[]): GraphData => {
const max = Math.max(...data.map(val => val.value));
const min = Math.min(...data.map(val => val.value));
const y = scaleLinear().domain([0, max]).range([GRAPH_HEIGHT, 35]);
const x = scaleTime()
.domain([new Date(2000, 1, 1), new Date(2000, 1, 15)])
.range([10, GRAPH_WIDTH - 10]);
const curvedLine = line<DataPoint>()
.x(d => x(new Date(d.date)))
.y(d => y(d.value))
.curve(curveBasis)(data);
const skPath = Skia.Path.MakeFromSVGString(curvedLine!);
return {
max,
min,
curve: skPath!,
};
};
const transitionStart = (end: number) => {
state.current = {
current: end,
next: state.current.current,
};
transition.current = 0;
runTiming(transition, 1, {
duration: 750,
easing: Easing.inOut(Easing.cubic),
});
};
const graphData = [makeGraph(originalData), makeGraph(animatedData)];
const path = useComputedValue(() => {
const start = graphData[state.current.current].curve;
const end = graphData[state.current.next].curve;
const result = start.interpolate(end, transition.current);
return result?.toSVGString() ?? '0';
}, [state, transition]);
return (
<View style={styles.container}>
<Canvas
style={{
width: GRAPH_WIDTH,
height: GRAPH_HEIGHT,
}}>
<Line
p1={vec(10, 130)}
p2={vec(400, 130)}
color="lightgrey"
style="stroke"
strokeWidth={1}
/>
<Line
p1={vec(10, 250)}
p2={vec(400, 250)}
color="lightgrey"
style="stroke"
strokeWidth={1}
/>
<Line
p1={vec(10, 370)}
p2={vec(400, 370)}
color="lightgrey"
style="stroke"
strokeWidth={1}
/>
<Path style="stroke" path={path} strokeWidth={4} color="#6231ff" />
</Canvas>
<View style={styles.buttonContainer}>
<Pressable
onPress={() => transitionStart(0)}
style={styles.buttonStyle}>
<Text style={styles.textStyle}>Graph 1</Text>
</Pressable>
<Pressable
onPress={() => transitionStart(1)}
style={styles.buttonStyle}>
<Text style={styles.textStyle}>Graph 2</Text>
</Pressable>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
alignItems: 'center',
},
buttonContainer: {
flexDirection: 'row',
},
buttonStyle: {
marginRight: 20,
backgroundColor: '#6231ff',
paddingVertical: 5,
paddingHorizontal: 20,
borderRadius: 10,
},
textStyle: {
color: 'white',
fontSize: 20,
},
});
export default App;