]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Add suggestedMin, suggestedMax, min, and max options to radialLinear scale 1848/head
authoretimberg <evert.timberg@gmail.com>
Sun, 3 Jan 2016 02:03:09 +0000 (21:03 -0500)
committeretimberg <evert.timberg@gmail.com>
Sun, 3 Jan 2016 02:03:09 +0000 (21:03 -0500)
src/scales/scale.radialLinear.js
test/scale.radialLinear.tests.js

index 77eb9a790351a404ada4821233a6e6cc22a543c0..7d901aaf0b91c2f42e72733e0b5edebaf2829969 100644 (file)
                                }
                        }, this);
 
-                       if (this.min === this.max) {
-                               this.min--;
-                               this.max++;
-                       }
-
                        // If we are forcing it to begin at 0, but 0 will already be rendered on the chart,
                        // do nothing since that would make the chart weird. If the user really wants a weird chart
                        // axis, they can manually override it
                                        this.min = 0;
                                }
                        }
+
+                       if (this.options.ticks.min !== undefined) {
+                               this.min = this.options.ticks.min;
+                       } else if (this.options.ticks.suggestedMin !== undefined) {
+                               this.min = Math.min(this.min, this.options.ticks.suggestedMin);
+                       }
+
+                       if (this.options.ticks.max !== undefined) {
+                               this.max = this.options.ticks.max;
+                       } else if (this.options.ticks.suggestedMax !== undefined) {
+                               this.max = Math.max(this.max, this.options.ticks.suggestedMax);
+                       }
+
+                       if (this.min === this.max) {
+                               this.min--;
+                               this.max++;
+                       }
                },
                buildTicks: function() {
                        
                        var niceMin = Math.floor(this.min / spacing) * spacing;
                        var niceMax = Math.ceil(this.max / spacing) * spacing;
 
+                       var numSpaces = Math.ceil((niceMax - niceMin) / spacing);
+
                        // Put the values into the ticks array
-                       for (var j = niceMin; j <= niceMax; j += spacing) {
-                               this.ticks.push(j);
+                       this.ticks.push(this.options.ticks.min !== undefined ? this.options.ticks.min : niceMin);
+                       for (var j = 1; j < numSpaces; ++j) {
+                               this.ticks.push(niceMin + (j * spacing));
                        }
+                       this.ticks.push(this.options.ticks.max !== undefined ? this.options.ticks.max : niceMax);
 
                        // At this point, we need to update our max and min given the tick values since we have expanded the
                        // range of the scale
index f0c48aa27e9d28396875c8e564545c2fbbf91a25..b3eeebd8775b95ae87478f15278cb7dc9b93b430 100644 (file)
@@ -185,6 +185,73 @@ describe('Test the radial linear scale', function() {
                expect(scale.max).toBe(1);
        });
 
+       it('Should use the suggestedMin and suggestedMax options', function() {
+               var scaleID = 'myScale';
+
+               var mockData = {
+                       datasets: [{
+                               yAxisID: scaleID,
+                               data: [1, 1, 1, 2, 1, 0]
+                       }],
+                       labels: ['lablel1', 'label2', 'label3', 'label4', 'label5', 'label6']
+               };
+
+               var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('radialLinear'));
+               config.ticks.suggestedMin = -10;
+               config.ticks.suggestedMax = 10;
+
+               var mockContext = window.createMockContext();
+               var Constructor = Chart.scaleService.getScaleConstructor('radialLinear');
+               var scale = new Constructor({
+                       ctx: mockContext,
+                       options: config,
+                       chart: {
+                               data: mockData
+                       },
+                       id: scaleID
+               });
+
+               // Set arbitrary width and height for now
+               scale.update(200, 300);
+               expect(scale.min).toBe(-10);
+               expect(scale.max).toBe(10);
+       });
+
+       it('Should use the min and max options', function() {
+               var scaleID = 'myScale';
+
+               var mockData = {
+                       datasets: [{
+                               yAxisID: scaleID,
+                               data: [1, 1, 1, 2, 1, 0]
+                       }],
+                       labels: ['lablel1', 'label2', 'label3', 'label4', 'label5', 'label6']
+               };
+
+               var config = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('radialLinear'));
+               config.ticks.min = -1010;
+               config.ticks.max = 1010;
+
+               var mockContext = window.createMockContext();
+               var Constructor = Chart.scaleService.getScaleConstructor('radialLinear');
+               var scale = new Constructor({
+                       ctx: mockContext,
+                       options: config,
+                       chart: {
+                               data: mockData
+                       },
+                       id: scaleID
+               });
+
+               // Set arbitrary width and height for now
+               scale.update(200, 300);
+               expect(scale.min).toBe(-1010);
+               expect(scale.max).toBe(1010);
+               expect(scale.ticks[0]).toBe('-1010');
+               expect(scale.ticks[scale.ticks.length - 1]).toBe('1010');
+               expect(scale.ticks).toEqual(['-1010', '-1000', '0', '1000', '1010']);
+       });
+
        it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
                var scaleID = 'myScale';