From: Ben McCann Date: Fri, 8 Sep 2017 22:25:51 +0000 (-0700) Subject: Make major ticks optional and off by default (#4723) X-Git-Tag: v2.7.0~1^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=427aa99cb1af43208626048cb2505bf59121dd50;p=thirdparty%2FChart.js.git Make major ticks optional and off by default (#4723) --- diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index dd014e106..27c6255fa 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -284,8 +284,10 @@ function determineMajorUnit(unit) { * responsibility of the calling code to clamp values if needed. */ function generate(min, max, minor, major, capacity, options) { - var stepSize = helpers.valueOrDefault(options.stepSize, options.unitStepSize); - var weekday = minor === 'week' ? options.isoWeekday : false; + var timeOpts = options.time; + var stepSize = helpers.valueOrDefault(timeOpts.stepSize, timeOpts.unitStepSize); + var weekday = minor === 'week' ? timeOpts.isoWeekday : false; + var majorTicksEnabled = options.ticks.major.enabled; var interval = INTERVALS[minor]; var first = moment(min); var last = moment(max); @@ -313,7 +315,7 @@ function generate(min, max, minor, major, capacity, options) { time = moment(first); - if (major && !weekday && !options.round) { + if (majorTicksEnabled && major && !weekday && !timeOpts.round) { // Align the first tick on the previous `minor` unit aligned on the `major` unit: // we first aligned time on the previous `major` unit then add the number of full // stepSize there is between first and the previous major time. @@ -434,7 +436,11 @@ module.exports = function(Chart) { * @see https://github.com/chartjs/Chart.js/pull/4507 * @since 2.7.0 */ - source: 'auto' + source: 'auto', + + major: { + enabled: false + } } }; @@ -564,7 +570,7 @@ module.exports = function(Chart) { break; case 'auto': default: - timestamps = generate(min, max, unit, majorUnit, capacity, timeOpts); + timestamps = generate(min, max, unit, majorUnit, capacity, options); } if (options.bounds === 'ticks' && timestamps.length) { @@ -626,9 +632,10 @@ module.exports = function(Chart) { var majorUnit = me._majorUnit; var majorFormat = me._majorFormat; var majorTime = tick.clone().startOf(me._majorUnit).valueOf(); - var major = majorUnit && majorFormat && time === majorTime; + var majorTickOpts = options.ticks.major; + var major = majorTickOpts.enabled && majorUnit && majorFormat && time === majorTime; var label = tick.format(major ? majorFormat : me._minorFormat); - var tickOpts = major ? options.ticks.major : options.ticks.minor; + var tickOpts = major ? majorTickOpts : options.ticks.minor; var formatter = helpers.valueOrDefault(tickOpts.callback, tickOpts.userCallback); return formatter ? formatter(label, index, ticks) : label; diff --git a/test/specs/scale.time.tests.js b/test/specs/scale.time.tests.js index 3c757243d..ac79368ec 100755 --- a/test/specs/scale.time.tests.js +++ b/test/specs/scale.time.tests.js @@ -94,7 +94,9 @@ describe('Time scale tests', function() { autoSkipPadding: 0, labelOffset: 0, minor: {}, - major: {}, + major: { + enabled: false + }, }, time: { parser: false, @@ -306,7 +308,7 @@ describe('Time scale tests', function() { scale.update(2500, 200); var ticks = getTicksLabels(scale); - expect(ticks).toEqual(['8PM', '9PM', '10PM', '11PM', 'Jan 2', '1AM', '2AM', '3AM', '4AM', '5AM', '6AM', '7AM', '8AM', '9AM', '10AM', '11AM', '12PM', '1PM', '2PM', '3PM', '4PM', '5PM', '6PM', '7PM', '8PM', '9PM']); + expect(ticks).toEqual(['8PM', '9PM', '10PM', '11PM', '12AM', '1AM', '2AM', '3AM', '4AM', '5AM', '6AM', '7AM', '8AM', '9AM', '10AM', '11AM', '12PM', '1PM', '2PM', '3PM', '4PM', '5PM', '6PM', '7PM', '8PM', '9PM']); }); it('build ticks honoring the minUnit', function() { @@ -324,7 +326,7 @@ describe('Time scale tests', function() { var scale = createScale(mockData, config); var ticks = getTicksLabels(scale); - expect(ticks).toEqual(['Jan 2015', 'Jan 2', 'Jan 3']); + expect(ticks).toEqual(['Jan 1', 'Jan 2', 'Jan 3']); }); it('should build ticks using the config diff', function() { @@ -345,7 +347,7 @@ describe('Time scale tests', function() { var ticks = getTicksLabels(scale); // last date is feb 15 because we round to start of week - expect(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']); + expect(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']); }); describe('config step size', function() {