Skip to content

Commit 8b3442f

Browse files
committed
feat: add pathfill prop and clean up api
1 parent f677a3e commit 8b3442f

File tree

4 files changed

+17
-32
lines changed

4 files changed

+17
-32
lines changed

example/app/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type PanGestureHandlerOnChangeEventPayload,
55
HoverGestureHandlerOnChangeEventPayload,
66
} from "@codeherence/react-native-graph";
7+
import { LinearGradient, vec } from "@shopify/react-native-skia";
78
import { useCallback, useEffect, useState } from "react";
89
import { Button, StyleSheet, Text, View } from "react-native";
910
import { useAnimatedProps, useSharedValue } from "react-native-reanimated";
@@ -80,6 +81,10 @@ export default () => {
8081
onPanGestureEnd={onEndWorklet}
8182
onHoverGestureEnd={onEndWorklet}
8283
onHoverGestureChange={onHoverChangeWorklet}
84+
strokeWidth={2}
85+
PathFill={({ width }) => (
86+
<LinearGradient start={vec(0, 0)} end={vec(width, 0)} colors={["blue", "yellow"]} />
87+
)}
8388
/>
8489
</View>
8590
);

src/components/LineChart/Cursor.tsx

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ export const Cursor: React.FC<CursorProps> = ({ x, y, height, cursorRadius }) =>
1818
return [{ translateX: x.value }, { translateY: y.value }];
1919
}, []);
2020

21-
// const animatedText: AnimatedProp<string> = useDerivedValue(() => {
22-
// // Must interpolate the y value to the proper value
23-
// const interpolatedY = interpolate(
24-
// y.value,
25-
// [cursorRadius, height + cursorRadius],
26-
// [maxValue, minValue]
27-
// );
28-
// return formatter(interpolatedY);
29-
// }, [maxValue, minValue, height, formatter]);
30-
31-
// const bannerTransform = useDerivedValue(() => {
32-
// return [{ translateX: cursorRadius }, { translateY: -cursorRadius - BASE_LABEL_MARGIN }];
33-
// });
34-
3521
return (
3622
<>
3723
<Line
@@ -42,11 +28,6 @@ export const Cursor: React.FC<CursorProps> = ({ x, y, height, cursorRadius }) =>
4228
strokeWidth={2}
4329
/>
4430
<Group transform={transform}>
45-
{/* {BannerComponent && (
46-
<Group transform={bannerTransform} style="fill" color="red">
47-
{BannerComponent({ text: animatedText })}
48-
</Group>
49-
)} */}
5031
<Circle cx={0} cy={0} r={cursorRadius} color="black" />
5132
</Group>
5233
</>

src/components/LineChart/constants.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,3 @@ export const DEFAULT_STROKE_WIDTH = 2;
55
export const DEFAULT_CURSOR_RADIUS = 8;
66
export const DEFAULT_CURVE_TYPE: ComputePathProps["curveType"] = "linear";
77
export const DEFAULT_BANNER_COMPONENT = null;
8-
export const DEFAULT_FORMATTER = (price: number) => {
9-
"worklet";
10-
return price.toString();
11-
};

src/components/LineChart/index.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import { useSharedValue } from "react-native-reanimated";
77
import { AxisLabelComponentProps, AxisLabelContainer } from "./AxisLabel";
88
import { Cursor } from "./Cursor";
99
import { computePath, type ComputePathProps, computeGraphData } from "./computations";
10-
import {
11-
DEFAULT_CURSOR_RADIUS,
12-
DEFAULT_CURVE_TYPE,
13-
DEFAULT_FORMATTER,
14-
DEFAULT_STROKE_WIDTH,
15-
} from "./constants";
10+
import { DEFAULT_CURSOR_RADIUS, DEFAULT_CURVE_TYPE, DEFAULT_STROKE_WIDTH } from "./constants";
1611
import {
1712
HoverGestureHandlerOnBeginEventPayload,
1813
HoverGestureHandlerOnChangeEventPayload,
@@ -23,16 +18,22 @@ import {
2318
useGestures,
2419
} from "./useGestures";
2520

21+
export interface PathFillProps {
22+
strokeWidth: number;
23+
height: number;
24+
width: number;
25+
}
26+
2627
export type LineChartProps = ViewProps & {
2728
/** Array of [x, y] points for the chart */
2829
points: [number, number][];
2930
strokeWidth?: number;
3031
cursorRadius?: number;
3132
curveType?: ComputePathProps["curveType"];
3233
/** A worklet function to format a given price. */
33-
formatter?: (price: number) => string;
3434
TopAxisLabel?: React.FC<AxisLabelComponentProps>;
3535
BottomAxisLabel?: React.FC<AxisLabelComponentProps>;
36+
PathFill?: React.FC<PathFillProps>;
3637
/** Callback when the pan gesture begins. This function must be a worklet function. */
3738
onPanGestureBegin?: ((payload: PanGestureHandlerOnBeginEventPayload) => void) | null;
3839
onPanGestureChange?: ((payload: PanGestureHandlerOnChangeEventPayload) => void) | null;
@@ -47,9 +48,9 @@ export const LineChart: React.FC<LineChartProps> = ({
4748
strokeWidth = DEFAULT_STROKE_WIDTH,
4849
cursorRadius = DEFAULT_CURSOR_RADIUS,
4950
curveType = DEFAULT_CURVE_TYPE,
50-
formatter = DEFAULT_FORMATTER,
5151
TopAxisLabel = null,
5252
BottomAxisLabel = null,
53+
PathFill = null,
5354
onPanGestureBegin = null,
5455
onPanGestureChange = null,
5556
onPanGestureEnd = null,
@@ -104,7 +105,9 @@ export const LineChart: React.FC<LineChartProps> = ({
104105
<GestureDetector gesture={gestures}>
105106
<View style={styles.container} onLayout={onLayout}>
106107
<Canvas style={{ height, width }}>
107-
<Path style="stroke" path={path} strokeWidth={strokeWidth} color="black" />
108+
<Path style="stroke" path={path} strokeWidth={strokeWidth} color="white">
109+
{PathFill && PathFill({ width, height, strokeWidth })}
110+
</Path>
108111
<Cursor x={x} y={y} height={height} cursorRadius={cursorRadius} />
109112
</Canvas>
110113
</View>

0 commit comments

Comments
 (0)