Merge branch 'iso_weekday' of https://github.com/tomduncalf/Chart.js into tomduncalf-iso_weekday

Conflicts:
	docs/01-Scales.md
This commit is contained in:
Evert Timberg 2016-05-12 18:24:14 -04:00
commit 8c209405e9
3 changed files with 49 additions and 7 deletions

View File

@ -183,6 +183,9 @@ The time scale extends the core scale class with the following tick template:
// string - By default, no rounding is applied. To round, set to a supported time unit eg. 'week', 'month', 'year', etc.
round: false,
// Number - By default, the week and therefore the scale starts on the locale defined week if the unit is 'week'. To override the start day of the week, set this to an integer between 1 and 7 - see http://momentjs.com/docs/#/get-set/iso-weekday/
isoWeekday: false,
// Moment js for each of the units. Replaces `displayFormat`
// To override, use a pattern string from http://momentjs.com/docs/#/displaying/format/
displayFormats: {

View File

@ -47,6 +47,7 @@ module.exports = function(Chart) {
unit: false, // false == automatic or override with week, month, year, etc.
round: false, // none, or override with week, month, year, etc.
displayFormat: false, // DEPRECATED
isoWeekday: false, // override week start day - see http://momentjs.com/docs/#/get-set/iso-weekday/
// defaults to unit's corresponding unitFormat below or override using pattern string from http://momentjs.com/docs/#/displaying/format/
displayFormats: {
@ -77,6 +78,13 @@ module.exports = function(Chart) {
getLabelMoment: function(datasetIndex, index) {
return this.labelMoments[datasetIndex][index];
},
getMomentStartOf: function(tick) {
if (this.options.time.unit === 'week' && this.options.time.isoWeekday !== false) {
return tick.clone().startOf('isoWeek').isoWeekday(this.options.time.isoWeekday);
} else {
return tick.clone().startOf(this.tickUnit);
}
},
determineDataLimits: function() {
this.labelMoments = [];
@ -208,8 +216,8 @@ module.exports = function(Chart) {
unitDefinition = time.units[unitDefinitionIndex];
this.tickUnit = unitDefinition.name;
var leadingUnitBuffer = this.firstTick.diff(this.firstTick.clone().startOf(this.tickUnit), this.tickUnit, true);
var trailingUnitBuffer = this.lastTick.clone().add(1, this.tickUnit).startOf(this.tickUnit).diff(this.lastTick, this.tickUnit, true);
var leadingUnitBuffer = this.firstTick.diff(this.getMomentStartOf(this.firstTick), this.tickUnit, true);
var trailingUnitBuffer = this.getMomentStartOf(this.lastTick.clone().add(1, this.tickUnit)).diff(this.lastTick, this.tickUnit, true);
this.scaleSizeInUnits = this.lastTick.diff(this.firstTick, this.tickUnit, true) + leadingUnitBuffer + trailingUnitBuffer;
this.displayFormat = this.options.time.displayFormats[unitDefinition.name];
}
@ -220,18 +228,18 @@ module.exports = function(Chart) {
// Only round the first tick if we have no hard minimum
if (!this.options.time.min) {
this.firstTick.startOf(this.tickUnit);
this.firstTick = this.getMomentStartOf(this.firstTick);
roundedStart = this.firstTick;
} else {
roundedStart = this.firstTick.clone().startOf(this.tickUnit);
roundedStart = this.getMomentStartOf(this.firstTick);
}
// Only round the last tick if we have no hard maximum
if (!this.options.time.max) {
var roundedEnd = this.lastTick.clone().startOf(this.tickUnit);
var roundedEnd = this.getMomentStartOf(this.lastTick);
if (roundedEnd.diff(this.lastTick, this.tickUnit, true) !== 0) {
// Do not use end of because we need this to be in the next time unit
this.lastTick.add(1, this.tickUnit).startOf(this.tickUnit);
this.lastTick = this.getMomentStartOf(this.lastTick.add(1, this.tickUnit));
}
}
@ -278,7 +286,7 @@ module.exports = function(Chart) {
this.scaleSizeInUnits = this.lastTick.diff(this.firstTick, this.tickUnit, true);
}
}
this.ctx.restore();
},
// Get tooltip label

View File

@ -79,6 +79,7 @@ describe('Time scale tests', function() {
format: false,
unit: false,
round: false,
isoWeekday: false,
displayFormat: false,
displayFormats: {
'millisecond': 'h:mm:ss.SSS a', // 11:20:01.123 AM
@ -328,6 +329,36 @@ describe('Time scale tests', function() {
expect(scale.ticks).toEqual([ 'Jan 1, 2015', 'Jan 5, 2015' ]);
});
it('Should use the isoWeekday option', function() {
var scaleID = 'myScale';
var mockData = {
labels: [
"2015-01-01T20:00:00", // Thursday
"2015-01-02T20:00:00", // Friday
"2015-01-03T20:00:00" // Saturday
]
};
var mockContext = window.createMockContext();
var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('time'));
config.time.unit = 'week';
// Wednesday
config.time.isoWeekday = 3;
var Constructor = Chart.scaleService.getScaleConstructor('time');
var scale = new Constructor({
ctx: mockContext,
options: config, // use default config for scale
chart: {
data: mockData
},
id: scaleID
});
scale.update(400, 50);
expect(scale.ticks).toEqual([ 'Dec 31, 2014', 'Jan 7, 2015' ]);
});
it('should get the correct pixel for a value', function() {
chartInstance = window.acquireChart({
type: 'line',