-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* interval for rect * default insets for intervals * Update src/transforms/interval.js * numeric interval & documentation (#552) * interval for rule and bar * Update CHANGELOG Co-authored-by: Philippe Rivière <[email protected]>
- Loading branch information
Showing
11 changed files
with
301 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {offset} from "../style.js"; | ||
|
||
export function maybeInsetX({inset, insetLeft, insetRight, ...options} = {}) { | ||
([insetLeft, insetRight] = maybeInset(inset, insetLeft, insetRight)); | ||
return {inset, insetLeft, insetRight, ...options}; | ||
} | ||
|
||
export function maybeInsetY({inset, insetTop, insetBottom, ...options} = {}) { | ||
([insetTop, insetBottom] = maybeInset(inset, insetTop, insetBottom)); | ||
return {inset, insetTop, insetBottom, ...options}; | ||
} | ||
|
||
function maybeInset(inset, inset1, inset2) { | ||
return inset === undefined && inset1 === undefined && inset2 === undefined | ||
? (offset ? [1, 0] : [0.5, 0.5]) | ||
: [inset1, inset2]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {labelof, maybeValue, valueof} from "../mark.js"; | ||
import {maybeInsetX, maybeInsetY} from "./inset.js"; | ||
|
||
// TODO Allow the interval to be specified as a string, e.g. “day” or “hour”? | ||
// This will require the interval knowing the type of the associated scale to | ||
// chose between UTC and local time (or better, an explicit timeZone option). | ||
function maybeInterval(interval) { | ||
if (interval == null) return; | ||
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}; | ||
} | ||
if (typeof interval.floor !== "function" || typeof interval.offset !== "function") throw new Error("invalid interval"); | ||
return interval; | ||
} | ||
|
||
// The interval may be specified either as x: {value, interval} or as {x, | ||
// interval}. The former is used, for example, for Plot.rect. | ||
function maybeIntervalValue(value, {interval} = {}) { | ||
value = {...maybeValue(value)}; | ||
value.interval = maybeInterval(value.interval === undefined ? interval : value.interval); | ||
return value; | ||
} | ||
|
||
function maybeIntervalK(k, maybeInsetK, options = {}) { | ||
const {[k]: v, [`${k}1`]: v1, [`${k}2`]: v2} = options; | ||
const {value, interval} = maybeIntervalValue(v, options); | ||
if (interval == null) return options; | ||
let V1; | ||
const tv1 = data => V1 || (V1 = valueof(data, value).map(v => interval.floor(v))); | ||
const label = labelof(v); | ||
return maybeInsetK({ | ||
...options, | ||
[k]: undefined, | ||
[`${k}1`]: v1 === undefined ? {transform: tv1, label} : v1, | ||
[`${k}2`]: v2 === undefined ? {transform: () => tv1().map(v => interval.offset(v)), label} : v2 | ||
}); | ||
} | ||
|
||
export function maybeIntervalX(options) { | ||
return maybeIntervalK("x", maybeInsetX, options); | ||
} | ||
|
||
export function maybeIntervalY(options = {}) { | ||
return maybeIntervalK("y", maybeInsetY, options); | ||
} |
Oops, something went wrong.