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

Better number -> string callback for the radial linear scale #3281

Merged
merged 6 commits into from
Sep 24, 2016

Conversation

etimberg
Copy link
Member

@etimberg etimberg commented Sep 9, 2016

Fixes #2602

Image Before Change

before

Image After Change

after

@etimberg
Copy link
Member Author

etimberg commented Sep 9, 2016

@simonbrunel I'm not sure what the best way to handle this is. Right now, the radial linear config has a copy of the exact same function as the linear scale config.

It would almost be nice if we wrote this using ES6 imports because then the config could go off and live in another file and be imported in 2 places but ultimately exist only once in the build. That's probably for later though

@simonbrunel
Copy link
Member

I'm not sure neither but it's similar to controller.doughnut.js and controler.polarArea.js. Found these ones some time ago (when trying to make Code Climate happier) and I was thinking to move it in code.legend.js (I still have a local branch with that changes).

For your case, a solution could be to move that code in core.scale.js:

Chart.Scale.ticks = {
    generators: {
        linear: function() {
           // your linear code
        },
        values: {
            // the default one
        }
    }
}

and in scale.linear.js and scale.radialLinear.js:

ticks: {
    callback: Chart.Scale.ticks.generators.linear
}

Then we can add more tick generation methods, maybe :)

It could be also in core.ticks.js (via Chart.ticks = { ... }) or even in scale.linearbase.js.

Just draft ideas though, not sure that the best way to solve that.

@etimberg
Copy link
Member Author

@simonbrunel I moved the common stuff into Chart.Ticks.formatters. if we like where this is going, I can try and factor out the generator functions into Chart.Ticks.generators. This would probably eliminate much of the need for the scale.linearBase file.

@@ -47,7 +47,7 @@ module.exports = function(Chart) {
labelOffset: 0,
// We pass through arrays to be rendered as multiline labels, we convert Others to strings here.
callback: function(value) {
return helpers.isArray(value) ? value : '' + value;
return Chart.Ticks.formatters.values(value);
Copy link
Member

Choose a reason for hiding this comment

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

I think we can save some bits and a function call: callback: Chart.Ticks.formatters.values

}

return tickString;
return Chart.Ticks.formatters.linear(tickValue, index, ticks);
Copy link
Member

Choose a reason for hiding this comment

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

Same here: callback: Chart.Ticks.formatters.linear

return value.toExponential();
}
return '';
return Chart.Ticks.formatters.logarithmic(value, index, arr);
Copy link
Member

Choose a reason for hiding this comment

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

callback: Chart.Ticks.formatters.logarithmic

backdropPaddingX: 2,

callback: function(tickValue, index, ticks) {
return Chart.Ticks.formatters.linear(tickValue, index, ticks);
Copy link
Member

Choose a reason for hiding this comment

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

Same

@simonbrunel
Copy link
Member

It looks really great 👍 Just a few minor optimizations but I like how it helps to refactor code. Doing this for generators seems a good idea.

@etimberg
Copy link
Member Author

Great, I'll make the changes and move the generators in as well

@etimberg
Copy link
Member Author

I've moved the linear and logarithmic generators into Chart.Ticks.generators. Now I'm wishing we had written the category scale to work as a numeric scale that simply displayed values. It would give a lot more code re-use

@etimberg
Copy link
Member Author

@simonbrunel do you think we need to move any more stuff into the Chart.Ticks.generators namespace? if not, I think this is good to merge.

constructor: function() {
// These two properties kept for backwards compatibility
var me = this;
Object.defineProperty(me, 'min', {
Copy link
Member

Choose a reason for hiding this comment

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

do we really need to keep these properties?

Copy link
Member Author

Choose a reason for hiding this comment

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

I know that the zoom plugin uses them. I don't know if anything else does

Copy link
Member

Choose a reason for hiding this comment

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

What the reason to move min and max into a new dataRange object?

Copy link
Member Author

Choose a reason for hiding this comment

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

mostly so that I was able to only pass the dataRange to the generator rather than passing the entire scale object

Copy link
Member

Choose a reason for hiding this comment

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

hum, so it could break plugins that modify min and max?

Would not it be simpler to keep min/max (no dataRange) and create the object when calling the generator:

var ticks = me.ticks = Chart.Ticks.generators.linear(numericGeneratorOptions, {
    min: me.min,
    max: me.max
});

Copy link
Member Author

Choose a reason for hiding this comment

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

that would also work. I can refactor this to use that if you prefer

@simonbrunel simonbrunel merged commit d09a17a into master Sep 24, 2016
@simonbrunel simonbrunel deleted the fix/2602 branch September 24, 2016 20:56
@simonbrunel simonbrunel modified the milestone: Version 2.4 Nov 4, 2016
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
…#3281)

Also create a new Chart.Ticks namespace to host common tick generators and formatters.
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