]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Make major ticks optional and off by default (#4723)
authorBen McCann <benjamin.j.mccann@gmail.com>
Fri, 8 Sep 2017 22:25:51 +0000 (15:25 -0700)
committerEvert Timberg <evert.timberg+github@gmail.com>
Fri, 8 Sep 2017 22:25:51 +0000 (18:25 -0400)
src/scales/scale.time.js
test/specs/scale.time.tests.js

index dd014e1069f605a5a10481a6b5b10cbfbb6b492d..27c6255fa0601ea9a86f344a8b06e777f7be99cd 100644 (file)
@@ -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;
index 3c757243d56ea5fc855df5e42c6e91d1d953ee12..ac79368ec51050ef24b227125e00ee1a52a8b5b5 100755 (executable)
@@ -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() {