Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“mid” interval transform #641

Merged
merged 2 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/marks/dot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {create} from "d3";
import {filter, positive} from "../defined.js";
import {maybeIntervalX, maybeIntervalY} from "../transforms/interval.js";
import {maybeIntervalMidX, maybeIntervalMidY} from "../transforms/interval.js";
import {Mark, identity, maybeNumber, maybeTuple} from "../mark.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform, offset} from "../style.js";

Expand Down Expand Up @@ -53,13 +53,13 @@ export class Dot extends Mark {

export function dot(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Dot(data, maybeIntervalY(maybeIntervalX({...options, x, y})));
return new Dot(data, maybeIntervalMidY(maybeIntervalMidX({...options, x, y})));
}

export function dotX(data, {x = identity, ...options} = {}) {
return new Dot(data, maybeIntervalY({...options, x}));
return new Dot(data, maybeIntervalMidY({...options, x}));
}

export function dotY(data, {y = identity, ...options} = {}) {
return new Dot(data, maybeIntervalX({...options, y}));
return new Dot(data, maybeIntervalMidX({...options, y}));
}
4 changes: 2 additions & 2 deletions src/marks/image.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {create} from "d3";
import {filter, positive} from "../defined.js";
import {maybeIntervalX, maybeIntervalY} from "../transforms/interval.js";
import {maybeIntervalMidX, maybeIntervalMidY} from "../transforms/interval.js";
import {Mark, maybeNumber, maybeTuple, string} from "../mark.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyTransform, applyAttr, offset, impliedString} from "../style.js";

Expand Down Expand Up @@ -90,5 +90,5 @@ export class Image extends Mark {

export function image(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Image(data, maybeIntervalY(maybeIntervalX({...options, x, y})));
return new Image(data, maybeIntervalMidY(maybeIntervalMidX({...options, x, y})));
}
8 changes: 4 additions & 4 deletions src/marks/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {create} from "d3";
import {filter, nonempty} from "../defined.js";
import {Mark, indexOf, identity, string, maybeNumber, maybeTuple, numberChannel} from "../mark.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyAttr, applyTransform, offset} from "../style.js";
import {maybeIntervalX, maybeIntervalY} from "../transforms/interval.js";
import {maybeIntervalMidX, maybeIntervalMidY} from "../transforms/interval.js";

const defaults = {
strokeLinejoin: "round"
Expand Down Expand Up @@ -80,15 +80,15 @@ export class Text extends Mark {

export function text(data, {x, y, ...options} = {}) {
([x, y] = maybeTuple(x, y));
return new Text(data, maybeIntervalY(maybeIntervalX({...options, x, y})));
return new Text(data, maybeIntervalMidY(maybeIntervalMidX({...options, x, y})));
}

export function textX(data, {x = identity, ...options} = {}) {
return new Text(data, maybeIntervalY({...options, x}));
return new Text(data, maybeIntervalMidY({...options, x}));
}

export function textY(data, {y = identity, ...options} = {}) {
return new Text(data, maybeIntervalX({...options, y}));
return new Text(data, maybeIntervalMidX({...options, y}));
}

function applyIndirectTextStyles(selection, mark) {
Expand Down
46 changes: 38 additions & 8 deletions src/transforms/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ function maybeInterval(interval) {
if (typeof interval === "number") {
const n = interval;
// Note: this offset doesn’t support the optional step argument for simplicity.
interval = {floor: d => n * Math.floor(d / n), offset: d => d + n};
interval = {
floor: d => n * Math.floor(d / n),
mid: d => n * (Math.floor(d / n) + 0.5),
offset: d => d + n
};
}
if (typeof interval.floor !== "function" || typeof interval.offset !== "function") throw new Error("invalid interval");
if (typeof interval.mid !== "function") {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we’ll have to wrap every interval with this object, even though we won’t end up using it in most places. I think it’d be better to only create the mid function if it’s needed.

interval = {
...interval,
mid: x => (x = interval.floor(x), new Date((+x + +interval.offset(x)) / 2))
};
}
return interval;
}

Expand All @@ -27,14 +37,30 @@ function maybeIntervalK(k, maybeInsetK, options) {
const {[k]: v, [`${k}1`]: v1, [`${k}2`]: v2} = options;
const {value, interval} = maybeIntervalValue(v, options);
if (value == null || interval == null) return options;
let V1;
const tv1 = data => V1 || (V1 = valueof(data, value).map(v => interval.floor(v)));
let D1, V1;
const label = labelof(v);
function transform(data) {
if (V1 !== undefined && data === D1) return V1; // memoize
return V1 = valueof(D1 = data, value).map(v => interval.floor(v));
}
return maybeInsetK({
...options,
[k]: {transform: (data) => tv1(data).map(v => mid(interval.offset(v), v)), label},
[`${k}1`]: v1 === undefined ? {transform: tv1, label} : v1,
[`${k}2`]: v2 === undefined ? {transform: (data) => tv1(data).map(v => interval.offset(v)), label} : v2
[k]: undefined,
[`${k}1`]: v1 === undefined ? {transform, label} : v1,
[`${k}2`]: v2 === undefined ? {transform: data => transform(data).map(v => interval.offset(v)), label} : v2
});
}

function maybeIntervalMidK(k, maybeInsetK, options) {
const {[k]: v} = options;
const {value, interval} = maybeIntervalValue(v, options);
if (value == null || interval == null) return options;
return maybeInsetK({
...options,
[k]: {
label: labelof(v),
transform: data => valueof(data, value).map(interval.mid)
}
});
}

Expand All @@ -46,6 +72,10 @@ export function maybeIntervalY(options = {}) {
return maybeIntervalK("y", maybeInsetY, options);
}

function mid(a, b) {
return a instanceof Date ? new Date((+a + +b) / 2) : (a + b) / 2;
export function maybeIntervalMidX(options = {}) {
return maybeIntervalMidK("x", maybeInsetX, options);
}

export function maybeIntervalMidY(options = {}) {
return maybeIntervalMidK("y", maybeInsetY, options);
}