Skip to content

Commit 801b730

Browse files
Filmbostock
andauthored
Smarter choice of lineX vs lineY vs line, for better tooltips (#1558)
Co-authored-by: Mike Bostock <[email protected]>
1 parent 42f5d9f commit 801b730

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

src/marks/auto.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,21 @@ export function autoSpec(data, options) {
9090
colorMode = "stroke";
9191
break;
9292
case "line":
93-
markImpl = X && Y ? line : X ? lineX : lineY; // 1d line by index
93+
markImpl =
94+
(X && Y) || xReduce || yReduce // same logic as area (see below), but default to line
95+
? yZero || yReduce || (X && isMonotonic(X))
96+
? lineY
97+
: xZero || xReduce || (Y && isMonotonic(Y))
98+
? lineX
99+
: line
100+
: X // 1d line by index
101+
? lineX
102+
: lineY;
94103
colorMode = "stroke";
95104
if (isHighCardinality(C)) Z = null; // TODO only if z not set by user
96105
break;
97106
case "area":
98-
markImpl = yZero ? areaY : xZero || (Y && isMonotonic(Y)) ? areaX : areaY; // favor areaY if unsure
107+
markImpl = !(yZero || yReduce) && (xZero || xReduce || (Y && isMonotonic(Y))) ? areaX : areaY; // favor areaY if unsure
99108
colorMode = "fill";
100109
if (isHighCardinality(C)) Z = null; // TODO only if z not set by user
101110
break;

test/marks/auto-test.js

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,39 @@ it("Plot.autoSpec makes a bar chart from an ordinal dimension", () => {
5353
});
5454
});
5555

56-
it("Plot.autoSpec makes a line from a monotonic dimension", () => {
56+
it("Plot.autoSpec makes a lineY from monotonic x", () => {
5757
const data = [
5858
{date: 1, value: 1},
59+
{date: 2, value: 0},
60+
{date: 3, value: 38}
61+
];
62+
assert.deepStrictEqual(Plot.autoSpec(data, {x: "date", y: "value"}), {
63+
fx: null,
64+
fy: null,
65+
x: {value: "date", reduce: null, zero: false},
66+
y: {value: "value", reduce: null, zero: false},
67+
color: {value: null, reduce: null},
68+
size: {value: null, reduce: null},
69+
mark: "line",
70+
markImpl: "lineY",
71+
markOptions: {
72+
fx: undefined,
73+
fy: undefined,
74+
x: Object.assign([1, 2, 3], {label: "date"}),
75+
y: Object.assign([1, 0, 38], {label: "value"}),
76+
stroke: undefined,
77+
z: undefined,
78+
r: undefined
79+
},
80+
transformImpl: undefined,
81+
transformOptions: {stroke: undefined, r: undefined},
82+
colorMode: "stroke"
83+
});
84+
});
85+
86+
it("Plot.autoSpec makes a lineY from monotonic x and monotonic y", () => {
87+
const data = [
88+
{date: 1, value: 0},
5989
{date: 2, value: 1},
6090
{date: 3, value: 38}
6191
];
@@ -67,12 +97,72 @@ it("Plot.autoSpec makes a line from a monotonic dimension", () => {
6797
color: {value: null, reduce: null},
6898
size: {value: null, reduce: null},
6999
mark: "line",
70-
markImpl: "line",
100+
markImpl: "lineY",
71101
markOptions: {
72102
fx: undefined,
73103
fy: undefined,
74104
x: Object.assign([1, 2, 3], {label: "date"}),
75-
y: Object.assign([1, 1, 38], {label: "value"}),
105+
y: Object.assign([0, 1, 38], {label: "value"}),
106+
stroke: undefined,
107+
z: undefined,
108+
r: undefined
109+
},
110+
transformImpl: undefined,
111+
transformOptions: {stroke: undefined, r: undefined},
112+
colorMode: "stroke"
113+
});
114+
});
115+
116+
it("Plot.autoSpec makes a lineX from monotonic y", () => {
117+
const data = [
118+
{date: 1, value: 1},
119+
{date: 2, value: 0},
120+
{date: 3, value: 38}
121+
];
122+
assert.deepStrictEqual(Plot.autoSpec(data, {x: "value", y: "date"}), {
123+
fx: null,
124+
fy: null,
125+
x: {value: "value", reduce: null, zero: false},
126+
y: {value: "date", reduce: null, zero: false},
127+
color: {value: null, reduce: null},
128+
size: {value: null, reduce: null},
129+
mark: "line",
130+
markImpl: "lineX",
131+
markOptions: {
132+
fx: undefined,
133+
fy: undefined,
134+
x: Object.assign([1, 0, 38], {label: "value"}),
135+
y: Object.assign([1, 2, 3], {label: "date"}),
136+
stroke: undefined,
137+
z: undefined,
138+
r: undefined
139+
},
140+
transformImpl: undefined,
141+
transformOptions: {stroke: undefined, r: undefined},
142+
colorMode: "stroke"
143+
});
144+
});
145+
146+
it("Plot.autoSpec makes a line from non-monotonic x and non-monotonic y", () => {
147+
const data = [
148+
{date: 2, value: 1},
149+
{date: 1, value: 0},
150+
{date: 3, value: 38}
151+
];
152+
assert.deepStrictEqual(Plot.autoSpec(data, {x: "date", y: "value", mark: "line"}), {
153+
fx: null,
154+
fy: null,
155+
x: {value: "date", reduce: null, zero: false},
156+
y: {value: "value", reduce: null, zero: false},
157+
color: {value: null, reduce: null},
158+
size: {value: null, reduce: null},
159+
mark: "line",
160+
markImpl: "line",
161+
markOptions: {
162+
fx: undefined,
163+
fy: undefined,
164+
x: Object.assign([2, 1, 3], {label: "date"}),
165+
y: Object.assign([1, 0, 38], {label: "value"}),
76166
stroke: undefined,
77167
z: undefined,
78168
r: undefined

0 commit comments

Comments
 (0)