Skip to content

Commit e051e5d

Browse files
authored
fix auto area (#1275)
* fix auto area * prioritize area zeroness; favor areaY * arrays are not reducers * remove unused import * shorter
1 parent 6eab4f2 commit e051e5d

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

src/marks/auto.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {ascending, InternSet} from "d3";
2-
import {isOrdinal, labelof, valueof, isOptions, isColor} from "../options.js";
3-
import {area, areaX, areaY} from "./area.js";
2+
import {isOrdinal, labelof, valueof, isOptions, isColor, isObject} from "../options.js";
3+
import {areaX, areaY} from "./area.js";
44
import {dot} from "./dot.js";
55
import {line, lineX, lineY} from "./line.js";
66
import {ruleX, ruleY} from "./rule.js";
@@ -123,16 +123,7 @@ export function auto(data, {x, y, color, size, fx, fy, mark} = {}) {
123123
if (isHighCardinality(color)) z = null; // TODO only if z not set by user
124124
break;
125125
case "area":
126-
mark =
127-
x && y
128-
? isContinuous(x) && isMonotonic(x)
129-
? areaY
130-
: isContinuous(y) && isMonotonic(y)
131-
? areaX
132-
: area // TODO error? how does it work with ordinal?
133-
: x
134-
? areaX
135-
: areaY; // 1d area by index
126+
mark = yZero ? areaY : xZero || (y && isMonotonic(y)) ? areaX : areaY; // favor areaY if unsure
136127
colorMode = "fill";
137128
if (isHighCardinality(color)) z = null; // TODO only if z not set by user
138129
break;
@@ -240,7 +231,7 @@ function isZeroReducer(reduce) {
240231

241232
// https://github.com/observablehq/plot/blob/818562649280e155136f730fc496e0b3d15ae464/src/transforms/group.js#L236
242233
function isReducer(reduce) {
243-
if (typeof reduce?.reduce === "function") return true;
234+
if (typeof reduce?.reduce === "function" && isObject(reduce)) return true; // N.B. array.reduce
244235
if (/^p\d{2}$/i.test(reduce)) return true;
245236
switch (`${reduce}`.toLowerCase()) {
246237
case "first":

src/transforms/group.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
range,
3030
second,
3131
percentile,
32-
isTemporal
32+
isTemporal,
33+
isObject
3334
} from "../options.js";
3435
import {basic} from "./basic.js";
3536

@@ -234,7 +235,7 @@ export function maybeGroup(I, X) {
234235
}
235236

236237
export function maybeReduce(reduce, value) {
237-
if (reduce && typeof reduce.reduce === "function") return reduce;
238+
if (typeof reduce?.reduce === "function" && isObject(reduce)) return reduce; // N.B. array.reduce
238239
if (typeof reduce === "function") return reduceFunction(reduce);
239240
if (/^p\d{2}$/i.test(reduce)) return reduceAccessor(percentile(reduce));
240241
switch (`${reduce}`.toLowerCase()) {

test/output/autoAutoHistogram.svg

Lines changed: 70 additions & 0 deletions
Loading

test/plots/autoplot.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ export async function autoAreaStackColor() {
177177
return Plot.auto(industries, {x: "date", y: "unemployed", color: "industry", mark: "area"}).plot();
178178
}
179179

180+
export async function autoAutoHistogram() {
181+
const weather = await d3.csv("data/seattle-weather.csv", d3.autoType);
182+
return Plot.auto(weather, {x: "temp_max", mark: "area"}).plot();
183+
}
184+
180185
export async function autoBarStackColorField() {
181186
const athletes = await d3.csv("data/athletes.csv", d3.autoType);
182187
return Plot.auto(athletes, {x: "height", color: {value: "gold"}}).plot();

0 commit comments

Comments
 (0)