-
Notifications
You must be signed in to change notification settings - Fork 185
a generic alternative for the collapsed scales #474
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,6 +179,7 @@ export function ScaleQ(key, scale, channels, { | |
|
||
if (range !== undefined) scale.range(range); | ||
if (clamp) scale.clamp(clamp); | ||
scale.isCollapsed = isCollapsed(scale); | ||
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. It’d be good to avoid the mutation here. |
||
return {type: "quantitative", reverse, domain, range, scale, inset, percent}; | ||
} | ||
|
||
|
@@ -284,3 +285,18 @@ function inferLogDomain(channels) { | |
} | ||
return [1, 10]; | ||
} | ||
|
||
// Certain marks have special behavior if a scale is collapsed, i.e. if the | ||
// domain is degenerate and represents only a single value such as [3, 3]; for | ||
// example, a rect will span the full extent of the chart along a collapsed | ||
// dimension (whereas a dot will simply be drawn in the center). | ||
export function isCollapsed(scale) { | ||
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. You moved this into quantitative.js, but this behavior isn’t limited to quantitative scales (although that is where it most often applies). See the ordinalBar example, which should also be considered collapsed if the domain only contains a single value. 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. Can you clarify the ordinalBar example? I've tried several variations and all seem to give the same chart under both branches. For example 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. The example is with a point scale: Plot.plot({
y: {
type: "point",
grid: true
},
marks: [
Plot.barY([0, 0, 0], {x: (d, i) => i, y2: d => d}),
Plot.ruleY([0])
]
}) This is admittedly a pretty pointless plot, but it does demonstrate the issue: in this PR, the y-domain is not considered collapsed because it is not a quantitative scale, even though its domain only has a single value [0]. Whereas in my PR you get: |
||
const domain = scale.domain(); | ||
const value = scale(domain[0]); | ||
for (let i = 1, n = domain.length; i < n; ++i) { | ||
if (scale(domain[i]) - value) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Plot from "@observablehq/plot"; | ||
|
||
export default async function() { | ||
return Plot.plot({ | ||
marks: [ | ||
Plot.barY({length: 1}, {x: ["foo"], y1: [0], y2: [0]}), | ||
Plot.ruleX(["foo"], {stroke: "red", y1: [0], y2: [0]}), | ||
Plot.dot({length: 1}, {x: ["foo"], y: [0], r: 100, fill: "white"}), | ||
Plot.text({length: 1}, {x: ["foo"], y: [0], text: ["♜"], fontSize: 180}) | ||
] | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as Plot from "@observablehq/plot"; | ||
|
||
export default async function() { | ||
return Plot.rectY([3], Plot.binX()).plot(); | ||
} |
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.
This is slightly different behavior than my PR this it will apply the scale’s inset. That might be desirable, though it’s different than the behavior of one-dimensional marks, and it’s also now dependent on the way the scale range is defined (which can be overridden). I assume it was done because the dimensions aren’t passed into this method, but it’s worth thinking about.
Also this requires materializing another array which is slow although not likely to be an issue. 😅