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

Generalizing scale.unknown for missing data. #97

Closed
mbostock opened this issue Mar 14, 2017 · 2 comments
Closed

Generalizing scale.unknown for missing data. #97

mbostock opened this issue Mar 14, 2017 · 2 comments
Assignees

Comments

@mbostock
Copy link
Member

It’d be great if you could define the equivalent to ordinal.unknown for a threshold scale. Currently if you call a threshold scale with a NaN value, it returns undefined:

var color = d3.scaleThreshold()
    .domain([2, 3, 4, 5, 6, 7, 8, 9])
    .range(d3.schemeRdPu[9]);

color(undefined); // undefined
color(NaN); // undefined

(Note that color(null) is treated equivalently to color(0), since +null === 0. That’s a separate issue, though.)

Returning undefined seems like reasonable default behavior, but it sometimes leads to surprising consequences that would be nice to avoid. For example, if you set context.strokeStyle to undefined, nothing happens! You continue to get whatever stroke style was set previously, thus silently displaying invalid data as whatever happened to come before it.

Safer would be something like this:

var color = d3.scaleThreshold()
    .domain([2, 3, 4, 5, 6, 7, 8, 9])
    .range(d3.schemeRdPu[9])
    .unknown("#ccc");

color(undefined); // "#ccc"
@mbostock mbostock self-assigned this Mar 14, 2017
@mbostock
Copy link
Member Author

A similar argument can be made for continuous scales like d3.scaleLinear, d3.scaleSequential, etc. Currently the behavior depends on the interpolator: a NaN t is passed to the interpolator, which can then decide what to do. In the common case of d3.interpolateRgb, d3.rgb treats NaN channel values as zero, so you end up with opaque black for any NaN input. It’d be nice to have a scale.unknown here, too, whose value would be returned instead of invoking the interpolator.

@mbostock mbostock changed the title threshold.unknown Generalizing scale.unknown for missing data. Mar 14, 2017
mbostock added a commit that referenced this issue Jan 26, 2019
@mbostock
Copy link
Member Author

Added to #156.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant