Skip to content

Commit fa4ec6c

Browse files
authored
Merge branch 'rainbow-me:master' into master
2 parents be8cc58 + 1a623fb commit fa4ec6c

File tree

7 files changed

+125
-2
lines changed

7 files changed

+125
-2
lines changed

.prettierrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
arrowParens: 'avoid',
3+
bracketSpacing: true,
4+
bracketSameLine: false,
5+
singleQuote: true,
6+
trailingComma: 'es5',
7+
};

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ Labels are useful while moving finger through the chart to show the exact value
149149
| `format` | reanimated worklet | `a => a` | Worklet for formatting data from the chart. It can be useful when your data is a timestamp or currency. |
150150
| ...props | `object` | `{}` | Rest of the props applied to `TextInput` including `style` |
151151

152+
### `CurrentPositionVerticalLine` & `OpeningPositionHorizontalLine`
153+
CurrentPositionVerticalLine is a vertical line moving with user's finger through chart.
154+
155+
OpeningPositionHorizontalLine is a horizontal static line on height of the first point.
156+
157+
| Prop name | type | default | description |
158+
|--------------|----------------------|----------|-------------|
159+
| `color` | `string` | `#000000`| Color of the line. |
160+
| `thickness` | `number` | `2` | Thickness of the line. |
161+
| `length` | `number` | `null` | Length of the line. |
162+
| ...props | `object` | `{}` | Rest of the props applied to svg's `Line`. |
163+
164+
![](gifs/lines.png)
152165

153166

154167
### Candle Charts

gifs/lines.png

19.3 KB
Loading

src/charts/linear/ChartLines.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React, { useContext } from 'react';
2+
import Animated from 'react-native-reanimated';
3+
import { Svg, Line } from 'react-native-svg';
4+
import ChartContext from '../../helpers/ChartContext';
5+
import withReanimatedFallback from '../../helpers/withReanimatedFallback';
6+
7+
function ChartLineFactory(orientation) {
8+
const isVertical = orientation == 'vertical';
9+
return function ChartLine({
10+
color = '#000000',
11+
thickness = 2,
12+
length,
13+
...props
14+
}) {
15+
const {
16+
currentPositionVerticalLineStyle,
17+
openingPositionHorizontalLineStyle,
18+
} = useContext(ChartContext);
19+
return (
20+
<Animated.View
21+
pointerEvents="none"
22+
style={[
23+
isVertical
24+
? currentPositionVerticalLineStyle
25+
: openingPositionHorizontalLineStyle,
26+
{
27+
height: isVertical ? length + 20 : thickness,
28+
position: 'absolute',
29+
left: 0,
30+
top: 0,
31+
width: isVertical ? thickness : length,
32+
zIndex: -1,
33+
},
34+
]}
35+
>
36+
<Svg>
37+
<Line
38+
stroke={color}
39+
strokeWidth={thickness}
40+
strokeDasharray={10}
41+
x1={0}
42+
y1={0}
43+
x2={isVertical ? 0 : length}
44+
y2={isVertical ? length + 20 : 0}
45+
{...props}
46+
/>
47+
</Svg>
48+
</Animated.View>
49+
);
50+
};
51+
}
52+
53+
export const CurrentPositionVerticalLine = withReanimatedFallback(
54+
ChartLineFactory('vertical')
55+
);
56+
export const OpeningPositionHorizontalLine = withReanimatedFallback(
57+
ChartLineFactory('horizontal')
58+
);

src/charts/linear/ChartPath.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export default function ChartPathProvider({
194194
state,
195195
setContextValue = () => {},
196196
providedData = rawData,
197+
proceededData,
197198
} = useContext(ChartContext) || generateValues();
198199

199200
const prevData = useSharedValue(valuesStore.current.prevData, 'prevData');
@@ -224,6 +225,7 @@ export default function ChartPathProvider({
224225
return;
225226
}
226227
const [parsedData] = parse(data.points, data.yRange);
228+
proceededData.value = parsedData;
227229
const [parsedoriginalData, newExtremes] = parse(
228230
data.nativePoints || data.points
229231
);

src/charts/linear/ChartPathProvider.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import React, { useMemo, useState } from 'react';
2-
import { useAnimatedStyle } from 'react-native-reanimated';
2+
import {
3+
useAnimatedStyle,
4+
useSharedValue,
5+
withTiming,
6+
} from 'react-native-reanimated';
37

48
import ChartContext, { useGenerateValues } from '../../helpers/ChartContext';
59

610
export default function ChartPathProvider({ data: providedData, children }) {
711
const values = useGenerateValues();
12+
const proceededData = useSharedValue(null);
813
const dotStyle = useAnimatedStyle(
914
() => ({
1015
opacity: values.dotScale.value,
@@ -16,16 +21,50 @@ export default function ChartPathProvider({ data: providedData, children }) {
1621
}),
1722
[]
1823
);
24+
25+
const currentPositionVerticalLineStyle = useAnimatedStyle(
26+
() => ({
27+
opacity: values.dotScale.value,
28+
transform: [{ translateX: values.positionX.value }],
29+
}),
30+
[]
31+
);
32+
33+
const openingPositionHorizontalLineStyle = useAnimatedStyle(() => {
34+
return {
35+
opacity: proceededData == null ? 0 : 1,
36+
transform: [
37+
{
38+
translateY: withTiming(
39+
proceededData?.value?.[0].y * values?.layoutSize?.value?.height +
40+
10 || 0
41+
),
42+
},
43+
],
44+
};
45+
}, [proceededData]);
46+
1947
const [contextReanimatedValue, setContextValue] = useState({});
2048
const contextValue = useMemo(
2149
() => ({
2250
dotStyle,
51+
currentPositionVerticalLineStyle,
52+
openingPositionHorizontalLineStyle,
2353
...values,
2454
...contextReanimatedValue,
2555
providedData,
56+
proceededData,
2657
setContextValue,
2758
}),
28-
[dotStyle, values, contextReanimatedValue, providedData]
59+
[
60+
dotStyle,
61+
currentPositionVerticalLineStyle,
62+
openingPositionHorizontalLineStyle,
63+
values,
64+
contextReanimatedValue,
65+
providedData,
66+
proceededData,
67+
]
2968
);
3069

3170
return (

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Animated.addWhitelistedNativeProps({ text: true });
33

44
export { default as ChartPathProvider } from './charts/linear/ChartPathProvider';
55
export { default as ChartDot } from './charts/linear/ChartDot';
6+
export {
7+
CurrentPositionVerticalLine,
8+
OpeningPositionHorizontalLine,
9+
} from './charts/linear/ChartLines';
610
export { ChartYLabel, ChartXLabel } from './charts/linear/ChartLabels';
711
export { default as ChartPath } from './charts/linear/ChartPath';
812
export { default as useChartData } from './helpers/useChartData';

0 commit comments

Comments
 (0)