-
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.
Merge pull request #512 from hshoff/chris--typescript-vx-voronoi
typescript(vx-voronoi): re-write package in TypeScript
- Loading branch information
Showing
9 changed files
with
66 additions
and
46 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
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
|
||
export type VoronoiPolygonProps = { | ||
/** Override render function which is provided polygon and generated path. */ | ||
children?: ({ path, polygon }: { path: string; polygon: [number, number][] }) => React.ReactNode; | ||
/** className to apply to path element. */ | ||
className?: string; | ||
/** Array of coordinate arrays for the polygon (e.g., [[x,y], [x1,y1], ...]), used to generate polygon path. */ | ||
polygon?: [number, number][]; | ||
}; | ||
|
||
export default function VoronoiPolygon({ | ||
polygon, | ||
className, | ||
children, | ||
...restProps | ||
}: VoronoiPolygonProps & Omit<React.SVGProps<SVGPathElement>, keyof VoronoiPolygonProps>) { | ||
if (!polygon) return null; | ||
const path = `M${polygon.join('L')}Z`; | ||
if (children) return <>{children({ path, polygon })}</>; | ||
|
||
return <path className={cx('vx-voronoi-polygon', className)} d={path} {...restProps} />; | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { voronoi as d3Voronoi } from 'd3-voronoi'; | ||
|
||
const CLIP_PADDING = 1; | ||
|
||
interface Config<Datum> { | ||
width?: number; | ||
height?: number; | ||
x?: (d: Datum) => number; | ||
y?: (d: Datum) => number; | ||
} | ||
|
||
/** | ||
* Returns a configured d3 voronoi `layout`. calling `layout(data)` returns a voronoi *diagram*. | ||
* Alternatively call `layout.polygons(data)`, `layout.triangles(data)`, `layout.links(data)` | ||
*/ | ||
export default function getVoronoi<Datum>({ width = 0, height = 0, x, y }: Config<Datum>) { | ||
const voronoi = d3Voronoi<Datum>(); | ||
|
||
if (x) voronoi.x(x); | ||
if (y) voronoi.y(y); | ||
|
||
voronoi.extent([ | ||
[-CLIP_PADDING, -CLIP_PADDING], | ||
[width + CLIP_PADDING, height + CLIP_PADDING], | ||
]); | ||
|
||
return voronoi; | ||
} |
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