Skip to content

Commit 8cb7815

Browse files
mbostockFil
andauthored
one-dimensional vectors (#782)
* one-dimensional vectors * documentation and (slightly contrived) test plot for vectorX/Y (#783) Co-authored-by: Philippe Rivière <[email protected]>
1 parent dcd9ec4 commit 8cb7815

File tree

6 files changed

+188
-2
lines changed

6 files changed

+188
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,14 @@ Plot.vector(wind, {x: "longitude", y: "latitude", length: "speed", rotate: "dire
12271227

12281228
Returns a new vector with the given *data* and *options*. If neither the **x** nor **y** options are specified, *data* is assumed to be an array of pairs [[*x₀*, *y₀*], [*x₁*, *y₁*], [*x₂*, *y₂*], …] such that **x** = [*x₀*, *x₁*, *x₂*, …] and **y** = [*y₀*, *y₁*, *y₂*, …].
12291229

1230+
#### Plot.vectorX(*data*, *options*)
1231+
1232+
Equivalent to Plot.vector except that if the **x** option is not specified, it defaults to the identity function and assumes that *data* = [*x₀*, *x₁*, *x₂*, …].
1233+
1234+
#### Plot.vectorY(*data*, *options*)
1235+
1236+
Equivalent to Plot.vector except that if the **y** option is not specified, it defaults to the identity function and assumes that *data* = [*y₀*, *y₁*, *y₂*, …].
1237+
12301238
## Decorations
12311239

12321240
Decorations are static marks that do not represent data. Currently this includes only [Plot.frame](#frame), although internally Plot’s axes are implemented as decorations and may in the future be exposed here for more flexible configuration.

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export {Rect, rect, rectX, rectY} from "./marks/rect.js";
1313
export {RuleX, RuleY, ruleX, ruleY} from "./marks/rule.js";
1414
export {Text, text, textX, textY} from "./marks/text.js";
1515
export {TickX, TickY, tickX, tickY} from "./marks/tick.js";
16-
export {Vector, vector} from "./marks/vector.js";
16+
export {Vector, vector, vectorX, vectorY} from "./marks/vector.js";
1717
export {valueof} from "./options.js";
1818
export {filter, reverse, sort, shuffle} from "./transforms/basic.js";
1919
export {bin, binX, binY} from "./transforms/bin.js";

src/marks/vector.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {create} from "d3";
22
import {radians} from "../math.js";
3-
import {maybeFrameAnchor, maybeNumberChannel, maybeTuple, keyword} from "../options.js";
3+
import {maybeFrameAnchor, maybeNumberChannel, maybeTuple, keyword, identity} from "../options.js";
44
import {Mark} from "../plot.js";
55
import {applyChannelStyles, applyDirectStyles, applyFrameAnchor, applyIndirectStyles, applyTransform, offset} from "../style.js";
66

@@ -65,3 +65,11 @@ export function vector(data, {x, y, ...options} = {}) {
6565
if (options.frameAnchor === undefined) ([x, y] = maybeTuple(x, y));
6666
return new Vector(data, {...options, x, y});
6767
}
68+
69+
export function vectorX(data, {x = identity, ...options} = {}) {
70+
return new Vector(data, {...options, x});
71+
}
72+
73+
export function vectorY(data, {y = identity, ...options} = {}) {
74+
return new Vector(data, {...options, y});
75+
}

test/output/caltrainDirection.svg

Lines changed: 142 additions & 0 deletions
Loading

test/plots/caltrain-direction.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
export default async function() {
5+
const caltrain = await d3.csv("data/caltrain.csv");
6+
return Plot.plot({
7+
x: {
8+
tickFormat: "%I %p"
9+
},
10+
color: {
11+
domain: "NLB",
12+
range: ["currentColor", "peru", "brown"]
13+
},
14+
facet: {
15+
data: caltrain,
16+
label: null,
17+
y: "type"
18+
},
19+
marks: [
20+
Plot.vectorX(caltrain, {
21+
x: d => new Date(Date.UTC(2000, 0, 1, d.hours, d.minutes)),
22+
stroke: "type",
23+
rotate: d => d.orientation === "N" ? 0 : 180
24+
})
25+
]
26+
});
27+
}

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export {default as ballotStatusRace} from "./ballot-status-race.js";
2525
export {default as beckerBarley} from "./becker-barley.js";
2626
export {default as boxplot} from "./boxplot.js";
2727
export {default as caltrain} from "./caltrain.js";
28+
export {default as caltrainDirection} from "./caltrain-direction.js";
2829
export {default as carsMpg} from "./cars-mpg.js";
2930
export {default as carsParcoords} from "./cars-parcoords.js";
3031
export {default as clamp} from "./clamp.js";

0 commit comments

Comments
 (0)