Skip to content

Commit 6eab4f2

Browse files
authored
only default to bar for x or y is zeroed (#1273)
* only default to bar for zeroed reducers * default bar if x or y is zeroed
1 parent 3579c92 commit 6eab4f2

File tree

6 files changed

+173
-39
lines changed

6 files changed

+173
-39
lines changed

src/marks/auto.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {InternSet} from "d3";
1+
import {ascending, InternSet} from "d3";
22
import {isOrdinal, labelof, valueof, isOptions, isColor} from "../options.js";
33
import {area, areaX, areaY} from "./area.js";
44
import {dot} from "./dot.js";
@@ -11,7 +11,6 @@ import {frame} from "./frame.js";
1111
import {bin, binX, binY} from "../transforms/bin.js";
1212
import {group, groupX, groupY} from "../transforms/group.js";
1313
import {marks} from "../mark.js";
14-
import {ascending} from "d3";
1514

1615
/** @jsdoc auto */
1716
export function auto(data, {x, y, color, size, fx, fy, mark} = {}) {
@@ -45,8 +44,8 @@ export function auto(data, {x, y, color, size, fx, fy, mark} = {}) {
4544
if (yReduce === undefined)
4645
yReduce = xReduce == null && yValue == null && sizeValue == null && xValue != null ? "count" : null;
4746

48-
let {zero: xZero} = x;
49-
let {zero: yZero} = y;
47+
let {zero: xZero = isZeroReducer(xReduce) ? true : undefined} = x;
48+
let {zero: yZero = isZeroReducer(yReduce) ? true : undefined} = y;
5049

5150
// TODO The line mark will need z?
5251
// TODO Limit and sort for bar charts (e.g. alphabet)?
@@ -98,17 +97,13 @@ export function auto(data, {x, y, color, size, fx, fy, mark} = {}) {
9897
mark =
9998
sizeValue != null || sizeReduce != null
10099
? "dot"
101-
: xReduce != null || yReduce != null || colorReduce != null
100+
: xZero || yZero || colorReduce != null // histogram or heatmap
102101
? "bar"
103102
: x && y
104-
? isContinuous(x) && isContinuous(y) && (isMonotonic(x) || isMonotonic(y))
103+
? isContinuous(x) && isContinuous(y) && (xReduce != null || yReduce != null || isMonotonic(x) || isMonotonic(y))
105104
? "line"
106-
: (isContinuous(x) && xZero) || (isContinuous(y) && yZero)
107-
? "bar"
108105
: "dot"
109-
: x
110-
? "rule"
111-
: y
106+
: x || y
112107
? "rule"
113108
: null;
114109
}
@@ -239,6 +234,10 @@ function makeOptions(value) {
239234
return isReducer(value) ? {reduce: value} : {value};
240235
}
241236

237+
function isZeroReducer(reduce) {
238+
return /^(?:distinct|count|sum|proportion)$/i.test(reduce);
239+
}
240+
242241
// https://github.com/observablehq/plot/blob/818562649280e155136f730fc496e0b3d15ae464/src/transforms/group.js#L236
243242
function isReducer(reduce) {
244243
if (typeof reduce?.reduce === "function") return true;
File renamed without changes.

test/output/autoLineHistogram.svg

Lines changed: 32 additions & 27 deletions
Loading

test/output/autoLineMean.svg

Lines changed: 52 additions & 0 deletions
Loading

test/output/autoLineMeanColor.svg

Lines changed: 68 additions & 0 deletions
Loading

test/plots/autoplot.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,21 @@ export async function autoBarStackColorConstant() {
187187
return Plot.auto(athletes, {x: "height", color: "gold"}).plot();
188188
}
189189

190-
export async function autoBarMean() {
190+
export async function autoBarMeanZero() {
191+
const weather = await d3.csv("data/seattle-weather.csv", d3.autoType);
192+
return Plot.auto(weather, {x: "date", y: {value: "temp_max", reduce: "mean", zero: true}}).plot();
193+
}
194+
195+
export async function autoLineMean() {
191196
const weather = await d3.csv("data/seattle-weather.csv", d3.autoType);
192197
return Plot.auto(weather, {x: "date", y: {value: "temp_max", reduce: "mean"}}).plot();
193198
}
194199

200+
export async function autoLineMeanColor() {
201+
const athletes = await d3.csv("data/athletes.csv", d3.autoType);
202+
return Plot.auto(athletes, {x: "date_of_birth", y: {value: "height", reduce: "mean"}, color: "sex"}).plot();
203+
}
204+
195205
export async function autoLineFacet() {
196206
const industries = await d3.csv("data/bls-industry-unemployment.csv", d3.autoType);
197207
return Plot.auto(industries, {x: "date", y: "unemployed", fy: "industry"}).plot();

0 commit comments

Comments
 (0)