-
Notifications
You must be signed in to change notification settings - Fork 715
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
new(xychart): add AnimatedLineSeries + tests #874
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ceb608f
internal(xychart): LineSeries => private/BaseLineSeries
williaster 3825d93
new(xychart): add AnimatedLineSeries, LineSeries, AnimatedPath
williaster 0987585
new(demo/xychart): use AnimatedLineSeries
williaster 5b88733
test(xychart): add Animated(BarGroup, BarStack, BarSeries, LineSeries…
williaster 81044b8
fix(xychart/getScaledValueFactory): return NaN instead of null
williaster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
everything is the same in this file except we use the child render function here.
sorry I tried to
git mv
and it didn't work in this UI view 💔