-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Conversation
abb0d05
to
edfaaf7
Compare
src/core/core.scale.js
Outdated
* Returns a subset of ticks to be plotted. | ||
* This functionality makes it so that we don't have overlapping labels. | ||
*/ | ||
autoSkip: function() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
src/core/core.scale.js
Outdated
// 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) { |
There was a problem hiding this comment.
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));
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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
src/core/core.scale.js
Outdated
} | ||
} | ||
|
||
helpers.each(me._ticks, function(tick, index) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/core/core.scale.js
Outdated
var cosRotation = Math.cos(labelRotationRadians); | ||
var longestRotatedLabel = me.longestLabelWidth * cosRotation; | ||
var result = []; | ||
var ilen = ticks.length; |
There was a problem hiding this comment.
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
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. good catch
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 |
ebd0857
to
8dd3dfc
Compare
There was a problem hiding this 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
src/core/core.scale.js
Outdated
// 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continue
?
There was a problem hiding this comment.
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
src/core/core.scale.js
Outdated
|
||
// 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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
src/core/core.scale.js
Outdated
|
||
// 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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/core/core.scale.js
Outdated
if (shouldSkip && !isLastTick) { | ||
return; | ||
} | ||
result.push(tick); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
src/core/core.scale.js
Outdated
} | ||
|
||
for (i = 0; i < tickCount; i++) { | ||
var tick = ticks[i]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/core/core.scale.js
Outdated
|
||
// 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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this 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.
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.