Skip to content

Commit 40ebc4d

Browse files
Evan YEvan Y
authored andcommitted
feat: introduce static line chart
Key Highlights: - Introduced isStatic property for LineChart component - Separated LineChart into an interactive and static chart component - Created example screen showcasing the static chart - Refactored the example application slightly by moving dummy data into the store directory
1 parent a05269a commit 40ebc4d

File tree

14 files changed

+15205
-186
lines changed

14 files changed

+15205
-186
lines changed

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
{
2-
"editor.formatOnSave": true
2+
"editor.formatOnSave": true,
3+
// Fix all errors
4+
"editor.codeActionsOnSave": {
5+
"source.fixAll.eslint": "always"
6+
},
7+
38
}

example/app/chart.tsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import { useAnimatedProps, useSharedValue } from "react-native-reanimated";
1111
import { useSafeAreaInsets } from "react-native-safe-area-context";
1212

1313
import AnimatedText from "../components/AnimatedText";
14-
import aapl_prices from "../data/aapl_prices.json";
15-
import msft_prices from "../data/msft_prices.json";
16-
import nvda_prices from "../data/nvda_prices.json";
17-
import unity_prices from "../data/unity_prices.json";
14+
import { priceMap } from "../src/store/prices";
1815

1916
const formatter = new Intl.NumberFormat("en-US", {
2017
style: "currency",
@@ -34,21 +31,6 @@ const uiFormatter = (price: number) => {
3431
}).format(price);
3532
};
3633

37-
const priceMap = {
38-
msft: [...msft_prices.results]
39-
.reverse()
40-
.map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]),
41-
aapl: aapl_prices.results
42-
.reverse()
43-
.map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]),
44-
nvda: nvda_prices.results
45-
.reverse()
46-
.map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]),
47-
unity: unity_prices.results
48-
.reverse()
49-
.map<[number, number]>((r) => [new Date(r.date).getTime(), r.close]),
50-
};
51-
5234
const priceMapKeys = Object.keys(priceMap) as (keyof typeof priceMap)[];
5335

5436
export default () => {

example/app/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export default () => {
1111
showsVerticalScrollIndicator={false}
1212
>
1313
<Pressable onPress={() => navigate("/chart")}>
14-
<Text style={styles.link}>Go to Charts</Text>
14+
<Text style={styles.link}>Go to interactive chart</Text>
15+
</Pressable>
16+
<Pressable onPress={() => navigate("/static_chart")}>
17+
<Text style={styles.link}>Go to static chart</Text>
1518
</Pressable>
1619
</ScrollView>
1720
);

example/app/static_chart.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { type AxisLabelProps, LineChart } from "@codeherence/react-native-graph";
2+
import { Paint } from "@shopify/react-native-skia";
3+
import { useReducer } from "react";
4+
import { Button, ScrollView, StyleSheet, Text } from "react-native";
5+
import { useSafeAreaInsets } from "react-native-safe-area-context";
6+
7+
import { priceMap } from "../src/store/prices";
8+
9+
const formatter = new Intl.NumberFormat("en-US", {
10+
style: "currency",
11+
currency: "USD",
12+
});
13+
14+
const AxisLabel: React.FC<AxisLabelProps> = ({ value }) => (
15+
<Text selectable={false}>{formatter.format(value)}</Text>
16+
);
17+
18+
const priceMapKeys = Object.keys(priceMap) as (keyof typeof priceMap)[];
19+
20+
export default () => {
21+
const { bottom, left, right } = useSafeAreaInsets();
22+
const [counter, increment] = useReducer((s: number) => s + 1, 0);
23+
24+
const symbol = priceMapKeys[counter % priceMapKeys.length]!;
25+
const data = priceMap[symbol];
26+
27+
return (
28+
<ScrollView
29+
style={styles.container}
30+
contentContainerStyle={[
31+
styles.contentContainer,
32+
{
33+
paddingBottom: bottom,
34+
paddingLeft: left,
35+
paddingRight: right,
36+
},
37+
]}
38+
showsVerticalScrollIndicator={false}
39+
>
40+
<Button title={`Showing ${symbol}. Press to switch.`} onPress={increment} />
41+
<LineChart
42+
isStatic
43+
points={data}
44+
style={styles.chart}
45+
TopAxisLabel={AxisLabel}
46+
BottomAxisLabel={AxisLabel}
47+
strokeWidth={2}
48+
PathFill={({ strokeWidth }) => (
49+
<Paint style="stroke" strokeWidth={strokeWidth} color="lightblue" />
50+
)}
51+
/>
52+
</ScrollView>
53+
);
54+
};
55+
56+
const styles = StyleSheet.create({
57+
container: { flex: 1 },
58+
contentContainer: { flexGrow: 1 },
59+
chart: { flex: 1, maxHeight: 300 },
60+
price: { fontSize: 32 },
61+
});

0 commit comments

Comments
 (0)