Skip to content

Commit cce1ed2

Browse files
Christoph Padermrousavy
andauthored
feat: Indicator and Range (#21)
* feat: implement indicator and range * memoize range and add null check in example * split up variables and add comment * simplify range in example * further simplify * Update example/src/screens/GraphPage.tsx Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com> * fix: indentation Co-authored-by: Marc Rousavy <marcrousavy@hotmail.com>
1 parent 9d66c44 commit cce1ed2

12 files changed

Lines changed: 641 additions & 186 deletions

example/src/screens/GraphPage.tsx

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { useCallback, useState } from 'react'
1+
import React, { useCallback, useMemo, useState } from 'react'
22
import { View, StyleSheet, Text, Button } from 'react-native'
33
import { LineGraph } from 'react-native-graph'
44
import StaticSafeAreaInsets from 'react-native-static-safe-area-insets'
5+
import type { GraphRange } from '../../../src/LineGraphProps'
56
import { SelectionDot } from '../components/CustomSelectionDot'
67
import { Toggle } from '../components/Toggle'
78
import {
@@ -19,15 +20,52 @@ export function GraphPage() {
1920
const [isAnimated, setIsAnimated] = useState(true)
2021
const [enablePanGesture, setEnablePanGesture] = useState(true)
2122
const [enableFadeInEffect, setEnableFadeInEffect] = useState(false)
23+
const [enableCustomSelectionDot, setEnableCustomSelectionDot] =
24+
useState(false)
25+
const [enableRange, setEnableRange] = useState(false)
26+
const [enableIndicator, setEnableIndicator] = useState(false)
27+
const [indicatorPulsating, setIndicatorPulsating] = useState(false)
2228

2329
const [points, setPoints] = useState(() => generateRandomGraphData(POINTS))
24-
const smallPoints = generateSinusGraphData(9)
30+
const smallPoints = useMemo(() => generateSinusGraphData(9), [])
2531

2632
const refreshData = useCallback(() => {
2733
setPoints(generateRandomGraphData(POINTS))
2834
hapticFeedback('impactLight')
2935
}, [])
3036

37+
const highestDate = useMemo(
38+
() =>
39+
points.length !== 0 && points[points.length - 1] != null
40+
? points[points.length - 1]!.date
41+
: undefined,
42+
[points]
43+
)
44+
const range: GraphRange | undefined = useMemo(() => {
45+
// if range is disabled, default to infinite range (undefined)
46+
if (!enableRange) return undefined
47+
48+
if (points.length !== 0 && highestDate != null) {
49+
return {
50+
x: {
51+
min: points[0]!.date,
52+
max: new Date(highestDate.getTime() + 30),
53+
},
54+
y: {
55+
min: -200,
56+
max: 200,
57+
},
58+
}
59+
} else {
60+
return {
61+
y: {
62+
min: -200,
63+
max: 200,
64+
},
65+
}
66+
}
67+
}, [enableRange, highestDate, points])
68+
3169
return (
3270
<View style={[styles.container, { backgroundColor: colors.background }]}>
3371
<View style={styles.row}>
@@ -52,7 +90,10 @@ export function GraphPage() {
5290
enablePanGesture={enablePanGesture}
5391
enableFadeInMask={enableFadeInEffect}
5492
onGestureStart={() => hapticFeedback('impactLight')}
55-
SelectionDot={SelectionDot}
93+
SelectionDot={enableCustomSelectionDot ? SelectionDot : undefined}
94+
range={range}
95+
enableIndicator={enableIndicator}
96+
indicatorPulsating={indicatorPulsating}
5697
/>
5798

5899
<Button title="Refresh" onPress={refreshData} />
@@ -73,6 +114,26 @@ export function GraphPage() {
73114
isEnabled={enableFadeInEffect}
74115
setIsEnabled={setEnableFadeInEffect}
75116
/>
117+
<Toggle
118+
title="Custom Selection Dot:"
119+
isEnabled={enableCustomSelectionDot}
120+
setIsEnabled={setEnableCustomSelectionDot}
121+
/>
122+
<Toggle
123+
title="Enable Range:"
124+
isEnabled={enableRange}
125+
setIsEnabled={setEnableRange}
126+
/>
127+
<Toggle
128+
title="Enable Indicator:"
129+
isEnabled={enableIndicator}
130+
setIsEnabled={setEnableIndicator}
131+
/>
132+
<Toggle
133+
title="Indicator pulsating:"
134+
isEnabled={indicatorPulsating}
135+
setIsEnabled={setIndicatorPulsating}
136+
/>
76137
</View>
77138

78139
<View style={styles.spacer} />

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"devDependencies": {
5858
"@react-native-community/eslint-config": "^2.0.0",
5959
"@release-it/conventional-changelog": "^2.0.0",
60+
"@shopify/react-native-skia": "^0.1.136",
6061
"@types/react": "^17.0.42",
6162
"@types/react-native": "^0.67.4",
6263
"eslint": "^7.2.0",
@@ -67,18 +68,17 @@
6768
"react": "^17.0.2",
6869
"react-native": "^0.67.4",
6970
"react-native-builder-bob": "^0.18.0",
70-
"release-it": "^14.2.2",
71-
"typescript": "^4.4.3",
7271
"react-native-gesture-handler": "^2.5.0",
7372
"react-native-reanimated": "^2.9.1",
74-
"@shopify/react-native-skia": "^0.1.136"
73+
"release-it": "^14.2.2",
74+
"typescript": "^4.4.3"
7575
},
7676
"peerDependencies": {
77+
"@shopify/react-native-skia": "*",
7778
"react": "*",
7879
"react-native": "*",
79-
"@shopify/react-native-skia": "*",
80-
"react-native-reanimated": ">=2",
81-
"react-native-gesture-handler": ">=2"
80+
"react-native-gesture-handler": ">=2",
81+
"react-native-reanimated": ">=2"
8282
},
8383
"release-it": {
8484
"git": {

0 commit comments

Comments
 (0)