-
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
Time axis ticks formatting for base and senior unit #4268
Changes from 13 commits
0fc6ed3
0572606
890f1c5
f5ddc57
d43f9ee
b471c73
d88a03a
dd66acb
86c4259
241ad82
086bf49
7242f96
5bd9cb6
3ab65fc
7bcae92
0dd966f
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 |
---|---|---|
|
@@ -487,6 +487,7 @@ module.exports = function(Chart) { | |
var context = me.ctx; | ||
var globalDefaults = Chart.defaults.global; | ||
var optionTicks = options.ticks; | ||
var optionMajorTicks = options.majorTicks ? options.majorTicks : optionTicks; | ||
var gridLines = options.gridLines; | ||
var scaleLabel = options.scaleLabel; | ||
|
||
|
@@ -503,6 +504,8 @@ module.exports = function(Chart) { | |
|
||
var tickFontColor = helpers.getValueOrDefault(optionTicks.fontColor, globalDefaults.defaultFontColor); | ||
var tickFont = parseFontOptions(optionTicks); | ||
var majorTickFontColor = helpers.getValueOrDefault(optionMajorTicks.fontColor, globalDefaults.defaultFontColor); | ||
var majorTickFont = parseFontOptions(optionMajorTicks); | ||
|
||
var tl = gridLines.drawTicks ? gridLines.tickMarkLength : 0; | ||
|
||
|
@@ -513,9 +516,6 @@ module.exports = function(Chart) { | |
var cosRotation = Math.cos(labelRotationRadians); | ||
var longestRotatedLabel = me.longestLabelWidth * cosRotation; | ||
|
||
// Make sure we draw text in the correct color and font | ||
context.fillStyle = tickFontColor; | ||
|
||
var itemsToDraw = []; | ||
|
||
if (isHorizontal) { | ||
|
@@ -547,7 +547,8 @@ module.exports = function(Chart) { | |
var yTickStart = options.position === 'bottom' ? me.top : me.bottom - tl; | ||
var yTickEnd = options.position === 'bottom' ? me.top + tl : me.bottom; | ||
|
||
helpers.each(me.ticks, function(label, index) { | ||
helpers.each(me.ticks, function(tick, index) { | ||
var label = typeof tick === 'object' && typeof tick.value !== 'undefined' ? tick.value : tick; | ||
// If the callback returned a null or undefined value, do not draw this line | ||
if (label === undefined || label === null) { | ||
return; | ||
|
@@ -645,6 +646,7 @@ module.exports = function(Chart) { | |
glBorderDashOffset: borderDashOffset, | ||
rotation: -1 * labelRotationRadians, | ||
label: label, | ||
major: tick.major === true, | ||
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. Why not simply: |
||
textBaseline: textBaseline, | ||
textAlign: textAlign | ||
}); | ||
|
@@ -678,10 +680,12 @@ module.exports = function(Chart) { | |
} | ||
|
||
if (optionTicks.display) { | ||
// Make sure we draw text in the correct color and font | ||
context.save(); | ||
context.translate(itemToDraw.labelX, itemToDraw.labelY); | ||
context.rotate(itemToDraw.rotation); | ||
context.font = tickFont.font; | ||
context.font = itemToDraw.major ? majorTickFont.font : tickFont.font; | ||
context.fillStyle = itemToDraw.major ? majorTickFontColor : tickFontColor; | ||
context.textBaseline = itemToDraw.textBaseline; | ||
context.textAlign = itemToDraw.textAlign; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,9 @@ module.exports = function(Chart) { | |
displayFormats: { | ||
millisecond: 'h:mm:ss.SSS a', // 11:20:01.123 AM, | ||
second: 'h:mm:ss a', // 11:20:01 AM | ||
minute: 'h:mm:ss a', // 11:20:01 AM | ||
hour: 'MMM D, hA', // Sept 4, 5PM | ||
day: 'll', // Sep 4 2015 | ||
minute: 'h:mm a', // 11:20 AM | ||
hour: 'hA', // 5PM | ||
day: 'MMM D', // Sep 4 | ||
week: 'll', // Week 46, or maybe "[W]WW - YYYY" ? | ||
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. I think we should change this to |
||
month: 'MMM YYYY', // Sept 2015 | ||
quarter: '[Q]Q - YYYY', // Q3 | ||
|
@@ -36,6 +36,19 @@ module.exports = function(Chart) { | |
}, | ||
ticks: { | ||
autoSkip: false | ||
}, | ||
majorTicks: { | ||
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. I liked to have all tick options scoped under the
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. Also, defaults 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. If minor ticks will fetch their options from 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. I mean, global options for ticks: {
minor: {}, //< use common tick options
major: {} //< use common tick options
} and we keep current global
It's not a breaking change because minor and major ticks would use the 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. @etimberg @benmccann thoughts? 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. Yes, @simonbrunel, I like your suggestion 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. @simonbrunel I like that suggestion as well 😄 |
||
beginAtZero: false, | ||
minRotation: 0, | ||
maxRotation: 50, | ||
mirror: false, | ||
padding: 0, | ||
reverse: false, | ||
display: true, | ||
autoSkip: true, | ||
autoSkipPadding: 0, | ||
labelOffset: 0, | ||
callback: Chart.Ticks.formatters.values | ||
} | ||
}; | ||
|
||
|
@@ -45,8 +58,17 @@ module.exports = function(Chart) { | |
throw new Error('Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com'); | ||
} | ||
|
||
this.mergeTicksOptions(); | ||
|
||
Chart.Scale.prototype.initialize.call(this); | ||
}, | ||
mergeTicksOptions: function() { | ||
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. The docs for this function imply that it should be applicable to all scales. Should 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. Also, we should really mark all internal members private and make them public only if requested. 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. Sorry I can't find docs for 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. This method should apply to all scale, so the new 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. Ok understood. Thanks! |
||
for (var key in this.options.ticks) { | ||
if (typeof this.options.majorTicks[key] === 'undefined') { | ||
this.options.majorTicks[key] = this.options.ticks[key]; | ||
} | ||
} | ||
}, | ||
determineDataLimits: function() { | ||
var me = this; | ||
var timeOpts = me.options.time; | ||
|
@@ -182,14 +204,34 @@ module.exports = function(Chart) { | |
}, | ||
// Function to format an individual tick mark | ||
tickFormatFunction: function(tick, index, ticks) { | ||
var formattedTick = tick.format(this.displayFormat); | ||
var tickOpts = this.options.ticks; | ||
var formattedTick; | ||
var tickClone = tick.clone(); | ||
var tickTimestamp = tick.valueOf(); | ||
var major = false; | ||
var tickOpts; | ||
if (this.majorUnit && this.majorDisplayFormat && tickTimestamp === tickClone.startOf(this.majorUnit).valueOf()) { | ||
// format as senior unit | ||
formattedTick = tick.format(this.majorDisplayFormat); | ||
tickOpts = this.options.majorTicks; | ||
major = true; | ||
} else { | ||
// format as base unit | ||
formattedTick = tick.format(this.displayFormat); | ||
tickOpts = this.options.ticks; | ||
} | ||
|
||
var callback = helpers.getValueOrDefault(tickOpts.callback, tickOpts.userCallback); | ||
|
||
if (callback) { | ||
return callback(formattedTick, index, ticks); | ||
return { | ||
value: callback(formattedTick, index, ticks), | ||
major: major | ||
}; | ||
} | ||
return formattedTick; | ||
return { | ||
value: formattedTick, | ||
major: major | ||
}; | ||
}, | ||
convertTicksToLabels: function() { | ||
var me = this; | ||
|
@@ -257,11 +299,12 @@ module.exports = function(Chart) { | |
var me = this; | ||
|
||
me.displayFormat = me.options.time.displayFormats.millisecond; // Pick the longest format for guestimation | ||
var exampleLabel = me.tickFormatFunction(moment(exampleTime), 0, []); | ||
var exampleLabel = me.tickFormatFunction(moment(exampleTime), 0, []).value; | ||
var tickLabelWidth = me.getLabelWidth(exampleLabel); | ||
|
||
var innerWidth = me.isHorizontal() ? me.width : me.height; | ||
var labelCapacity = innerWidth / tickLabelWidth; | ||
|
||
return labelCapacity; | ||
} | ||
}); | ||
|
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.
var label = (tick && tick.value) || tick;