},
};
- 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];
};
- 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) {
});
- 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;
};
- 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;
}
};
- 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);
},
};
- 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);
--- /dev/null
+(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);
// 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;
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: [{
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: {
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: {