Skip to content

Commit dc083a3

Browse files
authored
trivial intervals for rect (#831)
1 parent bddb4d7 commit dc083a3

File tree

5 files changed

+131
-8
lines changed

5 files changed

+131
-8
lines changed

src/marks/rect.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Mark} from "../plot.js";
44
import {isCollapsed} from "../scales.js";
55
import {applyDirectStyles, applyIndirectStyles, applyTransform, impliedString, applyAttr, applyChannelStyles} from "../style.js";
66
import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
7-
import {maybeIntervalX, maybeIntervalY} from "../transforms/interval.js";
7+
import {maybeTrivialIntervalX, maybeTrivialIntervalY} from "../transforms/interval.js";
88
import {maybeStackX, maybeStackY} from "../transforms/stack.js";
99

1010
const defaults = {
@@ -68,13 +68,13 @@ export class Rect extends Mark {
6868
}
6969

7070
export function rect(data, options) {
71-
return new Rect(data, maybeIntervalX(maybeIntervalY(options)));
71+
return new Rect(data, maybeTrivialIntervalX(maybeTrivialIntervalY(options)));
7272
}
7373

7474
export function rectX(data, options = {y: indexOf, interval: 1, x2: identity}) {
75-
return new Rect(data, maybeStackX(maybeIntervalY(maybeIdentityX(options))));
75+
return new Rect(data, maybeStackX(maybeTrivialIntervalY(maybeIdentityX(options))));
7676
}
7777

7878
export function rectY(data, options = {x: indexOf, interval: 1, y2: identity}) {
79-
return new Rect(data, maybeStackY(maybeIntervalX(maybeIdentityY(options))));
79+
return new Rect(data, maybeStackY(maybeTrivialIntervalX(maybeIdentityY(options))));
8080
}

src/transforms/interval.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,39 @@ function maybeIntervalValue(value, {interval}) {
2828
return value;
2929
}
3030

31-
function maybeIntervalK(k, maybeInsetK, options) {
31+
function maybeIntervalK(k, maybeInsetK, options, trivial) {
3232
const {[k]: v, [`${k}1`]: v1, [`${k}2`]: v2} = options;
3333
const {value, interval} = maybeIntervalValue(v, options);
34-
if (value == null || interval == null) return options;
34+
if (value == null || (interval == null && !trivial)) return options;
35+
const label = labelof(v);
36+
if (interval == null) {
37+
let V;
38+
const kv = {transform: data => V || (V = valueof(data, value)), label};
39+
return {
40+
...options,
41+
[k]: undefined,
42+
[`${k}1`]: v1 === undefined ? kv : v1,
43+
[`${k}2`]: v2 === undefined ? kv : v2
44+
};
45+
}
3546
let V1;
3647
const tv1 = data => V1 || (V1 = valueof(data, value).map(v => interval.floor(v)));
37-
const label = labelof(v);
3848
return maybeInsetK({
3949
...options,
4050
[k]: undefined,
4151
[`${k}1`]: v1 === undefined ? {transform: tv1, label} : v1,
42-
[`${k}2`]: v2 === undefined ? {transform: () => tv1().map(v => interval.offset(v)), label} : v2
52+
[`${k}2`]: v2 === undefined ? {transform: data => tv1(data).map(v => interval.offset(v)), label} : v2
4353
});
4454
}
4555

56+
export function maybeTrivialIntervalX(options = {}) {
57+
return maybeIntervalK("x", maybeInsetX, options, true);
58+
}
59+
60+
export function maybeTrivialIntervalY(options = {}) {
61+
return maybeIntervalK("y", maybeInsetY, options, true);
62+
}
63+
4664
export function maybeIntervalX(options = {}) {
4765
return maybeIntervalK("x", maybeInsetX, options);
4866
}

test/output/groupedRects.svg

Lines changed: 95 additions & 0 deletions
Loading

test/plots/grouped-rects.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Plot from "@observablehq/plot";
2+
3+
export default async function() {
4+
return Plot.plot({
5+
marks: [
6+
Plot.rectY({length: 10}, Plot.groupX({y: "count"}, {x: (d, i) => "ABCDEFGHIJ"[i]}))
7+
]
8+
});
9+
}

test/plots/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export {default as gistempAnomalyMoving} from "./gistemp-anomaly-moving.js";
6161
export {default as gistempAnomalyTransform} from "./gistemp-anomaly-transform.js";
6262
export {default as googleTrendsRidgeline} from "./google-trends-ridgeline.js";
6363
export {default as gridChoropleth} from "./grid-choropleth.js";
64+
export {default as groupedRects} from "./grouped-rects.js";
6465
export {default as hadcrutWarmingStripes} from "./hadcrut-warming-stripes.js";
6566
export {default as highCardinalityOrdinal} from "./high-cardinality-ordinal.js";
6667
export {default as identityScale} from "./identity-scale.js";

0 commit comments

Comments
 (0)