-
Notifications
You must be signed in to change notification settings - Fork 171
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
coerce to the scale’s type #532
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
95e2e1a
coerce to the scale’s type
mbostock dfde475
upgrade isoformat
mbostock 1b3428c
document type coercion
Fil 25ff424
language
Fil e626354
changelog
Fil 24d0897
update CHANGELOG
mbostock 11c3228
coerce invalid dates to undefined
mbostock 916d04b
update README
mbostock 9d7cde3
update README
mbostock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import {ScaleDiverging, ScaleDivergingSqrt, ScaleDivergingPow, ScaleDivergingLog | |
import {ScaleTime, ScaleUtc} from "./scales/temporal.js"; | ||
import {ScaleOrdinal, ScalePoint, ScaleBand} from "./scales/ordinal.js"; | ||
import {isOrdinal, isTemporal} from "./mark.js"; | ||
import {parse as isoParse} from "isoformat"; | ||
|
||
export function Scales(channels, {inset, round, nice, align, padding, ...options} = {}) { | ||
const scales = {}; | ||
|
@@ -58,7 +59,37 @@ function autoScaleRound(scale) { | |
} | ||
|
||
function Scale(key, channels = [], options = {}) { | ||
switch (inferScaleType(key, channels, options)) { | ||
const type = inferScaleType(key, channels, options); | ||
|
||
// Once the scale type is known, coerce the associated channel values and any | ||
// explicitly-specified domain to the expected type. | ||
switch (type) { | ||
case "diverging": | ||
case "diverging-sqrt": | ||
case "diverging-pow": | ||
case "diverging-log": | ||
case "diverging-symlog": | ||
case "cyclical": | ||
case "sequential": | ||
case "linear": | ||
case "sqrt": | ||
case "threshold": | ||
case "quantile": | ||
case "pow": | ||
case "log": | ||
case "symlog": | ||
options = coerceType(channels, options, coerceNumber, Float64Array); | ||
break; | ||
case "identity": | ||
if (registry.get(key) === position) options = coerceType(channels, options, coerceNumber, Float64Array); | ||
break; | ||
case "utc": | ||
case "time": | ||
options = coerceType(channels, options, coerceDate); | ||
break; | ||
} | ||
|
||
switch (type) { | ||
case "diverging": return ScaleDiverging(key, channels, options); | ||
case "diverging-sqrt": return ScaleDivergingSqrt(key, channels, options); | ||
case "diverging-pow": return ScaleDivergingPow(key, channels, options); | ||
|
@@ -144,3 +175,32 @@ export function isCollapsed(scale) { | |
} | ||
return true; | ||
} | ||
|
||
// Mutates channel.value! | ||
function coerceType(channels, options, coerce, type) { | ||
for (const c of channels) c.value = coerceArray(c.value, coerce, type); | ||
return {...options, domain: coerceArray(options.domain, coerce, type)}; | ||
} | ||
|
||
function coerceArray(array, coerce, type = Array) { | ||
if (array !== undefined) return type.from(array, coerce); | ||
} | ||
|
||
// Unlike Mark’s number, here we want to convert null and undefined to NaN, | ||
// since the result will be stored in a Float64Array and we don’t want null to | ||
// be coerced to zero. | ||
function coerceNumber(x) { | ||
return x == null ? NaN : +x; | ||
} | ||
|
||
// When coercing strings to dates, we only want to allow the ISO 8601 format | ||
// since the built-in string parsing of the Date constructor varies across | ||
// browsers. (In the future, this could be made more liberal if desired, though | ||
// it is still generally preferable to do date parsing yourself explicitly, | ||
// rather than rely on Plot.) Any non-string values are coerced to number first | ||
// and treated as milliseconds since UNIX epoch. | ||
function coerceDate(x) { | ||
return x instanceof Date ? x | ||
: typeof x === "string" ? isoParse(x) | ||
: new Date(x == null ? NaN : +x); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning a Date instance for undefined or NaN input feels wrong here, especially because our defined helper considers invalid dates to be defined. We should probably return undefined instead of an invalid date. |
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,19 @@ | ||
import * as Plot from "@observablehq/plot"; | ||
import * as d3 from "d3"; | ||
|
||
export default async function() { | ||
const AAPL = await d3.csv("data/aapl.csv"); | ||
return Plot.plot({ | ||
x: { | ||
type: "utc" | ||
}, | ||
y: { | ||
type: "linear", | ||
grid: true | ||
}, | ||
marks: [ | ||
Plot.line(AAPL, {x: "Date", y: "Close"}), | ||
Plot.ruleY([0]) | ||
] | ||
}); | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe these comments could be reflected in the README?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will take a pass at the README before I merge. Thank you! 👍