Skip to content

curve: "auto" (line + projection) #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 6, 2022
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2836,8 +2836,9 @@ The following named curve methods are supported:
* *step* - a piecewise constant function where *y* changes at the midpoint of *x*
* *step-after* - a piecewise constant function where *y* changes after *x*
* *step-before* - a piecewise constant function where *x* changes after *y*
* *projection* - use the (possibly spherical) [projection](#projection-options)

If *curve* is a function, it will be invoked with a given *context* in the same fashion as a [D3 curve factory](https://github.com/d3/d3-shape/blob/main/README.md#custom-curves).
If *curve* is a function, it will be invoked with a given *context* in the same fashion as a [D3 curve factory](https://github.com/d3/d3-shape/blob/main/README.md#custom-curves). The *projection* curve is only available for the [line mark](#line) and is typically used in conjunction with a spherical [projection](#projection-options) to interpolate along [geodesics](https://en.wikipedia.org/wiki/Geodesic).

The tension option only has an effect on bundle, cardinal and Catmull–Rom splines (*bundle*, *cardinal*, *cardinal-open*, *cardinal-closed*, *catmull-rom*, *catmull-rom-open*, and *catmull-rom-closed*). For bundle splines, it corresponds to [beta](https://github.com/d3/d3-shape/blob/main/README.md#curveBundle_beta); for cardinal splines, [tension](https://github.com/d3/d3-shape/blob/main/README.md#curveCardinal_tension); for Catmull–Rom splines, [alpha](https://github.com/d3/d3-shape/blob/main/README.md#curveCatmullRom_alpha).

Expand Down
55 changes: 45 additions & 10 deletions src/marks/line.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {line as shapeLine} from "d3";
import {geoPath, line as shapeLine} from "d3";
import {create} from "../context.js";
import {Curve} from "../curve.js";
import {indexOf, identity, maybeTuple, maybeZ} from "../options.js";
import {Mark} from "../plot.js";
import {coerceNumbers} from "../scales.js";
import {
applyDirectStyles,
applyIndirectStyles,
Expand All @@ -23,28 +24,40 @@ const defaults = {
strokeMiterlimit: 1
};

const curveProjection = Symbol("projection");

// For the “projection” curve, return a symbol instead of a curve
// implementation; we’ll use d3.geoPath instead of d3.line to render.
function LineCurve({curve, tension}) {
return typeof curve !== "function" && `${curve}`.toLowerCase() === "projection"
? curveProjection
: Curve(curve, tension);
}

export class Line extends Mark {
constructor(data, options = {}) {
const {x, y, z, curve, tension} = options;
const {x, y, z} = options;
const curve = LineCurve(options);
super(
data,
{
x: {value: x, scale: "x"},
y: {value: y, scale: "y"},
x: {value: x, scale: curve === curveProjection ? undefined : "x"}, // unscaled if projected
y: {value: y, scale: curve === curveProjection ? undefined : "y"}, // unscaled if projected
Comment on lines +44 to +45
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #1165

z: {value: maybeZ(options), optional: true}
},
options,
defaults
);
this.z = z;
this.curve = Curve(curve, tension);
this.curve = curve;
markers(this, options);
}
filter(index) {
return index;
}
render(index, scales, channels, dimensions, context) {
const {x: X, y: Y} = channels;
const {curve} = this;
return create("svg:g", context)
.call(applyIndirectStyles, this, scales, dimensions, context)
.call(applyTransform, this, scales)
Expand All @@ -59,17 +72,39 @@ export class Line extends Mark {
.call(applyGroupedMarkers, this, channels)
.attr(
"d",
shapeLine()
.curve(this.curve)
.defined((i) => i >= 0)
.x((i) => X[i])
.y((i) => Y[i])
curve === curveProjection
? sphereLine(context.projection, X, Y)
: shapeLine()
.curve(curve)
.defined((i) => i >= 0)
.x((i) => X[i])
.y((i) => Y[i])
)
)
.node();
}
}

function sphereLine(projection, X, Y) {
const path = geoPath(projection);
X = coerceNumbers(X);
Y = coerceNumbers(Y);
return (I) => {
let line = [];
const lines = [line];
for (const i of I) {
// Check for undefined value; see groupIndex.
if (i === -1) {
line = [];
lines.push(line);
} else {
line.push([X[i], Y[i]]);
}
}
return path({type: "MultiLineString", coordinates: lines});
};
}

/** @jsdoc line */
export function line(data, options = {}) {
let {x, y, ...remainingOptions} = options;
Expand Down
3 changes: 3 additions & 0 deletions test/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ https://www.flother.is/2017/olympic-games-data/
## barley.csv
http://search.r-project.org/R/library/lattice/html/barley.html

## beagle.csv
https://observablehq.com/@bmschmidt/data-driven-projections-darwins-world

## bls-metro-unemployment.csv
Bureau of Labor Statistics
https://www.bls.gov/
Expand Down
Loading