Skip to content

Commit 9171abf

Browse files
committed
maybeDasharrayChannel identifies arrays of numbers (possibly joined by white space or commas) as constant
1 parent e280e2b commit 9171abf

File tree

6 files changed

+195
-5
lines changed

6 files changed

+195
-5
lines changed

src/style.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ export function applyFrameAnchor({frameAnchor}, {width, height, marginTop, margi
366366
];
367367
}
368368

369-
function maybeDasharrayChannel(channel) {
370-
return typeof channel === "function" ? [channel] : [, channel];
369+
// A constant stroke-dasharray can be specified as an array of numbers
370+
// or as a string joining numbers with white space or comma
371+
function maybeDasharrayChannel(value) {
372+
if (value == null) return [undefined, "none"];
373+
return (typeof value === "string" && value.match(/^([\d.]+(\s+|,)){1,}[\d.]+$/) || Array.isArray(value) && typeof value[0] === "number") ? [undefined, `${value}`] : [value, undefined];
371374
}
Lines changed: 156 additions & 0 deletions
Loading

test/plots/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ export {default as stargazersBinned} from "./stargazers-binned.js";
211211
export {default as stargazersHourly} from "./stargazers-hourly.js";
212212
export {default as stargazersHourlyGroup} from "./stargazers-hourly-group.js";
213213
export {default as stocksIndex} from "./stocks-index.js";
214+
export {default as strokeDasharrayConstant} from "./strokedasharray-constant.js";
215+
export {default as strokeDasharrayVariable} from "./strokedasharray-variable.js";
214216
export {default as thisIsJustToSay} from "./this-is-just-to-say.js";
215217
export {default as trafficHorizon} from "./traffic-horizon.js";
216218
export {default as travelersYearOverYear} from "./travelers-year-over-year.js";
@@ -229,7 +231,6 @@ export {default as usPresidentialElection2020} from "./us-presidential-election-
229231
export {default as usPresidentialForecast2016} from "./us-presidential-forecast-2016.js";
230232
export {default as usRetailSales} from "./us-retail-sales.js";
231233
export {default as usStatePopulationChange} from "./us-state-population-change.js";
232-
export {default as variableStrokeDasharray} from "./variable-strokedasharray.js";
233234
export {default as vectorField} from "./vector-field.js";
234235
export {default as vectorFrame} from "./vector-frame.js";
235236
export {default as wealthBritainBar} from "./wealth-britain-bar.js";

test/plots/variable-strokedasharray.js renamed to test/plots/strokedasharray-constant.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export default async function() {
2020
x2: 1,
2121
y1: 0,
2222
y2: 1,
23-
strokeDasharray: d => d.strokeDasharray,
24-
strokeDashoffset: d => d.strokeDashoffset,
23+
strokeDasharray: "1,2,3",
24+
strokeDashoffset: "strokeDashoffset",
2525
bend: true,
2626
headLength: 0
2727
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as Plot from "@observablehq/plot";
2+
3+
const data = Array.from(["none", "10 5", [20, 3], [30, 5, 10, 10], null],
4+
(strokeDasharray) => Array.from([0, 2, 20, 60, NaN],
5+
(strokeDashoffset) => ({strokeDasharray, strokeDashoffset})
6+
)
7+
).flat();
8+
9+
export default async function() {
10+
return Plot.plot({
11+
height: 640,
12+
x: {inset: 10},
13+
y: {inset: 10},
14+
axis: null,
15+
facet: {data, x: "strokeDasharray", y: "strokeDashoffset"},
16+
marks: [
17+
Plot.frame(),
18+
Plot.arrow(data, {
19+
x1: 0,
20+
x2: 1,
21+
y1: 0,
22+
y2: 1,
23+
strokeDasharray: "strokeDasharray",
24+
strokeDashoffset: "strokeDashoffset",
25+
bend: true,
26+
headLength: 0
27+
})
28+
]
29+
});
30+
}

0 commit comments

Comments
 (0)