]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
If tick options have min, max and stepSize use them to generate evenly spaced ticks
authorAidan Fewster <fewstera@gmail.com>
Tue, 4 Oct 2016 10:53:55 +0000 (11:53 +0100)
committerEvert Timberg <evert.timberg+github@gmail.com>
Tue, 4 Oct 2016 13:30:28 +0000 (09:30 -0400)
src/core/core.ticks.js
test/scale.linear.tests.js

index f55b8d40e0a482cfc10e0450cce836dc268a7e05..78ec07c3b94f44ea20c17f1928092ddf9a05662b 100644 (file)
@@ -64,8 +64,17 @@ module.exports = function(Chart) {
                                }
                                var niceMin = Math.floor(dataRange.min / spacing) * spacing;
                                var niceMax = Math.ceil(dataRange.max / spacing) * spacing;
-                               var numSpaces = (niceMax - niceMin) / spacing;
 
+                               // If min, max and stepSize is set and they make an evenly spaced scale use it.
+                               if (generationOptions.min && generationOptions.max && generationOptions.stepSize) {
+                                       var minMaxDeltaDivisableByStepSize = ((generationOptions.max - generationOptions.min) % generationOptions.stepSize) === 0;
+                                       if (minMaxDeltaDivisableByStepSize) {
+                                               niceMin = generationOptions.min;
+                                               niceMax = generationOptions.max;
+                                       }
+                               }
+
+                               var numSpaces = (niceMax - niceMin) / spacing;
                                // If very close to our rounded value, use it.
                                if (helpers.almostEquals(numSpaces, Math.round(numSpaces), spacing / 1000)) {
                                        numSpaces = Math.round(numSpaces);
index 3df77325848b1d7e6b82f98f887762c47357da9e..4eb585a7b66f04196acfb3c0fa2ef0e61431cd91 100644 (file)
@@ -486,6 +486,38 @@ describe('Linear Scale', function() {
                expect(chart.scales.yScale0.ticks[chart.scales.yScale0.ticks.length - 1]).toBe('-1010');
        });
 
+       it('Should use min, max and stepSize to create fixed spaced ticks', function() {
+               var chart = window.acquireChart({
+                       type: 'bar',
+                       data: {
+                               datasets: [{
+                                       yAxisID: 'yScale0',
+                                       data: [10, 3, 6, 8, 3, 1]
+                               }],
+                               labels: ['a', 'b', 'c', 'd', 'e', 'f']
+                       },
+                       options: {
+                               scales: {
+                                       yAxes: [{
+                                               id: 'yScale0',
+                                               type: 'linear',
+                                               ticks: {
+                                                       min: 1,
+                                                       max: 11,
+                                                       stepSize: 2
+                                               }
+                                       }]
+                               }
+                       }
+               });
+
+               expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
+               expect(chart.scales.yScale0.min).toBe(1);
+               expect(chart.scales.yScale0.max).toBe(11);
+               expect(chart.scales.yScale0.ticks).toEqual(['11', '9', '7', '5', '3', '1']);
+       });
+
+
        it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
                var chart = window.acquireChart({
                        type: 'bar',