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

Improved formatting of numeric scale labels #7007

Merged
merged 4 commits into from
Feb 3, 2020

Conversation

benmccann
Copy link
Contributor

@benmccann benmccann commented Jan 23, 2020

This makes three main changes:

  • Use commas in large numbers just as proposed in Format numbers in tooltip #7004 for the tooltips
  • Change the arguments to ticks.callback to be a context object similar to scriptable options
  • Use the same logic to format numbers on linear and log scales. The log scale previously always used scientific notation, which has never made sense to me. E.g. if you look at the test that changed in scale.logarithmic.tests.js the numbers range from 1 to 80. It's much easier to read these just as normal numbers instead of scientific notation. We still use scientific notation when all the numbers are small.

There's also just some minor changes like using const/let and improving the structure of the if/else checks to bring cognitive complexity down from 9 to 5 to make codeclimate happy

@benmccann benmccann force-pushed the format-ticks branch 2 times, most recently from fcedf3a to 08effe7 Compare January 23, 2020 16:38
etimberg
etimberg previously approved these changes Jan 25, 2020
src/core/core.ticks.js Outdated Show resolved Hide resolved
test/specs/scale.radialLinear.tests.js Show resolved Hide resolved
@etimberg
Copy link
Member

If we think this will be useful to lots of users, perhaps we can offer it as an option, but not change the defaults? That would give a quick switch but keep the defaults simple (similar idea for #7004)

@benmccann benmccann force-pushed the format-ticks branch 4 times, most recently from e585af8 to 26b1d00 Compare January 27, 2020 21:28
@benmccann
Copy link
Contributor Author

If we think this will be useful to lots of users, perhaps we can offer it as an option, but not change the defaults? That would give a quick switch but keep the defaults simple

I'd probably rather keep it simpler by having fewer options and just always doing it. I think inserting a comma is a pretty minor change and I'm struggling to think of a usecase where it wouldn't be valuable. If we get feedback during the alpha/beta we can always come back to add an option.

Copy link
Member

@kurkle kurkle left a comment

Choose a reason for hiding this comment

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

Another thing I'm not quite sure about is the placement of the locale configuration. Should it rather be in options? One could want to have charts with different locales on the same page (for example in Chart.js docs).

src/core/core.scale.js Outdated Show resolved Hide resolved
@benmccann benmccann force-pushed the format-ticks branch 2 times, most recently from 8e63e8a to f8b0a69 Compare January 30, 2020 05:55
Copy link
Member

@kurkle kurkle left a comment

Choose a reason for hiding this comment

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

There is a mix of Chart.platform.locale and chart.platform.locale (global vs instance).
Is that intended?

@benmccann
Copy link
Contributor Author

No, it wasn't. I updated that. Thanks

kurkle
kurkle previously approved these changes Jan 30, 2020
Copy link
Member

@kurkle kurkle left a comment

Choose a reason for hiding this comment

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

Still unsure about the placement of locale, but that can be changed later if needed.

@benmccann
Copy link
Contributor Author

Rebased

Copy link
Member

@etimberg etimberg left a comment

Choose a reason for hiding this comment

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

Agree, not hot on placing it on Chart.platform since that was intended to be for switching between browser, basic platform, etc.

I think some documentation should also be written telling users how to use this new option (unless we are intending this only for testing)

tickString = '0'; // never show decimal places for 0
const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value));
const locale = Chart.platform.locale;
if (maxTick < 1e-4) { // all ticks are small numbers; use scientific notation
Copy link
Member

Choose a reason for hiding this comment

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

I know the old code didn't do this, but would it be good to do this if all ticks are large as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've added that

@benmccann
Copy link
Contributor Author

My thinking for putting it on Chart.platform is that is where we've put options that apply at the platform level. I.e. options that affect all charts (which right now is just disableCSSInjection). I'm assuming you wouldn't want charts with different languages on the same page, but maybe that's a bad assumption? What do you think would be a better place to put it? Directly under options?

@etimberg
Copy link
Member

Personally, I would put under options that way the user can set globally or per chart. I'd rather not limit users since that'll just be a feature request later

@benmccann
Copy link
Contributor Author

Ok, I've moved it to being an option. Unfortunately there's no really good page to document it on, so I've punted on that for now since it at least solves the testing issue

etimberg
etimberg previously approved these changes Jan 31, 2020
@benmccann benmccann requested a review from kurkle January 31, 2020 14:28
Copy link
Member

@kurkle kurkle left a comment

Choose a reason for hiding this comment

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

While at it, can we change the calling of the callback to be consistent. Either way is fine by me.

src/core/core.scale.js Outdated Show resolved Hide resolved
@@ -652,7 +652,7 @@ class TimeScale extends Scale {
tickOpts.callback
]);

return formatter ? formatter(label, index, ticks) : label;
return formatter ? formatter(label, index, ticks, me) : label;
Copy link
Member

Choose a reason for hiding this comment

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

Oh, looks like we are calling the callback inconsistently. Should probably make it consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, there's multiple things I don't like about this. One of the biggest is that the first parameter is different here. I'd noticed while working on this. It's not that easy to change though and a bit outside the scope of this PR, so probably better handled separately

const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value));
const minTick = Math.min(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value));
const locale = scale.chart.options.locale;
if (maxTick < 1e-4 || minTick > 1e+7) { // all ticks are small or big numbers; use scientific notation
Copy link
Member

Choose a reason for hiding this comment

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

Don't really like the use of constants here, but this is something that can be altered if it becomes an issue later.

@etimberg etimberg added this to the Version 3.0 milestone Feb 3, 2020
@etimberg etimberg merged commit f5d9892 into chartjs:master Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants