Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal(vx-shape/SplitLinePath): remove dir structure, fix types, fe…
…wer multiplications, improve path measurement node attributes
- Loading branch information
1 parent
67b889e
commit 6bcd74d
Showing
3 changed files
with
90 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { useMemo } from 'react'; | ||
import LinePath from './LinePath'; | ||
import getSplitLineSegments from '../util/getSplitLineSegments'; | ||
import { LinePathConfig } from '../types'; | ||
|
||
interface Point { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
type SplitLinePathProps<Datum> = { | ||
/** Array of data segments, where each segment will be a separate path in the rendered line. */ | ||
segments: Datum[][]; | ||
/** Styles to apply to each segment. If fewer styles are specified than the number of segments, they will be re-used. */ | ||
styles: Omit<React.SVGProps<SVGPathElement>, 'x' | 'y' | 'children'>[]; | ||
/** Override render function which is passed the configured path generator as input. */ | ||
children?: (renderProps: { | ||
index: number; | ||
segment: { x: number; y: number }[]; | ||
styles?: Omit<React.SVGProps<SVGPathElement>, 'x' | 'y' | 'children'>; | ||
}) => React.ReactNode; | ||
/** className applied to path element. */ | ||
className?: string; | ||
/** Optionally specify the sample rate for interpolating line segments. */ | ||
sampleRate?: number; | ||
} & LinePathConfig<Datum>; | ||
|
||
export default function SplitLinePath<Datum>({ | ||
children, | ||
className, | ||
curve, | ||
defined, | ||
sampleRate, | ||
segments, | ||
x, | ||
y, | ||
styles, | ||
}: SplitLinePathProps<Datum>) { | ||
// combine data to first draw entire path | ||
const combinedSegments: Datum[] = useMemo( | ||
() => segments.reduce((flat, segmentData) => flat.concat([...segmentData]), []), | ||
[segments], | ||
); | ||
|
||
return ( | ||
<LinePath data={combinedSegments} defined={defined} curve={curve} x={x} y={y}> | ||
{({ path }) => { | ||
// use entire path to interpolate individual segments | ||
const entirePath = path(combinedSegments); | ||
const computedLineSegments = getSplitLineSegments({ | ||
path: entirePath || '', | ||
segments, | ||
sampleRate, | ||
}); | ||
|
||
return computedLineSegments.map((segment, index) => | ||
children ? ( | ||
children({ index, segment, styles: styles[index] || styles[index % styles.length] }) | ||
) : ( | ||
<LinePath | ||
key={index} | ||
className={className} | ||
data={segment} | ||
x={(d: Point) => d.x || 0} | ||
y={(d: Point) => d.y || 0} | ||
{...(styles[index] || styles[index % styles.length])} | ||
/> | ||
), | ||
); | ||
}} | ||
</LinePath> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
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