Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions src/marks/delaunay.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {create, group, path, select, Delaunay} from "d3";
import {Curve} from "../curve.js";
import {maybeTuple, maybeZ} from "../options.js";
import {constant, maybeTuple, maybeZ} from "../options.js";
import {Mark} from "../plot.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform, offset} from "../style.js";
import {applyChannelStyles, applyDirectStyles, applyFrameAnchor, applyIndirectStyles, applyTransform, offset} from "../style.js";
import {markers, applyMarkers} from "./marker.js";

const delaunayLinkDefaults = {
Expand Down Expand Up @@ -47,8 +47,8 @@ class DelaunayLink extends Mark {
super(
data,
[
{name: "x", value: x, scale: "x"},
{name: "y", value: y, scale: "y"},
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "z", value: z, optional: true}
],
options,
Expand All @@ -60,6 +60,9 @@ class DelaunayLink extends Mark {
render(index, {x, y}, channels, dimensions) {
const {x: X, y: Y, z: Z} = channels;
const {dx, dy, curve} = this;
const [cx, cy] = applyFrameAnchor(this, dimensions);
const xi = X ? i => X[i] : constant(cx);
const yi = Y ? i => Y[i] : constant(cy);
const mark = this;

function links(index) {
Expand All @@ -76,14 +79,14 @@ class DelaunayLink extends Mark {
ti = index[ti];
tj = index[tj];
newIndex.push(++i);
X1[i] = X[ti];
Y1[i] = Y[ti];
X2[i] = X[tj];
Y2[i] = Y[tj];
X1[i] = xi(ti);
Y1[i] = yi(ti);
X2[i] = xi(tj);
Y2[i] = yi(tj);
for (const k in channels) newChannels[k].push(channels[k][tj]);
}

const {halfedges, hull, triangles} = Delaunay.from(index, i => X[i], i => Y[i]);
const {halfedges, hull, triangles} = Delaunay.from(index, xi, yi);
for (let i = 0; i < halfedges.length; ++i) { // inner edges
const j = halfedges[i];
if (j > i) link(triangles[i], triangles[j]);
Expand Down Expand Up @@ -126,33 +129,37 @@ class AbstractDelaunayMark extends Mark {
super(
data,
[
{name: "x", value: x, scale: "x"},
{name: "y", value: y, scale: "y"},
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "z", value: zof(options), optional: true}
],
options,
defaults
);
}
render(index, {x, y}, {x: X, y: Y, z: Z, ...channels}, dimensions) {
render(index, {x, y}, channels, dimensions) {
const {x: X, y: Y, z: Z} = channels;
const {dx, dy} = this;
const [cx, cy] = applyFrameAnchor(this, dimensions);
const xi = X ? i => X[i] : constant(cx);
const yi = Y ? i => Y[i] : constant(cy);
const mark = this;
function mesh(render) {
return function(index) {
const delaunay = Delaunay.from(index, i => X[i], i => Y[i]);
select(this).append("path")
.datum(index[0])
.call(applyDirectStyles, mark)
.attr("d", render(delaunay, dimensions))
.call(applyChannelStyles, mark, channels);
};

function mesh(index) {
const delaunay = Delaunay.from(index, xi, yi);
select(this).append("path")
.datum(index[0])
.call(applyDirectStyles, mark)
.attr("d", mark._render(delaunay, dimensions))
.call(applyChannelStyles, mark, channels);
}

return create("svg:g")
.call(applyIndirectStyles, this, dimensions)
.call(applyTransform, x, y, offset + dx, offset + dy)
.call(Z
? g => g.selectAll().data(group(index, i => Z[i]).values()).enter().append("g").each(mesh(this._render))
: g => g.datum(index).each(mesh(this._render)))
? g => g.selectAll().data(group(index, i => Z[i]).values()).enter().append("g").each(mesh)
: g => g.datum(index).each(mesh))
.node();
}
}
Expand Down Expand Up @@ -182,8 +189,8 @@ class Voronoi extends Mark {
super(
data,
[
{name: "x", value: x, scale: "x"},
{name: "y", value: y, scale: "y"},
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "z", value: z, optional: true}
],
options,
Expand All @@ -193,9 +200,12 @@ class Voronoi extends Mark {
render(index, {x, y}, channels, dimensions) {
const {x: X, y: Y, z: Z} = channels;
const {dx, dy} = this;
const [cx, cy] = applyFrameAnchor(this, dimensions);
const xi = X ? i => X[i] : constant(cx);
const yi = Y ? i => Y[i] : constant(cy);

function cells(index) {
const delaunay = Delaunay.from(index, i => X[i], i => Y[i]);
const delaunay = Delaunay.from(index, xi, yi);
const voronoi = voronoiof(delaunay, dimensions);
select(this)
.selectAll()
Expand Down
Loading