Skip to content

Commit e0f8378

Browse files
committed
in stackY, return y = (y1+y2)/2
1 parent 76599aa commit e0f8378

File tree

6 files changed

+80
-64
lines changed

6 files changed

+80
-64
lines changed

src/marks/stack.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ export function stackBarY(data, options) {
2020
}
2121

2222
export function stackLineX(data, {position, ...options}) {
23-
const s = stackX(data, options);
24-
s[1].x = position === "center" ? s[1].xm
25-
: position === "left" ? s[1].x1
26-
: s[1].x2;
27-
return line(...s);
23+
[data, options] = stackY(data, options);
24+
options.x = position === "center" ? options.x
25+
: position === "bottom" ? options.x1
26+
: options.x2;
27+
return line(data, options);
2828
}
2929

3030
export function stackLineY(data, {position, ...options}) {
31-
const s = stackY(data, options);
32-
s[1].y = position === "center" ? s[1].ym
33-
: position === "bottom" ? s[1].y1
34-
: s[1].y2;
35-
return line(...s);
31+
[data, options] = stackY(data, options);
32+
options.y = position === "center" ? options.y
33+
: position === "bottom" ? options.y1
34+
: options.y2;
35+
return line(data, options);
3636
}

src/transforms/stack.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {InternMap, ascending, range} from "d3-array";
22
import {valueof} from "../mark";
33

44
export function stackX(data, {x, y, z, ...options}) {
5-
const [, {x: y_, y1: x1, y2: x2, ym: xm, ...rest}] = stackY(data, {x: y, y: x, z, ...options});
6-
return [data, {...rest, x1, x2, xm, y: y_, z}];
5+
const [, {x: y_, y1: x1, y2: x2, y: x_, ...rest}] = stackY(data, {x: y, y: x, z, ...options});
6+
return [data, {...rest, x1, x2, x: x_, y: y_, z}];
77
}
88

99
export function stackY(data, {x, y, z, zOrder, offset, ...options}) {
@@ -66,14 +66,13 @@ export function stackY(data, {x, y, z, zOrder, offset, ...options}) {
6666
return {index: facets === undefined ? I : index, data};
6767
};
6868

69-
const ym = Y1.map((y1, i) => (y1 + Y2[i]) / 2);
7069
return [data, {
7170
...options,
7271
transform,
7372
x: maybeLabel(X, x),
7473
y1: maybeLabel(Y1, y),
7574
y2: Y2,
76-
ym,
75+
y: (_,i) => (Y1[i] + Y2[i]) / 2,
7776
z
7877
}];
7978
}

test/output/industryUnemploymentSilhouette.svg

Lines changed: 27 additions & 27 deletions
Loading

test/output/policeDeaths.svg

Lines changed: 13 additions & 7 deletions
Loading

test/plots/caltrain.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export default async function() {
88
"B": "rgba(196,62,29,1)",
99
"N": "rgba(34,34,34,1)"
1010
});
11-
11+
1212
return Plot.plot({
1313
marks: [
14-
Plot.text([{label: "Northbound", x: -2.2}, {label: "Southbound", x: 2.2}], {
14+
Plot.text([{label: "Northbound", x: -1.65}, {label: "Southbound", x: 1.65}], {
1515
x: "x", text: "label", y: () => 3
1616
}),
1717
Plot.text(
@@ -20,20 +20,16 @@ export default async function() {
2020
x: () => 0, y: h => h,
2121
text: h => `${(h - 1) % 12 + 1}${(h % 24) >= 12 ? "p": "a"}`
2222
}),
23-
Plot.text(data, {
24-
x: Plot.stackX(data, {
25-
x: d => d.orientation === "N" ? -1 : 1,
26-
y: "hours",
27-
type: "diverging"
28-
})[1].xm.map(d => d + 0.55 * Math.sign(d)),
23+
Plot.text(...Plot.stackX(data, {
24+
x: d => d.orientation === "N" ? -1 : 1,
2925
y: "hours",
3026
text: "minutes",
3127
fill: "type"
32-
}),
33-
Plot.ruleX([-.55, .55], { strokeWidth: 0.5, stroke: "#666" })
28+
})),
29+
Plot.ruleX([-.001, .001], { strokeWidth: 0.5, stroke: "#666" })
3430
],
3531
style: { fontSize: "12px" },
36-
x: { axis: null, domain: [-7, 7] },
32+
x: { axis: null, domain: [-7, 7], transform: d => d + 0.55 * Math.sign(d) },
3733
width: 340,
3834
y: { domain: Array.from({length: 23}, (_,i) => i + 3), axis: null },
3935
height: 400,

test/plots/police-deaths.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ export default async function() {
3434
z: "race", zOrder
3535
}),
3636

37+
// dashed lines
38+
Plot.stackLineY(data.flatMap(d => d.type === "police" ? [{...d, type:"-"}, d] : [d, {...d, type:"+"}]), {
39+
x: "type", y: "value",
40+
z: "race", zOrder,
41+
stroke: "black", curve: "monotone-x", strokeWidth: 0.5,
42+
position: "center",
43+
strokeDasharray: [5, 5]
44+
}),
45+
3746
// top black lines
3847
Plot.stackLineY(data.flatMap(d => d.type === "police" ? [{...d, type:"---"}, d] : [d, {...d, type:"+++"}]), {
3948
x: "type", y: "value",
4049
z: "race", zOrder,
41-
stroke:"black", curve: "monotone-x", strokeWidth: 2
50+
stroke: "black", curve: "monotone-x", strokeWidth: 2
4251
}),
4352

4453
// bottom black line
@@ -49,10 +58,16 @@ export default async function() {
4958
}),
5059

5160
// text
52-
Plot.text(data.map(d => ({...d, type: d.type === "police" ? "--" : "++"})), {
53-
x: "type", y: Plot.stackY(data, {x: "type", y: "value", z: "race", zOrder})[1].ym,
54-
text: d => `${d.race} ${d.value}%`
55-
})
61+
Plot.text(...Plot.stackY(
62+
data.map(d => ({...d, type: d.type === "police" ? "--" : "++"})),
63+
{
64+
x: "type",
65+
y: "value",
66+
z: "race",
67+
zOrder,
68+
text: d => `${d.race} ${d.value}%`
69+
}
70+
))
5671

5772
],
5873
marginBottom: 12

0 commit comments

Comments
 (0)