Skip to content

Commit

Permalink
dense interval for line and area
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Feb 28, 2022
1 parent 4d36de0 commit cb84431
Show file tree
Hide file tree
Showing 7 changed files with 1,686 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/marks/area.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Curve} from "../curve.js";
import {Mark} from "../plot.js";
import {first, indexOf, maybeZ, second} from "../options.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyGroupedChannelStyles, groupIndex} from "../style.js";
import {maybeDenseIntervalX, maybeDenseIntervalY} from "../transforms/bin.js";
import {maybeIdentityX, maybeIdentityY} from "../transforms/identity.js";
import {maybeStackX, maybeStackY} from "../transforms/stack.js";

Expand Down Expand Up @@ -60,10 +61,12 @@ export function area(data, options) {
return new Area(data, options);
}

export function areaX(data, {y = indexOf, ...options} = {}) {
return new Area(data, maybeStackX(maybeIdentityX({...options, y1: y, y2: undefined})));
export function areaX(data, options) {
const {y = indexOf, ...rest} = maybeDenseIntervalY(options);
return new Area(data, maybeStackX(maybeIdentityX({...rest, y1: y, y2: undefined})));
}

export function areaY(data, {x = indexOf, ...options} = {}) {
return new Area(data, maybeStackY(maybeIdentityY({...options, x1: x, x2: undefined})));
export function areaY(data, options) {
const {x = indexOf, ...rest} = maybeDenseIntervalX(options);
return new Area(data, maybeStackY(maybeIdentityY({...rest, x1: x, x2: undefined})));
}
5 changes: 3 additions & 2 deletions src/marks/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Curve} from "../curve.js";
import {Mark} from "../plot.js";
import {indexOf, identity, maybeTuple, maybeZ} from "../options.js";
import {applyDirectStyles, applyIndirectStyles, applyTransform, applyGroupedChannelStyles, offset, groupIndex} from "../style.js";
import {maybeDenseIntervalX, maybeDenseIntervalY} from "../transforms/bin.js";
import {applyGroupedMarkers, markers} from "./marker.js";

const defaults = {
Expand Down Expand Up @@ -60,9 +61,9 @@ export function line(data, {x, y, ...options} = {}) {
}

export function lineX(data, {x = identity, y = indexOf, ...options} = {}) {
return new Line(data, {...options, x, y});
return new Line(data, maybeDenseIntervalY({...options, x, y}));
}

export function lineY(data, {x = indexOf, y = identity, ...options} = {}) {
return new Line(data, {...options, x, y});
return new Line(data, maybeDenseIntervalX({...options, x, y}));
}
12 changes: 12 additions & 0 deletions src/transforms/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export function bin(outputs = {fill: "count"}, options = {}) {
return binn(x, y, null, null, outputs, maybeInsetX(maybeInsetY(options)));
}

function maybeDenseInterval(bin, k, options) {
return options?.interval == null ? options : bin({[k]: options?.reduce === undefined ? "sum" : options.reduce, filter: null}, options);
}

export function maybeDenseIntervalX(options) {
return maybeDenseInterval(binX, "y", options);
}

export function maybeDenseIntervalY(options) {
return maybeDenseInterval(binY, "x", options);
}

function binn(
bx, // optionally bin on x (exclusive with gx)
by, // optionally bin on y (exclusive with gy)
Expand Down
Loading

0 comments on commit cb84431

Please sign in to comment.