-
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
Honour axis min/max settings #4522
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
var moment = require('moment'); | ||
moment = typeof(moment) === 'function' ? moment : window.moment; | ||
|
||
var helpers = require('./helpers.core'); | ||
|
||
var interval = { | ||
millisecond: { | ||
size: 1, | ||
|
@@ -53,25 +55,35 @@ function generateTicksNiceRange(options, dataRange, niceRange) { | |
var ticks = []; | ||
if (options.maxTicks) { | ||
var stepSize = options.stepSize; | ||
var startTick = options.min !== undefined ? options.min : niceRange.min; | ||
var startTick = helpers.isNullOrUndef(options.min) ? niceRange.min : options.min; | ||
var majorUnit = options.majorUnit; | ||
var majorUnitStart = majorUnit ? moment(startTick).add(1, majorUnit).startOf(majorUnit) : startTick; | ||
var startRange = majorUnitStart.valueOf() - startTick; | ||
var stepValue = interval[options.unit].size * stepSize; | ||
var startFraction = startRange % stepValue; | ||
var alignedTick = startTick; | ||
if (startFraction && majorUnit && !options.timeOpts.round && !options.timeOpts.isoWeekday) { | ||
|
||
// first tick | ||
if (startFraction && majorUnit && !options.timeOpts.round && !options.timeOpts.isoWeekday && helpers.isNullOrUndef(options.min)) { | ||
alignedTick += startFraction - stepValue; | ||
ticks.push(alignedTick); | ||
} else { | ||
ticks.push(startTick); | ||
} | ||
|
||
// generate remaining ticks | ||
var cur = moment(alignedTick); | ||
var realMax = options.max || niceRange.max; | ||
var realMax = helpers.isNullOrUndef(options.max) ? niceRange.max : options.max; | ||
while (cur.add(stepSize, options.unit).valueOf() < realMax) { | ||
ticks.push(cur.valueOf()); | ||
} | ||
ticks.push(cur.valueOf()); | ||
|
||
// last tick | ||
if (helpers.isNullOrUndef(options.max)) { | ||
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. minor question. could we always just push 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. You can't because |
||
ticks.push(cur.valueOf()); | ||
} else { | ||
ticks.push(realMax); | ||
} | ||
} | ||
return 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.
I agree that this if is dubious. I'm not sure why
isoWeekday
is doing things 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.
I can't quite tell what the
isoWeekday
option is supposed to do from the docs. Looking at the code it seems that perhaps that settingisoWeekday
makes every tick a Monday. If you're manually setting each tick to a Monday then it'd make sense not to do smart alignment here, but it's quite difficult to tell what this option is. I tried to build a sample to test it out, but the time samples seem to be currently broken on master. After #4507 is merged I imagine the samples will be working again and then we can take a stab at better documenting that feature and seeing if we should removeisoWeekday
hereThere 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'd leave it unchanged for now as removing it breaks this https://github.com/chartjs/Chart.js/blob/master/test/specs/scale.time.tests.js#L331 test. The test itself is odd as
isoWeekDay
should be a boolean but the tests uses numerical value.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.
My understanding of
isoWeekDay
is that's a number representing the day (1: Monday - 7: Sunday) that should be considered the first day of the week and I guess thatisoWeekDay: true
is an alias to 1 (Monday).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.
Then the docs are off: https://github.com/chartjs/Chart.js/blob/master/docs/axes/cartesian/time.md shows its boolean.