Skip to content

Commit 164fb66

Browse files
authored
set context.path early (#2252)
* Set context.path early * adopt context.path * don't scale twice! adds a unit test * define path with projection * pretty * better test
1 parent 7d18179 commit 164fb66

File tree

6 files changed

+248
-22
lines changed

6 files changed

+248
-22
lines changed

src/marks/line.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {geoPath, line as shapeLine} from "d3";
1+
import {line as shapeLine} from "d3";
22
import {create} from "../context.js";
33
import {curveAuto, maybeCurveAuto} from "../curve.js";
44
import {Mark} from "../mark.js";
@@ -67,7 +67,7 @@ export class Line extends Mark {
6767
.attr(
6868
"d",
6969
curve === curveAuto && context.projection
70-
? sphereLine(context.projection, X, Y)
70+
? sphereLine(context.path(), X, Y)
7171
: shapeLine()
7272
.curve(curve)
7373
.defined((i) => i >= 0)
@@ -79,8 +79,7 @@ export class Line extends Mark {
7979
}
8080
}
8181

82-
function sphereLine(projection, X, Y) {
83-
const path = geoPath(projection);
82+
function sphereLine(path, X, Y) {
8483
X = coerceNumbers(X);
8584
Y = coerceNumbers(Y);
8685
return (I) => {

src/marks/link.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {geoPath, pathRound as path} from "d3";
1+
import {pathRound as path} from "d3";
22
import {create} from "../context.js";
33
import {curveAuto, maybeCurveAuto} from "../curve.js";
44
import {Mark} from "../mark.js";
@@ -52,7 +52,7 @@ export class Link extends Mark {
5252
.attr(
5353
"d",
5454
curve === curveAuto && context.projection
55-
? sphereLink(context.projection, X1, Y1, X2, Y2)
55+
? sphereLink(context.path(), X1, Y1, X2, Y2)
5656
: (i) => {
5757
const p = path();
5858
const c = curve(p);
@@ -70,8 +70,7 @@ export class Link extends Mark {
7070
}
7171
}
7272

73-
function sphereLink(projection, X1, Y1, X2, Y2) {
74-
const path = geoPath(projection);
73+
function sphereLink(path, X1, Y1, X2, Y2) {
7574
X1 = coerceNumbers(X1);
7675
Y1 = coerceNumbers(Y1);
7776
X2 = coerceNumbers(X2);

src/plot.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ export function plot(options = {}) {
159159
context.className = className;
160160
context.projection = createProjection(options, subdimensions);
161161

162+
// A path generator for marks that want to draw GeoJSON.
163+
context.path = function () {
164+
return geoPath(this.projection ?? xyProjection(scales));
165+
};
166+
162167
// Allows e.g. the axis mark to determine faceting lazily.
163168
context.filterFacets = (data, channels) => {
164169
return facetFilter(facets, {channels, groups: facetGroups(data, channels)});
@@ -236,11 +241,6 @@ export function plot(options = {}) {
236241
facetTranslate = facetTranslator(fx, fy, dimensions);
237242
}
238243

239-
// A path generator for marks that want to draw GeoJSON.
240-
context.path = function () {
241-
return geoPath(this.projection ?? xyProjection(scales));
242-
};
243-
244244
// Compute value objects, applying scales and projection as needed.
245245
for (const [mark, state] of stateByMark) {
246246
state.values = mark.scale(state.channels, scales, context);

src/transforms/centroid.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {geoCentroid as GeoCentroid, geoPath} from "d3";
1+
import {geoCentroid as GeoCentroid} from "d3";
22
import {memoize1} from "../memoize.js";
33
import {identity, valueof} from "../options.js";
44
import {initializer} from "./basic.js";
@@ -9,20 +9,17 @@ export function centroid({geometry = identity, ...options} = {}) {
99
// Suppress defaults for x and y since they will be computed by the initializer.
1010
// Propagate the (memoized) geometry channel in case it’s still needed.
1111
{...options, x: null, y: null, geometry: {transform: getG}},
12-
(data, facets, channels, scales, dimensions, {projection}) => {
12+
(data, facets, channels, scales, dimensions, context) => {
1313
const G = getG(data);
1414
const n = G.length;
1515
const X = new Float64Array(n);
1616
const Y = new Float64Array(n);
17-
const path = geoPath(projection);
18-
for (let i = 0; i < n; ++i) [X[i], Y[i]] = path.centroid(G[i]);
17+
const {centroid} = context.path();
18+
for (let i = 0; i < n; ++i) [X[i], Y[i]] = centroid(G[i]);
1919
return {
2020
data,
2121
facets,
22-
channels: {
23-
x: {value: X, scale: projection == null ? "x" : null, source: null},
24-
y: {value: Y, scale: projection == null ? "y" : null, source: null}
25-
}
22+
channels: {x: {value: X, scale: null, source: null}, y: {value: Y, scale: null, source: null}}
2623
};
2724
}
2825
);

0 commit comments

Comments
 (0)