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

expose instantiated scales descriptors in the render API #1810

Merged
merged 13 commits into from
Aug 16, 2023
Merged

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Aug 16, 2023

Address the most important part of #1263. In this simplest approach, we add the instantiated scales descriptors to the otherwise unmodified scales object.

the render method is thus called as

render(index, {x: xScale, y: yScale, color: colorScale, scales: {x, y, color}}, values, dimensions, context[, next])

where xScale, yScale, colorScale (etc) are D3 scales, and x, y, color are instantiated scale objects.

xScale, yScale… are documented as functions that map from the data domain to the visual domain (the apply function), but still leak the D3 abstraction. The long-term plan is to replace them with the apply function only; this means chasing all the internal occurrences — for example, of xScale.bandwidth() or xScale.ticks() — and replacing them with a proper look-up in the scale descriptor (scales.x.bandwidth). (It will be a different PR.)

@Fil Fil requested a review from mbostock August 16, 2023 13:10
…ptor’s range and domain, better align it with what the D3 scales return
src/plot.js Outdated Show resolved Hide resolved
src/scales.d.ts Outdated Show resolved Hide resolved
src/scales.js Outdated
const scaleFunctions = {scales};
for (const [key, desc] of Object.entries(descriptors)) {
const {scale, type, interval, label} = desc;
scales[key] = exposeScale(desc);
Copy link
Member

Choose a reason for hiding this comment

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

This involves a bunch of copying and these exposed scales are rarely used. I think we should investigate whether we can make this lazy using a getter (but probably a caching getter so that if you access the same scale multiple times it returns the same instance).

Copy link
Member

Choose a reason for hiding this comment

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

Fil’s comment: We’ll need these exposed scales in the near future anyway when we adopt them internally for axes etc., and therefore it’s premature optimization (or even slower) to make these lazy.

src/scales.js Outdated Show resolved Hide resolved
import * as d3 from "d3";
import * as htl from "htl";

export async function renderInstantiatedScales() {
Copy link
Member

Choose a reason for hiding this comment

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

The file name “render.ts” isn’t very descriptive.

And I think this would be better as a unit test instead of a snapshot test since most of it is about checking the exposed scales, not the visual appearance of the chart.

@mbostock
Copy link
Member

I like the progressive roll-out plan, starting here and then following up with tightening down the scale function representation to just the apply function. 👍

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

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

This also changes plot.scale(…) to return the existing instantiated scale instance, rather than creating a new instance each time you call it. I think that’s okay, but we should note that in the release notes and the documentation since it’s a slight change in semantics. Possibly we should still make a defensive copy to protect against external mutation, but… I dunno, maybe it’s better to minimize the number of copies instead.

src/scales.js Outdated Show resolved Hide resolved
@Fil Fil enabled auto-merge (squash) August 16, 2023 18:38
@Fil Fil merged commit f360557 into main Aug 16, 2023
1 check passed
@Fil Fil deleted the fil/render-api branch August 16, 2023 18:40
Fil added a commit that referenced this pull request Aug 21, 2023
* expose instantiated scales descriptors in the render API
* more uniform handling of identity scales
* don’t expose domain and range for identity (yet)

---------

Co-authored-by: Mike Bostock <mbostock@gmail.com>
chaichontat pushed a commit to chaichontat/plot that referenced this pull request Jan 14, 2024
…q#1810)

* expose instantiated scales descriptors in the render API
* more uniform handling of identity scales
* don’t expose domain and range for identity (yet)

---------

Co-authored-by: Mike Bostock <mbostock@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants