]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
More prework for zoom/pan
authorEvert Timberg <evert.timberg+github@gmail.com>
Sun, 17 Apr 2016 14:33:38 +0000 (10:33 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sun, 17 Apr 2016 14:33:38 +0000 (10:33 -0400)
src/scales/scale.category.js
src/scales/scale.linear.js
test/scale.category.tests.js

index a935361290a6c281bc3dbd965293fa9679fe52a6..5b096fef18ceb071956db006fa99828c22b5ca84 100644 (file)
@@ -9,25 +9,31 @@ module.exports = function(Chart) {
        };
 
        var DatasetScale = Chart.Scale.extend({
-               buildTicks: function(index) {
-                       this.startIndex = 0;
-                       this.endIndex = this.chart.data.labels.length;
+               // Implement this so that 
+               determineDataLimits: function() {
+                       this.minIndex = 0;
+                       this.maxIndex = this.chart.data.labels.length;
                        var findIndex;
 
                        if (this.options.ticks.min !== undefined) {
                                // user specified min value
                                findIndex = helpers.indexOf(this.chart.data.labels, this.options.ticks.min);
-                               this.startIndex = findIndex !== -1 ? findIndex : this.startIndex;
+                               this.minIndex = findIndex !== -1 ? findIndex : this.minIndex;
                        }
 
                        if (this.options.ticks.max !== undefined) {
                                // user specified max value
                                findIndex = helpers.indexOf(this.chart.data.labels, this.options.ticks.max);
-                               this.endIndex = findIndex !== -1 ? findIndex : this.endIndex;
+                               this.maxIndex = findIndex !== -1 ? findIndex : this.maxIndex;
                        }
 
+                       this.min = this.chart.data.labels[this.minIndex];
+                       this.max = this.chart.data.labels[this.maxIndex];
+               },
+
+               buildTicks: function(index) {
                        // If we are viewing some subset of labels, slice the original array
-                       this.ticks = (this.startIndex === 0 && this.endIndex === this.chart.data.labels.length) ? this.chart.data.labels : this.chart.data.labels.slice(this.startIndex, this.endIndex + 1);
+                       this.ticks = (this.minIndex === 0 && this.maxIndex === this.chart.data.labels.length) ? this.chart.data.labels : this.chart.data.labels.slice(this.minIndex, this.maxIndex + 1);
                },
 
                getLabelForIndex: function(index, datasetIndex) {
@@ -42,7 +48,7 @@ module.exports = function(Chart) {
                        if (this.isHorizontal()) {
                                var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
                                var valueWidth = innerWidth / offsetAmt;
-                               var widthOffset = (valueWidth * (index - this.startIndex)) + this.paddingLeft;
+                               var widthOffset = (valueWidth * (index - this.minIndex)) + this.paddingLeft;
 
                                if (this.options.gridLines.offsetGridLines && includeOffset) {
                                        widthOffset += (valueWidth / 2);
@@ -52,7 +58,7 @@ module.exports = function(Chart) {
                        } else {
                                var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
                                var valueHeight = innerHeight / offsetAmt;
-                               var heightOffset = (valueHeight * (index - this.startIndex)) + this.paddingTop;
+                               var heightOffset = (valueHeight * (index - this.minIndex)) + this.paddingTop;
 
                                if (this.options.gridLines.offsetGridLines && includeOffset) {
                                        heightOffset += (valueHeight / 2);
@@ -62,7 +68,7 @@ module.exports = function(Chart) {
                        }
                },
                getPixelForTick: function(index, includeOffset) {
-                       return this.getPixelForValue(this.ticks[index], index + this.startIndex, null, includeOffset);
+                       return this.getPixelForValue(this.ticks[index], index + this.minIndex, null, includeOffset);
                }
        });
 
index 9c6f1aeba97cd873efa2343171e4f08855a1a0b0..3cf2b020d939195870aa718a75040ab75b6df68c 100644 (file)
@@ -8,7 +8,8 @@ module.exports = function(Chart) {
                position: "left",
                ticks: {
                        callback: function(tickValue, index, ticks) {
-                               var delta = ticks[1] - ticks[0];
+                               // If we have lots of ticks, don't use the ones
+                               var delta = ticks.length > 3 ? ticks[2] - ticks[1] : ticks[1] - ticks[0];
 
                                // If we have a number like 2.5 as the delta, figure out how many decimal places we need
                                if (Math.abs(delta) > 1) {
index 47645fe2391842feacf857d18894036d9a09b1c5..8346ff0d1baa0da4839088baa77782cb09f5bc6a 100644 (file)
@@ -66,6 +66,7 @@ describe('Category scale tests', function() {
                        id: scaleID
                });
 
+               scale.determineDataLimits();
                scale.buildTicks();
                expect(scale.ticks).toEqual(mockData.labels);
        });
@@ -92,6 +93,7 @@ describe('Category scale tests', function() {
                        id: scaleID
                });
 
+               scale.determineDataLimits();
                scale.buildTicks();
 
                expect(scale.getLabelForIndex(1)).toBe('tick2');