Skip to content

Commit

Permalink
Reamed seniorDisplayFormat to majorDisplayFormat. Changed aligned tic…
Browse files Browse the repository at this point in the history
…k 'if' statement formatting. Removed isoWeekday from generateTick ooptions due to redundancy.
  • Loading branch information
andrew.hurskiy committed May 22, 2017
1 parent b96df87 commit 76a3d19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
30 changes: 18 additions & 12 deletions src/scales/scale.time.js
Expand Up @@ -184,16 +184,11 @@ module.exports = function(Chart) {
var startFraction = startRange % (interval[options.unit].size * stepSize);
var alignedTick = startTick;
ticks.push(startTick);
if (startTick !== alignedTick + startFraction &&
options.majorUnit &&
!options.timeOpts.round &&
!options.timeOpts.isoWeekday) {
if (startTick !== alignedTick + startFraction && options.majorUnit && !options.timeOpts.round && !options.timeOpts.isoWeekday) {
alignedTick += startFraction;
ticks.push(alignedTick);
}

var cur = moment(alignedTick);

while (cur.add(stepSize, options.unit).valueOf() < niceRange.max) {
ticks.push(cur.valueOf());
}
Expand All @@ -202,7 +197,6 @@ module.exports = function(Chart) {
ticks.push(realMax);
}
}

return ticks;
}

Expand All @@ -215,7 +209,7 @@ module.exports = function(Chart) {
Chart.Ticks.generators.time = function(options, dataRange) {
var niceMin;
var niceMax;
var isoWeekday = options.isoWeekday;
var isoWeekday = options.timeOpts.isoWeekday;
if (options.unit === 'week' && isoWeekday !== false) {
niceMin = moment(dataRange.min).startOf('isoWeek').isoWeekday(isoWeekday).valueOf();
niceMax = moment(dataRange.max).startOf('isoWeek').isoWeekday(isoWeekday);
Expand Down Expand Up @@ -341,7 +335,9 @@ module.exports = function(Chart) {
}

me.displayFormat = timeOpts.displayFormats[unit];
me.seniorDisplayFormat = timeOpts.displayFormats[majorUnit];
me.majorDisplayFormat = timeOpts.displayFormats[majorUnit];
me.unit = unit;
me.majorUnit = majorUnit;

var stepSize = timeOpts.stepSize || determineStepSize(minTimestamp || dataMin, maxTimestamp || dataMax, unit, maxTicks);

Expand All @@ -352,8 +348,7 @@ module.exports = function(Chart) {
stepSize: stepSize,
majorUnit: majorUnit,
unit: unit,
timeOpts: timeOpts,
isoWeekday: timeOpts.isoWeekday
timeOpts: timeOpts
}, {
min: dataMin,
max: dataMax
Expand Down Expand Up @@ -383,7 +378,18 @@ module.exports = function(Chart) {
},
// Function to format an individual tick mark
tickFormatFunction: function(tick, index, ticks) {
var formattedTick = tick.format(this.displayFormat);
var formattedTick;
var tickClone = tick.clone();
if (this.majorUnit &&
this.majorDisplayFormat &&
tick.valueOf() === tickClone.startOf(this.majorUnit).valueOf()) {
// format as senior unit
formattedTick = tick.format(this.majorDisplayFormat);
} else {
// format as base unit
formattedTick = tick.format(this.displayFormat);
}

var tickOpts = this.options.ticks;
var callback = helpers.getValueOrDefault(tickOpts.callback, tickOpts.userCallback);

Expand Down
12 changes: 4 additions & 8 deletions test/specs/scale.time.tests.js
Expand Up @@ -231,7 +231,7 @@ describe('Time scale tests', function() {

var scale = createScale(mockData, config);
scale.update(2500, 200);
expect(scale.ticks).toEqual(['Jan 1, 8PM', 'Jan 1, 9PM', 'Jan 1, 10PM', 'Jan 1, 11PM', 'Jan 2, 12AM', 'Jan 2, 1AM', 'Jan 2, 2AM', 'Jan 2, 3AM', 'Jan 2, 4AM', 'Jan 2, 5AM', 'Jan 2, 6AM', 'Jan 2, 7AM', 'Jan 2, 8AM', 'Jan 2, 9AM', 'Jan 2, 10AM', 'Jan 2, 11AM', 'Jan 2, 12PM', 'Jan 2, 1PM', 'Jan 2, 2PM', 'Jan 2, 3PM', 'Jan 2, 4PM', 'Jan 2, 5PM', 'Jan 2, 6PM', 'Jan 2, 7PM', 'Jan 2, 8PM', 'Jan 2, 9PM']);
expect(scale.ticks).toEqual(['Jan 1, 8PM', 'Jan 1, 9PM', 'Jan 1, 10PM', 'Jan 1, 11PM', 'Jan 2, 2015', 'Jan 2, 1AM', 'Jan 2, 2AM', 'Jan 2, 3AM', 'Jan 2, 4AM', 'Jan 2, 5AM', 'Jan 2, 6AM', 'Jan 2, 7AM', 'Jan 2, 8AM', 'Jan 2, 9AM', 'Jan 2, 10AM', 'Jan 2, 11AM', 'Jan 2, 12PM', 'Jan 2, 1PM', 'Jan 2, 2PM', 'Jan 2, 3PM', 'Jan 2, 4PM', 'Jan 2, 5PM', 'Jan 2, 6PM', 'Jan 2, 7PM', 'Jan 2, 8PM', 'Jan 2, 9PM']);
});

it('build ticks honoring the minUnit', function() {
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('Time scale tests', function() {
scale.update(800, 200);

// last date is feb 15 because we round to start of week
expect(scale.ticks).toEqual(['Dec 28, 2014', 'Jan 4, 2015', 'Jan 11, 2015', 'Jan 18, 2015', 'Jan 25, 2015', 'Feb 1, 2015', 'Feb 8, 2015', 'Feb 15, 2015']);
expect(scale.ticks).toEqual(['Dec 28, 2014', 'Jan 4, 2015', 'Jan 11, 2015', 'Jan 18, 2015', 'Jan 25, 2015', 'Feb 2015', 'Feb 8, 2015', 'Feb 15, 2015']);
});

describe('when specifying limits', function() {
Expand Down Expand Up @@ -463,13 +463,9 @@ describe('Time scale tests', function() {
});

var xScale = chart.scales.xScale0;
var pixel = xScale.getPixelForValue('', 0, 0);

expect(xScale.getPixelForValue('', 0, 0)).toBeCloseToPixel(62);

expect(xScale.getValueForPixel(62)).toBeCloseToTime({
value: moment(chart.data.labels[0]),
unit: 'day',
});
expect(xScale.getValueForPixel(pixel).valueOf()).toEqual(moment(chart.data.labels[0]).valueOf());
});

it('does not create a negative width chart when hidden', function() {
Expand Down

0 comments on commit 76a3d19

Please sign in to comment.