|
| 1 | +import {create, group, path, select, Delaunay} from "d3"; |
| 2 | +import {Curve} from "../curve.js"; |
| 3 | +import {maybeTuple, maybeZ} from "../options.js"; |
| 4 | +import {Mark} from "../plot.js"; |
| 5 | +import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform, offset} from "../style.js"; |
| 6 | +import {markers, applyMarkers} from "./marker.js"; |
| 7 | + |
| 8 | +const delaunayLinkDefaults = { |
| 9 | + ariaLabel: "delaunay link", |
| 10 | + fill: "none", |
| 11 | + stroke: "currentColor", |
| 12 | + strokeMiterlimit: 1 |
| 13 | +}; |
| 14 | + |
| 15 | +const delaunayMeshDefaults = { |
| 16 | + ariaLabel: "delaunay mesh", |
| 17 | + fill: null, |
| 18 | + stroke: "currentColor", |
| 19 | + strokeOpacity: 0.2 |
| 20 | +}; |
| 21 | + |
| 22 | +const hullDefaults = { |
| 23 | + ariaLabel: "hull", |
| 24 | + fill: "none", |
| 25 | + stroke: "currentColor", |
| 26 | + strokeWidth: 1.5, |
| 27 | + strokeMiterlimit: 1 |
| 28 | +}; |
| 29 | + |
| 30 | +const voronoiDefaults = { |
| 31 | + ariaLabel: "voronoi", |
| 32 | + fill: "none", |
| 33 | + stroke: "currentColor", |
| 34 | + strokeMiterlimit: 1 |
| 35 | +}; |
| 36 | + |
| 37 | +const voronoiMeshDefaults = { |
| 38 | + ariaLabel: "voronoi mesh", |
| 39 | + fill: null, |
| 40 | + stroke: "currentColor", |
| 41 | + strokeOpacity: 0.2 |
| 42 | +}; |
| 43 | + |
| 44 | +class DelaunayLink extends Mark { |
| 45 | + constructor(data, options = {}) { |
| 46 | + const {x, y, z, curve, tension} = options; |
| 47 | + super( |
| 48 | + data, |
| 49 | + [ |
| 50 | + {name: "x", value: x, scale: "x"}, |
| 51 | + {name: "y", value: y, scale: "y"}, |
| 52 | + {name: "z", value: z, optional: true} |
| 53 | + ], |
| 54 | + options, |
| 55 | + delaunayLinkDefaults |
| 56 | + ); |
| 57 | + this.curve = Curve(curve, tension); |
| 58 | + markers(this, options); |
| 59 | + } |
| 60 | + render(index, {x, y}, channels, dimensions) { |
| 61 | + const {x: X, y: Y, z: Z} = channels; |
| 62 | + const {dx, dy, curve} = this; |
| 63 | + const mark = this; |
| 64 | + |
| 65 | + function links(index) { |
| 66 | + let i = -1; |
| 67 | + const newIndex = []; |
| 68 | + const newChannels = {}; |
| 69 | + for (const k in channels) newChannels[k] = []; |
| 70 | + const X1 = []; |
| 71 | + const X2 = []; |
| 72 | + const Y1 = []; |
| 73 | + const Y2 = []; |
| 74 | + |
| 75 | + function link(ti, tj) { |
| 76 | + ti = index[ti]; |
| 77 | + tj = index[tj]; |
| 78 | + newIndex.push(++i); |
| 79 | + X1[i] = X[ti]; |
| 80 | + Y1[i] = Y[ti]; |
| 81 | + X2[i] = X[tj]; |
| 82 | + Y2[i] = Y[tj]; |
| 83 | + for (const k in channels) newChannels[k].push(channels[k][tj]); |
| 84 | + } |
| 85 | + |
| 86 | + const {halfedges, hull, triangles} = Delaunay.from(index, i => X[i], i => Y[i]); |
| 87 | + for (let i = 0; i < halfedges.length; ++i) { // inner edges |
| 88 | + const j = halfedges[i]; |
| 89 | + if (j > i) link(triangles[i], triangles[j]); |
| 90 | + } |
| 91 | + for (let i = 0; i < hull.length; ++i) { // convex hull |
| 92 | + link(hull[i], hull[(i + 1) % hull.length]); |
| 93 | + } |
| 94 | + |
| 95 | + select(this) |
| 96 | + .selectAll() |
| 97 | + .data(newIndex) |
| 98 | + .join("path") |
| 99 | + .call(applyDirectStyles, mark) |
| 100 | + .attr("d", i => { |
| 101 | + const p = path(); |
| 102 | + const c = curve(p); |
| 103 | + c.lineStart(); |
| 104 | + c.point(X1[i], Y1[i]); |
| 105 | + c.point(X2[i], Y2[i]); |
| 106 | + c.lineEnd(); |
| 107 | + return p; |
| 108 | + }) |
| 109 | + .call(applyChannelStyles, mark, newChannels) |
| 110 | + .call(applyMarkers, mark, newChannels); |
| 111 | + } |
| 112 | + |
| 113 | + return create("svg:g") |
| 114 | + .call(applyIndirectStyles, this, dimensions) |
| 115 | + .call(applyTransform, x, y, offset + dx, offset + dy) |
| 116 | + .call(Z |
| 117 | + ? g => g.selectAll().data(group(index, i => Z[i]).values()).enter().append("g").each(links) |
| 118 | + : g => g.datum(index).each(links)) |
| 119 | + .node(); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +class AbstractDelaunayMark extends Mark { |
| 124 | + constructor(data, options = {}, defaults, zof = ({z}) => z) { |
| 125 | + const {x, y} = options; |
| 126 | + super( |
| 127 | + data, |
| 128 | + [ |
| 129 | + {name: "x", value: x, scale: "x"}, |
| 130 | + {name: "y", value: y, scale: "y"}, |
| 131 | + {name: "z", value: zof(options), optional: true} |
| 132 | + ], |
| 133 | + options, |
| 134 | + defaults |
| 135 | + ); |
| 136 | + } |
| 137 | + render(index, {x, y}, {x: X, y: Y, z: Z, ...channels}, dimensions) { |
| 138 | + const {dx, dy} = this; |
| 139 | + const mark = this; |
| 140 | + function mesh(render) { |
| 141 | + return function(index) { |
| 142 | + const delaunay = Delaunay.from(index, i => X[i], i => Y[i]); |
| 143 | + select(this).append("path") |
| 144 | + .datum(index[0]) |
| 145 | + .call(applyDirectStyles, mark) |
| 146 | + .attr("d", render(delaunay, dimensions)) |
| 147 | + .call(applyChannelStyles, mark, channels); |
| 148 | + }; |
| 149 | + } |
| 150 | + return create("svg:g") |
| 151 | + .call(applyIndirectStyles, this, dimensions) |
| 152 | + .call(applyTransform, x, y, offset + dx, offset + dy) |
| 153 | + .call(Z |
| 154 | + ? g => g.selectAll().data(group(index, i => Z[i]).values()).enter().append("g").each(mesh(this._render)) |
| 155 | + : g => g.datum(index).each(mesh(this._render))) |
| 156 | + .node(); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +class DelaunayMesh extends AbstractDelaunayMark { |
| 161 | + constructor(data, options = {}) { |
| 162 | + super(data, options, delaunayMeshDefaults); |
| 163 | + this.fill = "none"; |
| 164 | + } |
| 165 | + _render(delaunay) { |
| 166 | + return delaunay.render(); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +class Hull extends AbstractDelaunayMark { |
| 171 | + constructor(data, options = {}) { |
| 172 | + super(data, options, hullDefaults, maybeZ); |
| 173 | + } |
| 174 | + _render(delaunay) { |
| 175 | + return delaunay.renderHull(); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +class Voronoi extends Mark { |
| 180 | + constructor(data, options = {}) { |
| 181 | + const {x, y, z} = options; |
| 182 | + super( |
| 183 | + data, |
| 184 | + [ |
| 185 | + {name: "x", value: x, scale: "x"}, |
| 186 | + {name: "y", value: y, scale: "y"}, |
| 187 | + {name: "z", value: z, optional: true} |
| 188 | + ], |
| 189 | + options, |
| 190 | + voronoiDefaults |
| 191 | + ); |
| 192 | + } |
| 193 | + render(index, {x, y}, channels, dimensions) { |
| 194 | + const {x: X, y: Y, z: Z} = channels; |
| 195 | + const {dx, dy} = this; |
| 196 | + |
| 197 | + function cells(index) { |
| 198 | + const delaunay = Delaunay.from(index, i => X[i], i => Y[i]); |
| 199 | + const voronoi = voronoiof(delaunay, dimensions); |
| 200 | + select(this) |
| 201 | + .selectAll() |
| 202 | + .data(index) |
| 203 | + .enter() |
| 204 | + .append("path") |
| 205 | + .call(applyDirectStyles, this) |
| 206 | + .attr("d", (_, i) => voronoi.renderCell(i)) |
| 207 | + .call(applyChannelStyles, this, channels); |
| 208 | + } |
| 209 | + |
| 210 | + return create("svg:g") |
| 211 | + .call(applyIndirectStyles, this, dimensions) |
| 212 | + .call(applyTransform, x, y, offset + dx, offset + dy) |
| 213 | + .call(Z |
| 214 | + ? g => g.selectAll().data(group(index, i => Z[i]).values()).enter().append("g").each(cells) |
| 215 | + : g => g.datum(index).each(cells)) |
| 216 | + .node(); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +class VoronoiMesh extends AbstractDelaunayMark { |
| 221 | + constructor(data, options) { |
| 222 | + super(data, options, voronoiMeshDefaults); |
| 223 | + this.fill = "none"; |
| 224 | + } |
| 225 | + _render(delaunay, dimensions) { |
| 226 | + return voronoiof(delaunay, dimensions).render(); |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +function voronoiof(delaunay, dimensions) { |
| 231 | + const {width, height, marginTop, marginRight, marginBottom, marginLeft} = dimensions; |
| 232 | + return delaunay.voronoi([marginLeft, marginTop, width - marginRight, height - marginBottom]); |
| 233 | +} |
| 234 | + |
| 235 | +function delaunayMark(DelaunayMark, data, {x, y, ...options} = {}) { |
| 236 | + ([x, y] = maybeTuple(x, y)); |
| 237 | + return new DelaunayMark(data, {...options, x, y}); |
| 238 | +} |
| 239 | + |
| 240 | +export function delaunayLink(data, options) { |
| 241 | + return delaunayMark(DelaunayLink, data, options); |
| 242 | +} |
| 243 | + |
| 244 | +export function delaunayMesh(data, options) { |
| 245 | + return delaunayMark(DelaunayMesh, data, options); |
| 246 | +} |
| 247 | + |
| 248 | +export function hull(data, options) { |
| 249 | + return delaunayMark(Hull, data, options); |
| 250 | +} |
| 251 | + |
| 252 | +export function voronoi(data, options) { |
| 253 | + return delaunayMark(Voronoi, data, options); |
| 254 | +} |
| 255 | + |
| 256 | +export function voronoiMesh(data, options) { |
| 257 | + return delaunayMark(VoronoiMesh, data, options); |
| 258 | +} |
0 commit comments