]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
All dataset controllers now inherit from a common base class Chart.DatasetController...
authorEvert Timberg <evert.timberg@gmail.com>
Sun, 13 Dec 2015 19:35:40 +0000 (14:35 -0500)
committerEvert Timberg <evert.timberg@gmail.com>
Sun, 13 Dec 2015 19:35:40 +0000 (14:35 -0500)
src/controllers/controller.bar.js
src/controllers/controller.bubble.js
src/controllers/controller.doughnut.js
src/controllers/controller.line.js
src/controllers/controller.polarArea.js
src/controllers/controller.radar.js
src/core/core.datasetController.js [new file with mode: 0644]
src/core/core.tooltip.js
test/controller.bar.tests.js
test/controller.doughnut.tests.js
test/controller.line.tests.js

index a97e284e00e2429f75b7495ebb45ad34d1cce629..c1c667d334e9a9a46284c6a89d2bde4913e55071 100644 (file)
                },
        };
 
-       Chart.controllers.bar = function(chart, datasetIndex) {
-               this.initialize.call(this, chart, datasetIndex);
-       };
-
-       helpers.extend(Chart.controllers.bar.prototype, {
-
-               initialize: function(chart, datasetIndex) {
-                       this.chart = chart;
-                       this.index = datasetIndex;
-                       this.linkScales();
-                       this.addElements();
-               },
-               updateIndex: function(datasetIndex) {
-                       this.index = datasetIndex;
-               },
-               linkScales: function() {
-                       if (!this.getDataset().xAxisID) {
-                               this.getDataset().xAxisID = this.chart.options.scales.xAxes[0].id;
-                       }
-
-                       if (!this.getDataset().yAxisID) {
-                               this.getDataset().yAxisID = this.chart.options.scales.yAxes[0].id;
-                       }
-               },
-
-               getDataset: function() {
-                       return this.chart.data.datasets[this.index];
-               },
-
-               getScaleForID: function(scaleID) {
-                       return this.chart.scales[scaleID];
-               },
-
+       Chart.controllers.bar = Chart.DatasetController.extend({
                // Get the number of datasets that display bars. We use this to correctly calculate the bar width
                getBarCount: function getBarCount() {
                        var barCount = 0;
                        this.getDataset().metaData.splice(index, 0, rectangle);
                },
 
-               removeElement: function(index) {
-                       this.getDataset().metaData.splice(index, 1);
-               },
-
-               reset: function() {
-                       this.update(true);
-               },
-
-               buildOrUpdateElements: function buildOrUpdateElements() {
-                       var numData = this.getDataset().data.length;
-                       var numRectangles = this.getDataset().metaData.length;
-
-                       // Make sure that we handle number of datapoints changing
-                       if (numData < numRectangles) {
-                               // Remove excess bars for data points that have been removed
-                               this.getDataset().metaData.splice(numData, numRectangles - numData);
-                       } else if (numData > numRectangles) {
-                               // Add new elements
-                               for (var index = numRectangles; index < numData; ++index) {
-                                       this.addElementAndReset(index);
-                               }
-                       }
-               },
-
                update: function update(reset) {
                        var numBars = this.getBarCount();
 
 
                updateElement: function updateElement(rectangle, index, reset, numBars) {
 
-                       var xScale = this.getScaleForID(this.getDataset().xAxisID);
-                       var yScale = this.getScaleForID(this.getDataset().yAxisID);
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
+                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
 
                        var yScalePoint;
 
 
                calculateBarBase: function(datasetIndex, index) {
 
-                       var xScale = this.getScaleForID(this.getDataset().xAxisID);
-                       var yScale = this.getScaleForID(this.getDataset().yAxisID);
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
+                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
 
                        var base = 0;
 
 
                getRuler: function() {
 
-                       var xScale = this.getScaleForID(this.getDataset().xAxisID);
-                       var yScale = this.getScaleForID(this.getDataset().yAxisID);
-                       /*var datasetCount = !this.chart.isCombo ? this.chart.data.datasets.length : helpers.where(this.chart.data.datasets, function(ds) {
-                               return ds.type == 'bar';
-                       }).length;*/
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
+                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
                        var datasetCount = this.getBarCount();
 
                        var tickWidth = (function() {
 
                calculateBarWidth: function() {
 
-                       var xScale = this.getScaleForID(this.getDataset().xAxisID);
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
                        var ruler = this.getRuler();
 
                        if (xScale.options.stacked) {
 
                calculateBarX: function(index, datasetIndex) {
 
-                       var yScale = this.getScaleForID(this.getDataset().yAxisID);
-                       var xScale = this.getScaleForID(this.getDataset().xAxisID);
+                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
                        var barIndex = this.getBarIndex(datasetIndex);
 
                        var ruler = this.getRuler();
 
                calculateBarY: function(index, datasetIndex) {
 
-                       var xScale = this.getScaleForID(this.getDataset().xAxisID);
-                       var yScale = this.getScaleForID(this.getDataset().yAxisID);
+                       var xScale = this.getScaleForId(this.getDataset().xAxisID);
+                       var yScale = this.getScaleForId(this.getDataset().yAxisID);
 
                        var value = this.getDataset().data[index];
 
index d0d653870e66aa72760b21c1c14c000b5e1db99c..bce235c2ae3ca4a7c16cd893bf516c91531a9696 100644 (file)
        };
 
 
-       Chart.controllers.bubble = function(chart, datasetIndex) {
-               this.initialize.call(this, chart, datasetIndex);
-       };
-
-       helpers.extend(Chart.controllers.bubble.prototype, {
-
-               initialize: function(chart, datasetIndex) {
-                       this.chart = chart;
-                       this.index = datasetIndex;
-                       this.linkScales();
-                       this.addElements();
-               },
-               updateIndex: function(datasetIndex) {
-                       this.index = datasetIndex;
-               },
-
-               linkScales: function() {
-                       if (!this.getDataset().xAxisID) {
-                               this.getDataset().xAxisID = this.chart.options.scales.xAxes[0].id;
-                       }
-
-                       if (!this.getDataset().yAxisID) {
-                               this.getDataset().yAxisID = this.chart.options.scales.yAxes[0].id;
-                       }
-               },
-
-               getDataset: function() {
-                       return this.chart.data.datasets[this.index];
-               },
-
-               getScaleForId: function(scaleID) {
-                       return this.chart.scales[scaleID];
-               },
-
+       Chart.controllers.bubble = Chart.DatasetController.extend({
                addElements: function() {
 
                        this.getDataset().metaData = this.getDataset().metaData || [];
 
                        // Add to the points array
                        this.getDataset().metaData.splice(index, 0, point);
-
-               },
-               removeElement: function(index) {
-                       this.getDataset().metaData.splice(index, 1);
-               },
-
-               reset: function() {
-                       this.update(true);
-               },
-
-               buildOrUpdateElements: function buildOrUpdateElements() {
-                       // Handle the number of data points changing
-                       var numData = this.getDataset().data.length;
-                       var numPoints = this.getDataset().metaData.length;
-
-                       // Make sure that we handle number of datapoints changing
-                       if (numData < numPoints) {
-                               // Remove excess bars for data points that have been removed
-                               this.getDataset().metaData.splice(numData, numPoints - numData);
-                       } else if (numData > numPoints) {
-                               // Add new elements
-                               for (var index = numPoints; index < numData; ++index) {
-                                       this.addElementAndReset(index);
-                               }
-                       }
                },
 
                update: function update(reset) {
index 2c37b9b90db0e44e3b6c784ca92b30349ee4f987..a38c904d79a21371c1a6766b111f620e412cc0ab 100644 (file)
        });
 
 
-       Chart.controllers.doughnut = Chart.controllers.pie = function(chart, datasetIndex) {
-               this.initialize.call(this, chart, datasetIndex);
-       };
-
-       helpers.extend(Chart.controllers.doughnut.prototype, {
-
-               initialize: function(chart, datasetIndex) {
-                       this.chart = chart;
-                       this.index = datasetIndex;
-                       this.linkScales();
-                       this.addElements();
-               },
-               updateIndex: function(datasetIndex) {
-                       this.index = datasetIndex;
-               },
+       Chart.controllers.doughnut = Chart.controllers.pie = Chart.DatasetController.extend({
                linkScales: function() {
                        // no scales for doughnut
                },
 
-               getDataset: function() {
-                       return this.chart.data.datasets[this.index];
-               },
-
                addElements: function() {
                        this.getDataset().metaData = this.getDataset().metaData || [];
                        helpers.each(this.getDataset().data, function(value, index) {
                        // Add to the points array
                        this.getDataset().metaData.splice(index, 0, arc);
                },
-               removeElement: function(index) {
-                       this.getDataset().metaData.splice(index, 1);
-               },
-               reset: function() {
-                       this.update(true);
-               },
-
-               buildOrUpdateElements: function buildOrUpdateElements() {
-                       // Make sure we have metaData for each data point
-                       var numData = this.getDataset().data.length;
-                       var numArcs = this.getDataset().metaData.length;
-
-                       // Make sure that we handle number of datapoints changing
-                       if (numData < numArcs) {
-                               // Remove excess bars for data points that have been removed
-                               this.getDataset().metaData.splice(numData, numArcs - numData);
-                       } else if (numData > numArcs) {
-                               // Add new elements
-                               for (var index = numArcs; index < numData; ++index) {
-                                       this.addElementAndReset(index);
-                               }
-                       }
-               },
 
                getVisibleDatasetCount: function getVisibleDatasetCount() {
                        return helpers.where(this.chart.data.datasets, function(ds) { return helpers.isDatasetVisible(ds); }).length;
index 8d0ad0eb18c6bd7e3cc2a798dbf29160402b8f9a..4776f7f73b08147b0fc39ecefe7ebc96dc9fd1ef 100644 (file)
        };
 
 
-       Chart.controllers.line = function(chart, datasetIndex) {
-               this.initialize.call(this, chart, datasetIndex);
-       };
-
-       helpers.extend(Chart.controllers.line.prototype, {
-
-               initialize: function(chart, datasetIndex) {
-                       this.chart = chart;
-                       this.index = datasetIndex;
-                       this.linkScales();
-                       this.addElements();
-               },
-               updateIndex: function(datasetIndex) {
-                       this.index = datasetIndex;
-               },
-
-               linkScales: function() {
-                       if (!this.getDataset().xAxisID) {
-                               this.getDataset().xAxisID = this.chart.options.scales.xAxes[0].id;
-                       }
-
-                       if (!this.getDataset().yAxisID) {
-                               this.getDataset().yAxisID = this.chart.options.scales.yAxes[0].id;
-                       }
-               },
-
-               getDataset: function() {
-                       return this.chart.data.datasets[this.index];
-               },
-
-               getScaleForId: function(scaleID) {
-                       return this.chart.scales[scaleID];
-               },
-
+       Chart.controllers.line = Chart.DatasetController.extend({
                addElements: function() {
 
                        this.getDataset().metaData = this.getDataset().metaData || [];
                        // Make sure bezier control points are updated
                        this.updateBezierControlPoints();
                },
-               removeElement: function(index) {
-                       this.getDataset().metaData.splice(index, 1);
-               },
-
-               reset: function() {
-                       this.update(true);
-               },
-
-               buildOrUpdateElements: function buildOrUpdateElements() {
-                       // Handle the number of data points changing
-                       var numData = this.getDataset().data.length;
-                       var numPoints = this.getDataset().metaData.length;
-
-                       // Make sure that we handle number of datapoints changing
-                       if (numData < numPoints) {
-                               // Remove excess bars for data points that have been removed
-                               this.getDataset().metaData.splice(numData, numPoints - numData);
-                       } else if (numData > numPoints) {
-                               // Add new elements
-                               for (var index = numPoints; index < numData; ++index) {
-                                       this.addElementAndReset(index);
-                               }
-                       }
-               },
 
                update: function update(reset) {
                        var line = this.getDataset().metaDataset;
index 8b5ceef41fa82b8442d0c536df5eef6c22eb0474..7dbd3ea287485e5dd52e4bd7e8cc8204318dd137 100644 (file)
                }
        };
 
-       Chart.controllers.polarArea = function(chart, datasetIndex) {
-               this.initialize.call(this, chart, datasetIndex);
-       };
-
-       helpers.extend(Chart.controllers.polarArea.prototype, {
-
-               initialize: function(chart, datasetIndex) {
-                       this.chart = chart;
-                       this.index = datasetIndex;
-                       this.linkScales();
-                       this.addElements();
-               },
-               updateIndex: function(datasetIndex) {
-                       this.index = datasetIndex;
-               },
-
+       Chart.controllers.polarArea = Chart.DatasetController.extend({
                linkScales: function() {
                        // no scales for doughnut
                },
-
-               getDataset: function() {
-                       return this.chart.data.datasets[this.index];
-               },
-
-               getScaleForId: function(scaleID) {
-                       return this.chart.scales[scaleID];
-               },
-
                addElements: function() {
                        this.getDataset().metaData = this.getDataset().metaData || [];
                        helpers.each(this.getDataset().data, function(value, index) {
                        // Add to the points array
                        this.getDataset().metaData.splice(index, 0, arc);
                },
-               removeElement: function(index) {
-                       this.getDataset().metaData.splice(index, 1);
-               },
-
-               reset: function() {
-                       this.update(true);
-               },
-
-               buildOrUpdateElements: function buildOrUpdateElements() {
-                       // Handle the number of data points changing
-                       var numData = this.getDataset().data.length;
-                       var numPoints = this.getDataset().metaData.length;
-
-                       // Make sure that we handle number of datapoints changing
-                       if (numData < numPoints) {
-                               // Remove excess bars for data points that have been removed
-                               this.getDataset().metaData.splice(numData, numPoints - numData);
-                       } else if (numData > numPoints) {
-                               // Add new elements
-                               for (var index = numPoints; index < numData; ++index) {
-                                       this.addElementAndReset(index);
-                               }
-                       }
-               },
-
                getVisibleDatasetCount: function getVisibleDatasetCount() {
                        return helpers.where(this.chart.data.datasets, function(ds) { return helpers.isDatasetVisible(ds); }).length;
                },
 
                                return (2 * Math.PI) / (this.getDataset().data.length - numNaN);
                        }
-               },
-               updateScaleRange: function() {
-                       helpers.extend(this.chart.scale, {
-                               size: helpers.min([this.chart.width, this.chart.height]),
-                               xCenter: this.chart.width / 2,
-                               yCenter: this.chart.height / 2
-                       });
-               },
-
+               }
        });
 }).call(this);
index 9bec21e2b12d6425638531f5b17ad7171d64178f..6d808c67a7fd941f57888c3c030b92858bc3af43 100644 (file)
                },
        };
 
-       Chart.controllers.radar = function(chart, datasetIndex) {
-               this.initialize.call(this, chart, datasetIndex);
-       };
-
-
-       helpers.extend(Chart.controllers.radar.prototype, {
-
-               initialize: function(chart, datasetIndex) {
-                       this.chart = chart;
-                       this.index = datasetIndex;
-                       this.linkScales();
-                       this.addElements();
-               },
-               updateIndex: function(datasetIndex) {
-                       this.index = datasetIndex;
-               },
-
+       Chart.controllers.radar = Chart.DatasetController.extend({
                linkScales: function() {
                        // No need. Single scale only
                },
 
-               getDataset: function() {
-                       return this.chart.data.datasets[this.index];
-               },
-
-               getScaleForId: function(scaleID) {
-                       return this.chart.scales[scaleID];
-               },
-
                addElements: function() {
 
                        this.getDataset().metaData = this.getDataset().metaData || [];
                        // Make sure bezier control points are updated
                        this.updateBezierControlPoints();
                },
-               removeElement: function(index) {
-                       this.getDataset().metaData.splice(index, 1);
-               },
-
-               reset: function() {
-                       this.update(true);
-               },
-
-               buildOrUpdateElements: function buildOrUpdateElements() {
-                       // Handle the number of data points changing
-                       var numData = this.getDataset().data.length;
-                       var numPoints = this.getDataset().metaData.length;
-
-                       // Make sure that we handle number of datapoints changing
-                       if (numData < numPoints) {
-                               // Remove excess bars for data points that have been removed
-                               this.getDataset().metaData.splice(numData, numPoints - numData);
-                       } else if (numData > numPoints) {
-                               // Add new elements
-                               for (var index = numPoints; index < numData; ++index) {
-                                       this.addElementAndReset(index);
-                               }
-                       }
-               },
 
                update: function update(reset) {
 
                        point._model.borderColor = point.custom && point.custom.borderColor ? point.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().pointBorderColor, index, this.chart.options.elements.point.borderColor);
                        point._model.borderWidth = point.custom && point.custom.borderWidth ? point.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().pointBorderWidth, index, this.chart.options.elements.point.borderWidth);
                }
-
        });
-
-
-
 }).call(this);
diff --git a/src/core/core.datasetController.js b/src/core/core.datasetController.js
new file mode 100644 (file)
index 0000000..2f1d366
--- /dev/null
@@ -0,0 +1,77 @@
+(function() {
+
+       "use strict";
+
+       //Declare root variable - window in the browser, global on the server
+       var root = this,
+               Chart = root.Chart,
+               helpers = Chart.helpers;
+
+
+       // Base class for all dataset controllers (line, bar, etc)
+       Chart.DatasetController = function(chart, datasetIndex) {
+               this.initialize.call(this, chart, datasetIndex);
+       };
+
+       helpers.extend(Chart.DatasetController.prototype, {
+               initialize: function(chart, datasetIndex) {
+                       this.chart = chart;
+                       this.index = datasetIndex;
+                       this.linkScales();
+                       this.addElements();
+               },
+               updateIndex: function(datasetIndex) {
+                       this.index = datasetIndex;
+               },
+
+               linkScales: function() {
+                       if (!this.getDataset().xAxisID) {
+                               this.getDataset().xAxisID = this.chart.options.scales.xAxes[0].id;
+                       }
+
+                       if (!this.getDataset().yAxisID) {
+                               this.getDataset().yAxisID = this.chart.options.scales.yAxes[0].id;
+                       }
+               },
+
+               getDataset: function() {
+                       return this.chart.data.datasets[this.index];
+               },
+
+               getScaleForId: function(scaleID) {
+                       return this.chart.scales[scaleID];
+               },
+
+               reset: function() {
+                       this.update(true);
+               },
+
+               buildOrUpdateElements: function buildOrUpdateElements() {
+                       // Handle the number of data points changing
+                       var numData = this.getDataset().data.length;
+                       var numMetaData = this.getDataset().metaData.length;
+
+                       // Make sure that we handle number of datapoints changing
+                       if (numData < numMetaData) {
+                               // Remove excess bars for data points that have been removed
+                               this.getDataset().metaData.splice(numData, numMetaData - numData);
+                       } else if (numData > numMetaData) {
+                               // Add new elements
+                               for (var index = numMetaData; index < numData; ++index) {
+                                       this.addElementAndReset(index);
+                               }
+                       }
+               },
+
+               // Controllers should implement the following
+               addElements: helpers.noop,
+               addElementAndReset: helpers.noop,
+               draw: helpers.noop,
+               removeHoverStyle: helpers.noop,
+               setHoverStyle: helpers.noop,
+               update: helpers.noop,
+       });
+
+       Chart.DatasetController.extend = helpers.inherits;
+
+}).call(this);
index cbc3111b059631fd6d0385b1563850fb63a45a14..bad1189f3130fabf31f49f545ebe9b75ec1e994f 100644 (file)
                // Args are: (tooltipItem, data)
                getBeforeBody: function() {
                        var lines = this._options.tooltips.callbacks.beforeBody.apply(this, arguments);
-                       return helpers.isArray(lines) ? lines : [lines];
+                       return helpers.isArray(lines) ? lines : lines !== undefined ? [lines] : [];
                },
 
                // Args are: (tooltipItem, data)
                // Args are: (tooltipItem, data)
                getAfterBody: function() {
                        var lines = this._options.tooltips.callbacks.afterBody.apply(this, arguments);
-                       return helpers.isArray(lines) ? lines : [lines];
+                       return helpers.isArray(lines) ? lines : lines !== undefined ? [lines] : [];
                },
 
                // Get the footer and beforeFooter and afterFooter lines
                        tooltipHeight += vm.title.length ? vm.titleMarginBottom : 0; // Title's bottom Margin
 
                        tooltipHeight += combinedBodyLength * vm.bodyFontSize; // Body Lines
-                       tooltipHeight += (combinedBodyLength - 1) * vm.bodySpacing; // Body Line Spacing
+                       tooltipHeight += combinedBodyLength ? (combinedBodyLength - 1) * vm.bodySpacing : 0; // Body Line Spacing
 
                        tooltipHeight += vm.footer.length ? vm.footerMarginTop : 0; // Footer Margin
                        tooltipHeight += vm.footer.length * (vm.footerFontSize); // Footer Lines
-                       tooltipHeight += (vm.footer.length - 1) * vm.footerSpacing; // Footer Line Spacing
+                       tooltipHeight += vm.footer.length ? (vm.footer.length - 1) * vm.footerSpacing : 0; // Footer Line Spacing
 
                        // Width
                        var tooltipWidth = 0;
index 14cd977a41e3f6e9c2db625c8329b8d6500c9f92..f01d9a301828b4f65b761158efde1710146353af 100644 (file)
@@ -142,33 +142,6 @@ describe('Bar controller tests', function() {
                expect(chart.data.datasets[1].metaData[3] instanceof Chart.elements.Rectangle).toBe(true);
        });
 
-       it('should remove elements', function() {
-               var chart = {
-                       data: {
-                               datasets: [{}, {
-                                       data: [10, 15, 0, -4]
-                               }]
-                       },
-                       config: {
-                               type: 'bar'
-                       },
-                       options: {
-                               scales: {
-                                       xAxes: [{
-                                               id: 'firstXScaleID'
-                                       }],
-                                       yAxes: [{
-                                               id: 'firstYScaleID'
-                                       }]
-                               }
-                       }
-               };
-
-               var controller = new Chart.controllers.bar(chart, 1);
-               controller.removeElement(1);
-               expect(chart.data.datasets[1].metaData.length).toBe(3);
-       });
-
        it('should update elements', function() {
                var data = {
                        datasets: [{
index 7328b48af28160953a7756c7d8de4e02d317ba91..865d7b8cd4e727f02b4a3d6122a702ee57987864 100644 (file)
@@ -41,25 +41,6 @@ describe('Doughnut controller tests', function() {
                expect(chart.data.datasets[0].metaData[3] instanceof Chart.elements.Arc).toBe(true);
        });
 
-       it ('Should remove elements', function() {
-               var chart = {
-                       data: {
-                               datasets: [{
-                                       data: [10, 15, 0, 4]
-                               }]
-                       },
-                       config: {
-                               type: 'doughnut'
-                       },
-                       options: {
-                       }
-               };
-
-               var controller = new Chart.controllers.doughnut(chart, 0);
-               controller.removeElement(1);
-               expect(chart.data.datasets[0].metaData.length).toBe(3);
-       });
-
        it ('Should reset and update elements', function() {
                var chart = {
                        chartArea: {
index f813bb55db358d51ebaf5b3d1149dc38a4b8f132..1b918dff97d7850af2224817c0e5a5df6471a275 100644 (file)
@@ -76,33 +76,6 @@ describe('Line controller tests', function() {
                expect(chart.data.datasets[0].metaDataset instanceof Chart.elements.Line).toBe(true); // 1 line element
        });
 
-       it('should remove elements', function() {
-               var chart = {
-                       data: {
-                               datasets: [{
-                                       data: [10, 15, 0, -4]
-                               }]
-                       },
-                       config: {
-                               type: 'line'
-                       },
-                       options: {
-                               scales: {
-                                       xAxes: [{
-                                               id: 'firstXScaleID'
-                                       }],
-                                       yAxes: [{
-                                               id: 'firstYScaleID'
-                                       }]
-                               }
-                       }
-               };
-
-               var controller = new Chart.controllers.line(chart, 0);
-               controller.removeElement(0);
-               expect(chart.data.datasets[0].metaData.length).toBe(3);
-       });
-
        it('should draw all elements', function() {
                var chart = {
                        data: {