-
Notifications
You must be signed in to change notification settings - Fork 12k
Improve tick generation for linear scales #5938
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,35 +16,41 @@ function generateTicks(generationOptions, dataRange) { | |
| // for details. | ||
|
|
||
| var stepSize = generationOptions.stepSize; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
var stepSize = Math.max(0, generationOptions.stepSize);
var unit = stepSize || 1;
//... |
||
| var unit = stepSize > 0 ? stepSize : 1; | ||
| var maxNumSpaces = generationOptions.maxTicks - 1; | ||
| var min = generationOptions.min; | ||
| var max = generationOptions.max; | ||
| var spacing, precision, factor, niceRange, niceMin, niceMax, numSpaces; | ||
| var precision = generationOptions.precision; | ||
| var spacing, factor, niceMin, niceMax, numSpaces; | ||
|
|
||
| // spacing is set to a nice number of the dataRange divided by maxNumSpaces. | ||
| // stepSize is used as a minimum unit if it is specified. | ||
| spacing = helpers.niceNum((dataRange.max - dataRange.min) / maxNumSpaces / unit) * unit; | ||
| numSpaces = Math.ceil(dataRange.max / spacing) - Math.floor(dataRange.min / spacing); | ||
| if (numSpaces > maxNumSpaces) { | ||
| // If the calculated num of spaces exceeds maxNumSpaces, recalculate it | ||
| spacing = helpers.niceNum(numSpaces * spacing / maxNumSpaces / unit) * unit; | ||
| } | ||
|
|
||
| if (stepSize && stepSize > 0) { | ||
| spacing = stepSize; | ||
| if (!(stepSize > 0) && !helpers.isNullOrUndef(precision)) { | ||
simonbrunel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // If the user specified a precision, round to that number of decimal places | ||
| factor = Math.pow(10, precision); | ||
| spacing = Math.ceil(spacing * factor) / factor; | ||
| } else { | ||
| niceRange = helpers.niceNum(dataRange.max - dataRange.min, false); | ||
| spacing = helpers.niceNum(niceRange / (generationOptions.maxTicks - 1), true); | ||
|
|
||
| precision = generationOptions.precision; | ||
| if (!helpers.isNullOrUndef(precision)) { | ||
| // If the user specified a precision, round to that number of decimal places | ||
| factor = Math.pow(10, precision); | ||
| spacing = Math.ceil(spacing * factor) / factor; | ||
| } | ||
| } | ||
| // If a precision is not specified, calculate factor based on spacing | ||
| if (!factor) { | ||
| // If a precision is not specified, calculate factor based on spacing | ||
| factor = Math.pow(10, helpers.decimalPlaces(spacing)); | ||
| } | ||
|
|
||
| niceMin = Math.floor(dataRange.min / spacing) * spacing; | ||
| niceMax = Math.ceil(dataRange.max / spacing) * spacing; | ||
|
|
||
| // If min, max and stepSize is set and they make an evenly spaced scale use it. | ||
| if (!helpers.isNullOrUndef(min) && !helpers.isNullOrUndef(max) && stepSize) { | ||
| if (stepSize > 0) { | ||
simonbrunel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // If very close to our whole number, use it. | ||
| if (helpers.almostWhole((max - min) / stepSize, spacing / 1000)) { | ||
| if (!helpers.isNullOrUndef(min) && helpers.almostWhole(min / spacing, spacing / 1000)) { | ||
| niceMin = min; | ||
| } | ||
| if (!helpers.isNullOrUndef(max) && helpers.almostWhole(max / spacing, spacing / 1000)) { | ||
| niceMax = max; | ||
| } | ||
| } | ||
|
|
@@ -155,7 +161,7 @@ module.exports = function(Chart) { | |
| var tickOpts = opts.ticks; | ||
|
|
||
| // Figure out what the max number of ticks we can support it is based on the size of | ||
| // the axis area. For now, we say that the minimum tick spacing in pixels must be 50 | ||
| // the axis area. For now, we say that the minimum tick spacing in pixels must be 40 | ||
| // We also limit the maximum number of ticks to 11 which gives a nice 10 squares on | ||
| // the graph. Make sure we always have at least 2 ticks | ||
| var maxTicks = me.getTickLimit(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.