Skip to content

Commit 2789a15

Browse files
committed
merge voronoi into delaunay
1 parent 1562d2f commit 2789a15

File tree

6 files changed

+96
-99
lines changed

6 files changed

+96
-99
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -984,27 +984,27 @@ Equivalent to [Plot.cell](#plotcelldata-options), except that if the **y** optio
984984
985985
### Delaunay
986986
987-
Plot provides a handful of marks for Delaunay and Voronoi diagrams (using [d3-delaunay](https://github.com/d3/d3-delaunay) and [Delaunator](https://github.com/mapbox/delaunator)). These marks require the **x** and **y** channels to be specified.
987+
[Source](./src/marks/delaunay.js) · Plot provides a handful of marks for Delaunay and Voronoi diagrams (using [d3-delaunay](https://github.com/d3/d3-delaunay) and [Delaunator](https://github.com/mapbox/delaunator)). These marks require the **x** and **y** channels to be specified.
988988
989989
#### Plot.delaunayLink(*data*, *options*)
990990
991-
[Source](./src/marks/delaunay.js) · Draws links for each edge of the Delaunay triangulation of the points given by the **x** and **y** channels.
991+
Draws links for each edge of the Delaunay triangulation of the points given by the **x** and **y** channels.
992992
993993
#### Plot.delaunayMesh(*data*, *options*)
994994
995-
[Source](./src/marks/delaunay.js) · Draws a mesh of the Delaunay triangulation of the points given by the **x** and **y** channels.
995+
Draws a mesh of the Delaunay triangulation of the points given by the **x** and **y** channels.
996996
997997
#### Plot.hull(*data*, *options*)
998998
999-
[Source](./src/marks/delaunay.js) · Draws a convex hull around the points given by the **x** and **y** channels.
999+
Draws a convex hull around the points given by the **x** and **y** channels.
10001000
10011001
#### Plot.voronoi(*data*, *options*)
10021002
1003-
[Source](./src/marks/voronoi.js) · Draws polygons for each cell of the Voronoi tesselation of the points given by the **x** and **y** channels.
1003+
Draws polygons for each cell of the Voronoi tesselation of the points given by the **x** and **y** channels.
10041004
10051005
#### Plot.voronoiMesh(*data*, *options*)
10061006
1007-
[Source](./src/marks/voronoi.js) · Draws a mesh for the cell boundaries of the Voronoi tesselation of the points given by the **x** and **y** channels.
1007+
Draws a mesh for the cell boundaries of the Voronoi tesselation of the points given by the **x** and **y** channels.
10081008
10091009
### Dot
10101010

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export {Arrow, arrow} from "./marks/arrow.js";
44
export {BarX, BarY, barX, barY} from "./marks/bar.js";
55
export {boxX, boxY} from "./marks/box.js";
66
export {Cell, cell, cellX, cellY} from "./marks/cell.js";
7-
export {delaunayLink, delaunayMesh, hull} from "./marks/delaunay.js";
7+
export {delaunayLink, delaunayMesh, hull, voronoi, voronoiMesh} from "./marks/delaunay.js";
88
export {Dot, dot, dotX, dotY, circle, hexagon} from "./marks/dot.js";
99
export {Frame, frame} from "./marks/frame.js";
1010
export {Hexgrid, hexgrid} from "./marks/hexgrid.js";
@@ -17,7 +17,6 @@ export {Text, text, textX, textY} from "./marks/text.js";
1717
export {TickX, TickY, tickX, tickY} from "./marks/tick.js";
1818
export {tree, cluster} from "./marks/tree.js";
1919
export {Vector, vector, vectorX, vectorY} from "./marks/vector.js";
20-
export {voronoi, voronoiMesh} from "./marks/voronoi.js";
2120
export {valueof, column} from "./options.js";
2221
export {filter, reverse, sort, shuffle, basic as transform, initializer} from "./transforms/basic.js";
2322
export {bin, binX, binY} from "./transforms/bin.js";

src/marks/delaunay.js

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import {Mark} from "../plot.js";
55
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform, offset} from "../style.js";
66
import {markers, applyMarkers} from "./marker.js";
77

8-
const linkDefaults = {
8+
const delaunayLinkDefaults = {
99
ariaLabel: "delaunay link",
1010
fill: "none",
1111
stroke: "currentColor",
1212
strokeMiterlimit: 1
1313
};
1414

15-
const meshDefaults = {
15+
const delaunayMeshDefaults = {
1616
ariaLabel: "delaunay mesh",
1717
fill: null,
1818
stroke: "currentColor",
@@ -23,10 +23,25 @@ const hullDefaults = {
2323
ariaLabel: "hull",
2424
fill: "none",
2525
stroke: "currentColor",
26-
strokeWidth: 1.5
26+
strokeWidth: 1.5,
27+
strokeMiterlimit: 1
28+
};
29+
30+
const voronoiDefaults = {
31+
ariaLabel: "voronoi",
32+
fill: "none",
33+
stroke: "currentColor",
34+
strokeMiterlimit: 1
2735
};
2836

29-
export class DelaunayLink extends Mark {
37+
const voronoiMeshDefaults = {
38+
ariaLabel: "voronoi mesh",
39+
fill: null,
40+
stroke: "currentColor",
41+
strokeOpacity: 0.2
42+
};
43+
44+
class DelaunayLink extends Mark {
3045
constructor(data, options = {}) {
3146
const {x, y, z, curve, tension} = options;
3247
super(
@@ -37,7 +52,7 @@ export class DelaunayLink extends Mark {
3752
{name: "z", value: z, optional: true}
3853
],
3954
options,
40-
linkDefaults
55+
delaunayLinkDefaults
4156
);
4257
this.curve = Curve(curve, tension);
4358
markers(this, options);
@@ -105,8 +120,8 @@ export class DelaunayLink extends Mark {
105120
}
106121
}
107122

108-
export class DelaunayMesh extends Mark {
109-
constructor(data, options = {}, defaults = meshDefaults) {
123+
class DelaunayMesh extends Mark {
124+
constructor(data, options = {}, defaults = delaunayMeshDefaults) {
110125
const {x, y, z, stroke} = options;
111126
super(
112127
data,
@@ -146,7 +161,7 @@ export class DelaunayMesh extends Mark {
146161
}
147162
}
148163

149-
export class Hull extends DelaunayMesh {
164+
class Hull extends DelaunayMesh {
150165
constructor(data, options = {}) {
151166
super(data, options, hullDefaults);
152167
}
@@ -155,6 +170,61 @@ export class Hull extends DelaunayMesh {
155170
}
156171
}
157172

173+
class Voronoi extends Mark {
174+
constructor(data, options = {}) {
175+
const {x, y, z} = options;
176+
super(
177+
data,
178+
[
179+
{name: "x", value: x, scale: "x"},
180+
{name: "y", value: y, scale: "y"},
181+
{name: "z", value: z, optional: true}
182+
],
183+
options,
184+
voronoiDefaults
185+
);
186+
}
187+
render(index, {x, y}, channels, dimensions) {
188+
const {x: X, y: Y, z: Z} = channels;
189+
const {dx, dy} = this;
190+
191+
function cells(index) {
192+
const delaunay = Delaunay.from(index, i => X[i], i => Y[i]);
193+
const voronoi = voronoiof(delaunay, dimensions);
194+
select(this)
195+
.selectAll()
196+
.data(index)
197+
.enter()
198+
.append("path")
199+
.call(applyDirectStyles, this)
200+
.attr("d", (_, i) => voronoi.renderCell(i))
201+
.call(applyChannelStyles, this, channels);
202+
}
203+
204+
return create("svg:g")
205+
.call(applyIndirectStyles, this, dimensions)
206+
.call(applyTransform, x, y, offset + dx, offset + dy)
207+
.call(Z
208+
? g => g.selectAll().data(group(index, i => Z[i]).values()).enter().append("g").each(cells)
209+
: g => g.datum(index).each(cells))
210+
.node();
211+
}
212+
}
213+
214+
function voronoiof(delaunay, dimensions) {
215+
const {width, height, marginTop, marginRight, marginBottom, marginLeft} = dimensions;
216+
return delaunay.voronoi([marginLeft, marginTop, width - marginRight, height - marginBottom]);
217+
}
218+
219+
class VoronoiMesh extends DelaunayMesh {
220+
constructor(data, options) {
221+
super(data, options, voronoiMeshDefaults);
222+
}
223+
_render(delaunay, dimensions) {
224+
return voronoiof(delaunay, dimensions).render();
225+
}
226+
}
227+
158228
export function delaunayMark(DelaunayMark, data, {x, y, ...options} = {}) {
159229
([x, y] = maybeTuple(x, y));
160230
return new DelaunayMark(data, {...options, x, y});
@@ -171,3 +241,12 @@ export function delaunayMesh(data, options) {
171241
export function hull(data, options) {
172242
return delaunayMark(Hull, data, options);
173243
}
244+
245+
// TODO voronoiX, voronoiY?
246+
export function voronoi(data, options) {
247+
return delaunayMark(Voronoi, data, options);
248+
}
249+
250+
export function voronoiMesh(data, options) {
251+
return delaunayMark(VoronoiMesh, data, options);
252+
}

src/marks/voronoi.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

test/output/penguinCulmenDelaunaySpecies.svg

Lines changed: 1 addition & 1 deletion
Loading

test/output/penguinCulmenVoronoi.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)