Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: useYAt #82

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ A sweet & simple chart library for React Native that will make us feel like **W*
- [LineChart.useChart](#linechartusechart)
- [LineChart.useDatetime](#linechartusedatetime)
- [LineChart.usePrice](#linechartuseprice)
- [LineChart.useYAt](#linechartuseyat)
- [CandlestickChart.useChart](#candlestickchartusechart)
- [CandlestickChart.useCandleData](#candlestickchartusecandledata)
- [CandlestickChart.useDatetime](#candlestickchartusedatetime)
Expand Down Expand Up @@ -1055,6 +1056,31 @@ const { value, formatted } = LineChart.usePrice({
| `value` | `string` | | Price value |
| `formatted` | `string` | | Formatted price value |

### LineChart.useYAt

Get the y value on the chart for a given value or index.

```jsx
const y = LineChart.useYAt({
at,
offsetY,
});
```

**Arguments**

| Variable | Type | Default | Description |
|-----------|--------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------|
| `at` | `number` or `{ index: number }` or `{ value: number }` | `0` | Index of followed `data` item. You can alternatively pass `{ value: number }`, corresponding to a y value. |
| `offsetY` | `number` | `0` | An optional offset for the y value. |

**Returns**

| Variable | Type | Default | Description |
| ----------- | -------- | ------- | --------------------- |
| `value` | `string` | | Price value |
| `formatted` | `string` | | Formatted price value |

### CandlestickChart.useChart

```jsx
Expand Down
61 changes: 8 additions & 53 deletions src/charts/line/HorizontalLine.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import React from 'react';
import Animated, {
useAnimatedProps,
useDerivedValue,
withTiming,
} from 'react-native-reanimated';
import Animated, { useAnimatedProps } from 'react-native-reanimated';
import { Line as SVGLine, LineProps } from 'react-native-svg';
import { getYForX, parse } from 'react-native-redash';

import { LineChartDimensionsContext } from './Chart';
import { useLineChart } from './useLineChart';
import type { AtPoint } from './types';
import { useYAt } from './useYAt';

const AnimatedLine = Animated.createAnimatedComponent(SVGLine);

Expand All @@ -33,16 +28,7 @@ type HorizontalLineProps = {
* />
* ```
*/
at?:
| {
index: number;
value?: never;
}
| {
index?: never;
value: number;
}
| number;
at?: AtPoint;
};

LineChartHorizontalLine.displayName = 'LineChartHorizontalLine';
Expand All @@ -53,41 +39,10 @@ export function LineChartHorizontalLine({
at = { index: 0 },
offsetY = 0,
}: HorizontalLineProps) {
const { width, path, height, gutter } = React.useContext(
LineChartDimensionsContext
);
const { data, yDomain } = useLineChart();

const parsedPath = React.useMemo(() => parse(path), [path]);
const pointWidth = React.useMemo(
() => width / data.length,
[data.length, width]
);

const y = useDerivedValue(() => {
if (typeof at === 'number' || at.index != null) {
const index = typeof at === 'number' ? at : at.index;
const yForX = getYForX(parsedPath!, pointWidth * index) || 0;
return withTiming(yForX + offsetY);
}
/**
* <gutter>
* | ---------- | <- yDomain.max |
* | | | offsetTop
* | | <- value |
* | |
* | | <- yDomain.min
* <gutter>
*/

const offsetTop = yDomain.max - at.value;
const percentageOffsetTop = offsetTop / (yDomain.max - yDomain.min);

const heightBetweenGutters = height - gutter * 2;

const offsetTopPixels = gutter + percentageOffsetTop * heightBetweenGutters;

return withTiming(offsetTopPixels + offsetY);
const { width } = React.useContext(LineChartDimensionsContext);
const y = useYAt({
at,
offsetY,
});

const lineAnimatedProps = useAnimatedProps(() => ({
Expand Down
2 changes: 2 additions & 0 deletions src/charts/line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useLineChartDatetime } from './useDatetime';
import { useLineChartPrice } from './usePrice';
import { useLineChart } from './useLineChart';
import { LineChartHoverTrap } from '../line/HoverTrap';
import { useYAt } from './useYAt';

export * from './Chart';
export * from './ChartPath';
Expand Down Expand Up @@ -54,4 +55,5 @@ export const LineChart = Object.assign(_LineChart, {
usePrice: useLineChartPrice,
useChart: useLineChart,
HoverTrap: LineChartHoverTrap,
useYAt: useYAt,
});
11 changes: 11 additions & 0 deletions src/charts/line/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ export type YDomain = {
min: number;
max: number;
};

export type AtPoint =
| {
index: number;
value?: never;
}
| {
index?: never;
value: number;
}
| number;
50 changes: 50 additions & 0 deletions src/charts/line/useYAt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import type { AtPoint } from './types';
import { useLineChart } from './useLineChart';
import { LineChartDimensionsContext } from './Chart';
import { getYForX, parse } from 'react-native-redash';
import { useDerivedValue, withTiming } from 'react-native-reanimated';

export type ViewAtProps = {
at: AtPoint;
offsetY?: number;
};

export function useYAt({ at, offsetY = 0 }: ViewAtProps) {
const { width, path, height, gutter } = React.useContext(
LineChartDimensionsContext
);
const { data, yDomain } = useLineChart();

const parsedPath = React.useMemo(() => parse(path), [path]);
const pointWidth = React.useMemo(
() => width / data.length,
[data.length, width]
);

return useDerivedValue(() => {
if (typeof at === 'number' || at.index != null) {
const index = typeof at === 'number' ? at : at.index;
const yForX = getYForX(parsedPath!, pointWidth * index) || 0;
return withTiming(yForX + offsetY);
}
/**
* <gutter>
* | ---------- | <- yDomain.max |
* | | | offsetTop
* | | <- value |
* | |
* | | <- yDomain.min
* <gutter>
*/

const offsetTop = yDomain.max - at.value;
const percentageOffsetTop = offsetTop / (yDomain.max - yDomain.min);

const heightBetweenGutters = height - gutter * 2;

const offsetTopPixels = gutter + percentageOffsetTop * heightBetweenGutters;

return withTiming(offsetTopPixels + offsetY);
}, [at, parsedPath, pointWidth, yDomain, height, gutter, offsetY]);
}