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

Add type names to scales #274

Closed
wants to merge 2 commits into from
Closed

Add type names to scales #274

wants to merge 2 commits into from

Conversation

mhkeller
Copy link

Per #25 (comment), this proposal adds a type field to each scale that returns a string name. I called it type for now but have no opinion on what that would end up being. Maybe scaleType or something else is better.

I added tests but, for now, only where the existing defaults were being tested. For example, I didn't immediately see where scales like sequentialLog were being tested for defaults. I can add those tests where most appropriate.

Let me know if you think this is a worthwhile addition. It would definitely help my use case with a minimal footprint on the original library. Thanks for your time in reviewing it.

@mhkeller mhkeller marked this pull request as ready for review February 14, 2023 01:42
@mhkeller
Copy link
Author

I added a test to check that the type for all scale functions matches

@mhkeller
Copy link
Author

mhkeller commented Mar 9, 2023

@mbostock Just wanted to check if this was something you would consider as a PR. It would be very useful in the chart framework I maintain.

@mbostock
Copy link
Member

mbostock commented Mar 9, 2023

Apologies but I don’t intend on supporting this for now.

@mhkeller
Copy link
Author

mhkeller commented Mar 9, 2023

Thanks for letting me know

@mhkeller
Copy link
Author

mhkeller commented Mar 9, 2023

@mbostock do you have any advice on differentiating between a scaleTime and scaleUtc? That's the last one I'm stuck on.

@mbostock
Copy link
Member

mbostock commented Mar 9, 2023

@mhkeller Here’s a hack…

const d = new Date;
d.getDay = () => console.log("time");
d.getUTCDay = () => console.log("utc");
const scale = d3.scaleUtc(); // or d3.scaleTime();
scale.tickFormat(0, "%a")(d);

Basically, override a Date object, and see what methods are called when you try to format it. That way you can tell what the scale type is.

@mhkeller
Copy link
Author

mhkeller commented Mar 9, 2023

Thanks that's much appreciated!

@mhkeller mhkeller closed this Mar 9, 2023
@mhkeller
Copy link
Author

For anyone coming across this in the future, I tweaked this slightly so my function would return and added a .copy to preserve any tickFormat set by the user.

const d = new Date;
let s;
d.getDay = () => s = 'time';
d.getUTCDay = () => s = 'utc';

scale.copy().tickFormat(0, '%a')(d);
return s;

@mbostock
Copy link
Member

Calling scale.tickFormat doesn’t mutate the scale, so no copy is necessary. It only affects the returned format function, not future calls to scale.tickFormat.

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

Successfully merging this pull request may close these issues.

2 participants