-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(xychart): add AnimatedLineSeries + tests (#874)
* internal(xychart): LineSeries => private/BaseLineSeries * new(xychart): add AnimatedLineSeries, LineSeries, AnimatedPath * new(demo/xychart): use AnimatedLineSeries * test(xychart): add Animated(BarGroup, BarStack, BarSeries, LineSeries) tests * fix(xychart/getScaledValueFactory): return NaN instead of null
- Loading branch information
1 parent
7514240
commit 48c4a34
Showing
12 changed files
with
211 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/visx-xychart/src/components/series/AnimatedLineSeries.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { AxisScale } from '@visx/axis'; | ||
import React from 'react'; | ||
import BaseLineSeries, { BaseLineSeriesProps } from './private/BaseLineSeries'; | ||
import AnimatedPath from './private/AnimatedPath'; | ||
|
||
export default function AnimatedLineSeries< | ||
XScale extends AxisScale, | ||
YScale extends AxisScale, | ||
Datum extends object | ||
>({ ...props }: Omit<BaseLineSeriesProps<XScale, YScale, Datum>, 'PathComponent'>) { | ||
return <BaseLineSeries<XScale, YScale, Datum> {...props} PathComponent={AnimatedPath} />; | ||
} |
77 changes: 5 additions & 72 deletions
77
packages/visx-xychart/src/components/series/LineSeries.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,11 @@ | ||
import React, { useContext, useCallback } from 'react'; | ||
import LinePath from '@visx/shape/lib/shapes/LinePath'; | ||
import { AxisScale } from '@visx/axis'; | ||
import DataContext from '../../context/DataContext'; | ||
import { SeriesProps } from '../../types'; | ||
import withRegisteredData, { WithRegisteredDataProps } from '../../enhancers/withRegisteredData'; | ||
import getScaledValueFactory from '../../utils/getScaledValueFactory'; | ||
import useEventEmitter, { HandlerParams } from '../../hooks/useEventEmitter'; | ||
import findNearestDatumX from '../../utils/findNearestDatumX'; | ||
import TooltipContext from '../../context/TooltipContext'; | ||
import findNearestDatumY from '../../utils/findNearestDatumY'; | ||
import React from 'react'; | ||
import BaseLineSeries, { BaseLineSeriesProps } from './private/BaseLineSeries'; | ||
|
||
type LineSeriesProps< | ||
export default function LineSeries< | ||
XScale extends AxisScale, | ||
YScale extends AxisScale, | ||
Datum extends object | ||
> = SeriesProps<XScale, YScale, Datum> & { | ||
/** Whether line should be rendered horizontally instead of vertically. */ | ||
horizontal?: boolean; | ||
}; | ||
|
||
function LineSeries<XScale extends AxisScale, YScale extends AxisScale, Datum extends object>({ | ||
data, | ||
dataKey, | ||
xAccessor, | ||
xScale, | ||
yAccessor, | ||
yScale, | ||
horizontal = true, | ||
...lineProps | ||
}: LineSeriesProps<XScale, YScale, Datum> & WithRegisteredDataProps<XScale, YScale, Datum>) { | ||
const { colorScale, theme, width, height } = useContext(DataContext); | ||
const { showTooltip, hideTooltip } = useContext(TooltipContext) ?? {}; | ||
const getScaledX = useCallback(getScaledValueFactory(xScale, xAccessor), [xScale, xAccessor]); | ||
const getScaledY = useCallback(getScaledValueFactory(yScale, yAccessor), [yScale, yAccessor]); | ||
const color = colorScale?.(dataKey) ?? theme?.colors?.[0] ?? '#222'; | ||
|
||
const handleMouseMove = useCallback( | ||
(params?: HandlerParams) => { | ||
const { svgPoint } = params || {}; | ||
if (svgPoint && width && height && showTooltip) { | ||
const datum = (horizontal ? findNearestDatumX : findNearestDatumY)({ | ||
point: svgPoint, | ||
data, | ||
xScale, | ||
yScale, | ||
xAccessor, | ||
yAccessor, | ||
width, | ||
height, | ||
}); | ||
if (datum) { | ||
showTooltip({ | ||
key: dataKey, | ||
...datum, | ||
svgPoint, | ||
}); | ||
} | ||
} | ||
}, | ||
[dataKey, data, xScale, yScale, xAccessor, yAccessor, width, height, showTooltip, horizontal], | ||
); | ||
useEventEmitter('mousemove', handleMouseMove); | ||
useEventEmitter('mouseout', hideTooltip); | ||
|
||
return ( | ||
<LinePath | ||
data={data} | ||
x={getScaledX} | ||
y={getScaledY} | ||
stroke={color} | ||
strokeWidth={2} | ||
{...lineProps} | ||
/> | ||
); | ||
>({ ...props }: Omit<BaseLineSeriesProps<XScale, YScale, Datum>, 'PathComponent'>) { | ||
return <BaseLineSeries<XScale, YScale, Datum> {...props} />; | ||
} | ||
|
||
export default withRegisteredData(LineSeries); |
11 changes: 11 additions & 0 deletions
11
packages/visx-xychart/src/components/series/private/AnimatedPath.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import { animated, useSpring } from 'react-spring'; | ||
|
||
export default function AnimatedPath({ | ||
d, | ||
stroke, | ||
...lineProps | ||
}: Omit<React.SVGProps<SVGPathElement>, 'ref'>) { | ||
const tweened = useSpring({ d, stroke, config: { precision: 0.01 } }); | ||
return <animated.path d={tweened.d} stroke={tweened.stroke} fill="transparent" {...lineProps} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/visx-xychart/src/components/series/private/BaseLineSeries.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React, { useContext, useCallback } from 'react'; | ||
import LinePath from '@visx/shape/lib/shapes/LinePath'; | ||
import { AxisScale } from '@visx/axis'; | ||
import DataContext from '../../../context/DataContext'; | ||
import { SeriesProps } from '../../../types'; | ||
import withRegisteredData, { WithRegisteredDataProps } from '../../../enhancers/withRegisteredData'; | ||
import getScaledValueFactory from '../../../utils/getScaledValueFactory'; | ||
import useEventEmitter, { HandlerParams } from '../../../hooks/useEventEmitter'; | ||
import findNearestDatumX from '../../../utils/findNearestDatumX'; | ||
import TooltipContext from '../../../context/TooltipContext'; | ||
import findNearestDatumY from '../../../utils/findNearestDatumY'; | ||
|
||
export type BaseLineSeriesProps< | ||
XScale extends AxisScale, | ||
YScale extends AxisScale, | ||
Datum extends object | ||
> = SeriesProps<XScale, YScale, Datum> & { | ||
/** Whether line should be rendered horizontally instead of vertically. */ | ||
horizontal?: boolean; | ||
/** Rendered component which is passed path props by BaseLineSeries after processing. */ | ||
PathComponent?: React.FC<Omit<React.SVGProps<SVGPathElement>, 'ref'>> | 'path'; | ||
}; | ||
|
||
function BaseLineSeries<XScale extends AxisScale, YScale extends AxisScale, Datum extends object>({ | ||
data, | ||
dataKey, | ||
xAccessor, | ||
xScale, | ||
yAccessor, | ||
yScale, | ||
horizontal = true, | ||
PathComponent = 'path', | ||
...lineProps | ||
}: BaseLineSeriesProps<XScale, YScale, Datum> & WithRegisteredDataProps<XScale, YScale, Datum>) { | ||
const { colorScale, theme, width, height } = useContext(DataContext); | ||
const { showTooltip, hideTooltip } = useContext(TooltipContext) ?? {}; | ||
const getScaledX = useCallback(getScaledValueFactory(xScale, xAccessor), [xScale, xAccessor]); | ||
const getScaledY = useCallback(getScaledValueFactory(yScale, yAccessor), [yScale, yAccessor]); | ||
const color = colorScale?.(dataKey) ?? theme?.colors?.[0] ?? '#222'; | ||
|
||
const handleMouseMove = useCallback( | ||
(params?: HandlerParams) => { | ||
const { svgPoint } = params || {}; | ||
if (svgPoint && width && height && showTooltip) { | ||
const datum = (horizontal ? findNearestDatumX : findNearestDatumY)({ | ||
point: svgPoint, | ||
data, | ||
xScale, | ||
yScale, | ||
xAccessor, | ||
yAccessor, | ||
width, | ||
height, | ||
}); | ||
if (datum) { | ||
showTooltip({ | ||
key: dataKey, | ||
...datum, | ||
svgPoint, | ||
}); | ||
} | ||
} | ||
}, | ||
[dataKey, data, xScale, yScale, xAccessor, yAccessor, width, height, showTooltip, horizontal], | ||
); | ||
useEventEmitter('mousemove', handleMouseMove); | ||
useEventEmitter('mouseout', hideTooltip); | ||
|
||
return ( | ||
<LinePath | ||
data={data} | ||
x={getScaledX} | ||
y={getScaledY} | ||
stroke={color} | ||
strokeWidth={2} | ||
{...lineProps} | ||
> | ||
{({ path }) => ( | ||
<PathComponent stroke={color} strokeWidth={2} {...lineProps} d={path(data) || ''} /> | ||
)} | ||
</LinePath> | ||
); | ||
} | ||
|
||
export default withRegisteredData(BaseLineSeries); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.