Skip to content

Commit 7f3cd89

Browse files
author
Christoph Pader
authored
Feat: Improve createGraphPath function and simplify range logic (#62)
* feat: improve createGraphPath function and simplify range logic * remove unused code
1 parent 479ce84 commit 7f3cd89

File tree

5 files changed

+160
-192
lines changed

5 files changed

+160
-192
lines changed

src/AnimatedLineGraph.tsx

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
createGraphPathWithGradient,
2525
getGraphPathRange,
2626
GraphPathRange,
27-
pixelFactorX,
27+
getXInRange,
28+
getPointsInRange,
2829
} from './CreateGraphPath'
2930
import Reanimated, {
3031
runOnJS,
@@ -51,9 +52,8 @@ const INDICATOR_PULSE_BLUR_RADIUS_BIG =
5152
INDICATOR_RADIUS * INDICATOR_BORDER_MULTIPLIER + 20
5253

5354
export function AnimatedLineGraph({
54-
points,
55+
points: allPoints,
5556
color,
56-
smoothing = 0.2,
5757
gradientFillColors,
5858
lineThickness = 3,
5959
range,
@@ -67,7 +67,7 @@ export function AnimatedLineGraph({
6767
enableIndicator = false,
6868
indicatorPulsating = false,
6969
horizontalPadding = enableIndicator
70-
? INDICATOR_RADIUS * INDICATOR_BORDER_MULTIPLIER
70+
? Math.ceil(INDICATOR_RADIUS * INDICATOR_BORDER_MULTIPLIER)
7171
: 0,
7272
verticalPadding = lineThickness,
7373
TopAxisLabel,
@@ -136,27 +136,27 @@ export function AnimatedLineGraph({
136136
const pointSelectedIndex = useRef<number>()
137137

138138
const pathRange: GraphPathRange = useMemo(
139-
() => getGraphPathRange(points, range),
140-
[points, range]
139+
() => getGraphPathRange(allPoints, range),
140+
[allPoints, range]
141141
)
142142

143-
const drawingWidth = useMemo(() => {
144-
return Math.max(Math.floor(width - 2 * horizontalPadding), 0)
145-
}, [horizontalPadding, width])
143+
const pointsInRange = useMemo(
144+
() => getPointsInRange(allPoints, pathRange),
145+
[allPoints, pathRange]
146+
)
147+
148+
const drawingWidth = useMemo(
149+
() => width - 2 * horizontalPadding,
150+
[horizontalPadding, width]
151+
)
146152

147153
const lineWidth = useMemo(() => {
148-
const lastPoint = points[points.length - 1]
154+
const lastPoint = pointsInRange[pointsInRange.length - 1]
149155

150-
if (lastPoint == null) return width - 2 * horizontalPadding
156+
if (lastPoint == null) return drawingWidth
151157

152-
return Math.max(
153-
Math.floor(
154-
(width - 2 * horizontalPadding) *
155-
pixelFactorX(lastPoint.date, pathRange.x.min, pathRange.x.max)
156-
),
157-
0
158-
)
159-
}, [horizontalPadding, pathRange.x.max, pathRange.x.min, points, width])
158+
return Math.max(getXInRange(drawingWidth, lastPoint.date, pathRange.x), 0)
159+
}, [drawingWidth, pathRange.x, pointsInRange])
160160

161161
const indicatorX = useMemo(
162162
() =>
@@ -181,7 +181,7 @@ export function AnimatedLineGraph({
181181
// view is not yet measured!
182182
return
183183
}
184-
if (points.length < 1) {
184+
if (pointsInRange.length < 1) {
185185
// points are still empty!
186186
return
187187
}
@@ -190,9 +190,8 @@ export function AnimatedLineGraph({
190190
let gradientPath
191191

192192
const createGraphPathProps = {
193-
points: points,
193+
pointsInRange: pointsInRange,
194194
range: pathRange,
195-
smoothing: smoothing,
196195
horizontalPadding: horizontalPadding,
197196
verticalPadding: verticalPadding,
198197
canvasHeight: height,
@@ -270,7 +269,7 @@ export function AnimatedLineGraph({
270269
paths,
271270
shouldFillGradient,
272271
gradientPaths,
273-
points,
272+
pointsInRange,
274273
range,
275274
straightLine,
276275
verticalPadding,
@@ -342,28 +341,30 @@ export function AnimatedLineGraph({
342341

343342
const setFingerX = useCallback(
344343
(fingerX: number) => {
345-
const lowerBound = horizontalPadding
346-
const upperBound = drawingWidth + horizontalPadding
347-
348-
const fingerXInRange = Math.min(Math.max(fingerX, lowerBound), upperBound)
349-
const y = getYForX(commands.current, fingerXInRange)
344+
const y = getYForX(commands.current, fingerX)
350345

351346
if (y != null) {
347+
circleX.current = fingerX
352348
circleY.current = y
353-
circleX.current = fingerXInRange
354349
}
355350

356-
if (isActive.value) pathEnd.current = fingerXInRange / width
351+
if (isActive.value) pathEnd.current = fingerX / width
357352

358-
const actualFingerX = fingerX - horizontalPadding
353+
const fingerXInRange = Math.max(fingerX - horizontalPadding, 0)
359354

360355
const index = Math.round(
361-
(actualFingerX / drawingWidth) * (points.length - 1)
356+
(fingerXInRange /
357+
getXInRange(
358+
drawingWidth,
359+
pointsInRange[pointsInRange.length - 1]!.date,
360+
pathRange.x
361+
)) *
362+
(pointsInRange.length - 1)
362363
)
363-
const pointIndex = Math.min(Math.max(index, 0), points.length - 1)
364+
const pointIndex = Math.min(Math.max(index, 0), pointsInRange.length - 1)
364365

365366
if (pointSelectedIndex.current !== pointIndex) {
366-
const dataPoint = points[pointIndex]
367+
const dataPoint = pointsInRange[pointIndex]
367368
pointSelectedIndex.current = pointIndex
368369

369370
if (dataPoint != null) {
@@ -379,7 +380,8 @@ export function AnimatedLineGraph({
379380
isActive.value,
380381
onPointSelected,
381382
pathEnd,
382-
points,
383+
pathRange.x,
384+
pointsInRange,
383385
width,
384386
]
385387
)
@@ -432,9 +434,9 @@ export function AnimatedLineGraph({
432434
)
433435

434436
useEffect(() => {
435-
if (points.length !== 0 && commands.current.length !== 0)
437+
if (pointsInRange.length !== 0 && commands.current.length !== 0)
436438
pathEnd.current = 1
437-
}, [commands, pathEnd, points.length])
439+
}, [commands, pathEnd, pointsInRange.length])
438440

439441
useEffect(() => {
440442
if (indicatorPulsating) {

0 commit comments

Comments
 (0)