-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAppWithPlotly.js
More file actions
130 lines (120 loc) · 3.01 KB
/
AppWithPlotly.js
File metadata and controls
130 lines (120 loc) · 3.01 KB
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
import React, { useState } from 'react';
import { StyleSheet, View, Button, Text, SafeAreaView } from 'react-native';
import Plotly from 'react-native-plotly';
const upTrace = {
__id: 'up',
x: [1, 2, 3, 4, 5],
y: [1, 2, 3, 4, 8],
type: 'scatter',
};
const downTrace = {
__id: 'down',
x: [1, 2, 3, 4, 5],
y: [8, 4, 3, 2, 1],
type: 'scatter',
};
function randArr(num, mul) {
const arr = [];
const index = [];
for (let i = 0; i < num; i++) {
arr.push(Math.random() * mul)
index.push(i);
}
return arr;
}
const App = () => {
const [trace, setTrace] = useState(upTrace);
const [resetKey, setResetKey] = useState(0);
const [loading, setLoading] = useState(true);
function swapData() {
if (trace.__id === 'up') {
setTrace(downTrace);
} else {
setTrace(upTrace);
}
}
const update = (_, { data, layout, config }, plotly) => {
plotly.react(data, layout, config);
};
function reset() {
setLoading(true);
setResetKey(resetKey + 1);
}
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={styles.container}>
<View style={styles.buttonRow}>
<Button onPress={swapData} title="Swap Data" />
</View>
<View style={styles.loadingRow}>
<Text>{loading ? 'Loading' : 'Finished Loading'}</Text>
</View>
<View style={styles.chartRow}>
<Plotly
// data={[trace]}
// layout={{ title: 'Plotly.js running in React Native!' }}
style={{ borderWidth: 1, borderColor: 'green', width: 'auto' }}
data={[
{
x: [4],
y: [8],
z: [4],
type: 'scatter3d',
mode: 'markers',
marker: { color: 'red' },
scene: 'scene3',
}
]}
layout={{
width: '100%',
height: 900,
title: 'Fancy Plot',
scene3: {
domain: {
x: [0.5, 0.99],
y: [0.5, 1]
},
camera: {
center: { x: 0, y: 0, z: 0 },
eye: { x: 2.5, y: 0.1, z: 0.1 },
up: { x: 0, y: 0, z: 1 }
}
},
}}
update={update}
// onLoad={() => setLoading(false)}
// debug
key={resetKey}
enableFullPlotly
/>
</View>
<View style={styles.buttonRow}>
<Button onPress={reset} title="Force chart rerender" />
</View>
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
buttonRow: {
flexDirection: 'row',
},
loadingRow: {
flexDirection: 'row',
},
chartRow: {
flex: 1,
width: '100%',
borderWidth: 1,
borderColor: 'red'
},
container: {
paddingTop: 30,
width: '100%',
height: '100%',
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
export default App;