2016-02-12 04:30:53 +01:00
|
|
|
"use strict";
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-12 04:30:53 +01:00
|
|
|
module.exports = function(Chart) {
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
var helpers = Chart.helpers;
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// Base class for all dataset controllers (line, bar, etc)
|
|
|
|
Chart.DatasetController = function(chart, datasetIndex) {
|
|
|
|
this.initialize.call(this, chart, datasetIndex);
|
|
|
|
};
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
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;
|
|
|
|
},
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
linkScales: function() {
|
|
|
|
if (!this.getDataset().xAxisID) {
|
|
|
|
this.getDataset().xAxisID = this.chart.options.scales.xAxes[0].id;
|
|
|
|
}
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
if (!this.getDataset().yAxisID) {
|
|
|
|
this.getDataset().yAxisID = this.chart.options.scales.yAxes[0].id;
|
|
|
|
}
|
|
|
|
},
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
getDataset: function() {
|
|
|
|
return this.chart.data.datasets[this.index];
|
|
|
|
},
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
getScaleForId: function(scaleID) {
|
|
|
|
return this.chart.scales[scaleID];
|
|
|
|
},
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
reset: function() {
|
|
|
|
this.update(true);
|
|
|
|
},
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
buildOrUpdateElements: function buildOrUpdateElements() {
|
|
|
|
// Handle the number of data points changing
|
|
|
|
var numData = this.getDataset().data.length;
|
|
|
|
var numMetaData = this.getDataset().metaData.length;
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// Controllers should implement the following
|
|
|
|
addElements: helpers.noop,
|
|
|
|
addElementAndReset: helpers.noop,
|
|
|
|
draw: helpers.noop,
|
|
|
|
removeHoverStyle: helpers.noop,
|
|
|
|
setHoverStyle: helpers.noop,
|
|
|
|
update: helpers.noop
|
|
|
|
});
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
Chart.DatasetController.extend = helpers.inherits;
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
};
|