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

Refactor autoskip functionality into a separate method #4614

Merged
merged 1 commit into from
Aug 7, 2017

Conversation

benmccann
Copy link
Contributor

@benmccann benmccann commented Aug 4, 2017

This is the first step towards improving the auto-skip functionality. I haven't attempted to make any functionality changes in this PR. My only goal here is to refactor the functionality into a self-contained method as a starting point towards further improvements.

I also haven't moved the calculation to happen outside of draw. I'm not confident I know this section of the code well enough yet to do that safely and would prefer to save that for later when I have a better understanding and am sure the change is safe.

@benmccann benmccann force-pushed the autoskip branch 2 times, most recently from abb0d05 to edfaaf7 Compare August 4, 2017 01:55
* Returns a subset of ticks to be plotted.
* This functionality makes it so that we don't have overlapping labels.
*/
autoSkip: function() {
Copy link
Member

Choose a reason for hiding this comment

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

I would call it _disseminate and make it explicitly @private. I would also add the ticks as parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had debated making ticks a parameter, so I've changed that since you've suggested it as well

I had also though about making it private, but I think that it's a method that users are potentially likely to want to override with their own implementation

I'm not a big fan of disseminate as a name. We're not really spreading the ticks, but rather are skipping some

Copy link
Member

Choose a reason for hiding this comment

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

I'm not a big fan of autoSkip, but actually was thinking of _decimate (not _disseminate). We should make it private until there is a need/request for it to be public, we don't want to maintain this kind of backward compatibility.

// if they defined a max number of optionTicks,
// increase skipRatio until that number is met
if (maxTicks && tickCount > maxTicks) {
while (!skipRatio || tickCount / (skipRatio || 1) > maxTicks) {
Copy link
Member

@simonbrunel simonbrunel Aug 4, 2017

Choose a reason for hiding this comment

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

Do we really need a loop here? Maybe something like (not tested):

skipRatio = Math.max(skipRatio, ~~(tickCount/maxTicks));

Copy link
Contributor Author

@benmccann benmccann Aug 4, 2017

Choose a reason for hiding this comment

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

I didn't want to change the logic too much in this PR. I figure it's probably all going to be mostly rewritten, so not worth investing in. And also I want to minimize changes before adding some more tests to ensure the logic is right. This PR helps work towards that by structuring the code so that it will be easier to test

Copy link
Member

Choose a reason for hiding this comment

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

We don't know how long it will take to get it fully rewritten, neither if it will land in 2.7.0. Let's take time to cleanup the code while refactoring it. Else there is no need for this kind of PR and I would prefer to merge the final one with a full overview of the new implementation.

Copy link
Member

Choose a reason for hiding this comment

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

Of course, that only applies if my suggested formula correctly works :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. yeah, your formula is correct

}
}

helpers.each(me._ticks, function(tick, index) {
Copy link
Member

Choose a reason for hiding this comment

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

I would convert this each into a regular for() loop since there might be potentially lot of ticks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

var cosRotation = Math.cos(labelRotationRadians);
var longestRotatedLabel = me.longestLabelWidth * cosRotation;
var result = [];
var ilen = ticks.length;
Copy link
Member

Choose a reason for hiding this comment

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

Let's choose between ilen or tickCount :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. good catch

@etimberg
Copy link
Member

etimberg commented Aug 4, 2017

For context, the branch where I played around with tick alignment is https://github.com/chartjs/Chart.js/blob/vertical-tick-alignment/src/core/core.scale.js. There are some changes in calculateTickRotation. There are also some changes to try and work on the auto-skipper. It's definitely not production ready, but could be a useful data point

@benmccann benmccann force-pushed the autoskip branch 4 times, most recently from ebd0857 to 8dd3dfc Compare August 5, 2017 15:47
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.

I think this is a good intermediate step towards cleaning all this up

// Since we always show the last tick,we need may need to hide the last shown one before
var shouldSkip = (skipRatio > 1 && i % skipRatio > 0) || (i % skipRatio === 0 && i + skipRatio >= tickCount);
if (shouldSkip && !isLastTick) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

continue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks. missed that one when converting from forEach


// Since we always show the last tick,we need may need to hide the last shown one before
var shouldSkip = (skipRatio > 1 && i % skipRatio > 0) || (i % skipRatio === 0 && i + skipRatio >= tickCount);
if (shouldSkip && !isLastTick) {
Copy link
Member

Choose a reason for hiding this comment

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

Why || helpers.isNullOrUndef(label) is not needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmm. I can't remember anymore. I've added it back. I really don't care about improving this code as I want to rewrite it all

Copy link
Member

Choose a reason for hiding this comment

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

I really care about everything that is merged in master :)


// Since we always show the last tick,we need may need to hide the last shown one before
var shouldSkip = (skipRatio > 1 && i % skipRatio > 0) || (i % skipRatio === 0 && i + skipRatio >= tickCount);
if (shouldSkip && !isLastTick) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we replace isLastTick by i < tickCount - 1 and remove the isLastTick variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

if (shouldSkip && !isLastTick) {
return;
}
result.push(tick);
Copy link
Member

Choose a reason for hiding this comment

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

Can we replace tick by ticks[i] and remove the tick variable? (except if the test on label is needed)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added back the test on label. It actually does look like it should be there

}

for (i = 0; i < tickCount; i++) {
var tick = ticks[i];
Copy link
Member

Choose a reason for hiding this comment

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

Can we move all variable declarations outside the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


// Since we always show the last tick,we need may need to hide the last shown one before
shouldSkip = (skipRatio > 1 && i % skipRatio > 0) || (i % skipRatio === 0 && i + skipRatio >= tickCount);
if (shouldSkip && i !== tickCount - 1 || helpers.isNullOrUndef(label)) {
Copy link
Member

Choose a reason for hiding this comment

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

You don't need this intermediary label variable, you can use tick.label directly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

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

A minor cleanup (label) then good to be merged.

@simonbrunel simonbrunel added this to the Version 2.7 milestone Aug 7, 2017
@simonbrunel simonbrunel merged commit 1eeffa3 into chartjs:master Aug 7, 2017
yofreke pushed a commit to yofreke/Chart.js that referenced this pull request Dec 30, 2017
@benmccann benmccann deleted the autoskip branch February 10, 2019 05:54
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
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