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;
|
2016-05-05 03:32:40 +02:00
|
|
|
var noop = helpers.noop;
|
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() {
|
2016-04-21 17:11:52 +02:00
|
|
|
var meta = this.getMeta();
|
|
|
|
var dataset = this.getDataset();
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-04-21 17:11:52 +02:00
|
|
|
if (meta.xAxisID === null) {
|
|
|
|
meta.xAxisID = dataset.xAxisID || this.chart.options.scales.xAxes[0].id;
|
|
|
|
}
|
|
|
|
if (meta.yAxisID === null) {
|
|
|
|
meta.yAxisID = dataset.yAxisID || this.chart.options.scales.yAxes[0].id;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
},
|
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-04-21 17:11:52 +02:00
|
|
|
getMeta: function() {
|
|
|
|
return this.chart.getDatasetMeta(this.index);
|
|
|
|
},
|
|
|
|
|
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
|
2016-05-05 03:32:40 +02:00
|
|
|
var meta = this.getMeta(),
|
|
|
|
md = meta.data,
|
|
|
|
numData = this.getDataset().data.length,
|
|
|
|
numMetaData = md.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
|
2016-05-05 03:32:40 +02:00
|
|
|
md.splice(numData, numMetaData - numData);
|
2016-02-14 23:06:00 +01:00
|
|
|
} 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
|
2016-05-05 03:32:40 +02:00
|
|
|
addElements: noop,
|
|
|
|
addElementAndReset: noop,
|
2016-05-08 03:04:39 +02:00
|
|
|
draw: function(ease) {
|
|
|
|
var easingDecimal = ease || 1;
|
|
|
|
helpers.each(this.getMeta().data, function(element, index) {
|
|
|
|
element.transition(easingDecimal).draw();
|
|
|
|
});
|
|
|
|
},
|
2016-05-06 02:47:33 +02:00
|
|
|
removeHoverStyle: function(element, elementOpts) {
|
|
|
|
var dataset = this.chart.data.datasets[element._datasetIndex],
|
|
|
|
index = element._index,
|
|
|
|
custom = element.custom,
|
|
|
|
valueOrDefault = helpers.getValueAtIndexOrDefault,
|
|
|
|
color = helpers.color,
|
|
|
|
model = element._model;
|
|
|
|
|
|
|
|
model.backgroundColor = custom && custom.backgroundColor ? custom.backgroundColor : valueOrDefault(dataset.backgroundColor, index, elementOpts.backgroundColor);
|
|
|
|
model.borderColor = custom && custom.borderColor ? custom.borderColor : valueOrDefault(dataset.borderColor, index, elementOpts.borderColor);
|
|
|
|
model.borderWidth = custom && custom.borderWidth ? custom.borderWidth : valueOrDefault(dataset.borderWidth, index, elementOpts.borderWidth);
|
|
|
|
},
|
|
|
|
setHoverStyle: function(element) {
|
|
|
|
var dataset = this.chart.data.datasets[element._datasetIndex],
|
|
|
|
index = element._index,
|
|
|
|
custom = element.custom,
|
|
|
|
valueOrDefault = helpers.getValueAtIndexOrDefault,
|
|
|
|
color = helpers.color,
|
2016-05-13 00:57:22 +02:00
|
|
|
getHoverColor = helpers.getHoverColor,
|
2016-05-06 02:47:33 +02:00
|
|
|
model = element._model;
|
|
|
|
|
2016-05-13 00:57:22 +02:00
|
|
|
model.backgroundColor = custom && custom.hoverBackgroundColor ? custom.hoverBackgroundColor : valueOrDefault(dataset.hoverBackgroundColor, index, getHoverColor(model.backgroundColor));
|
|
|
|
model.borderColor = custom && custom.hoverBorderColor ? custom.hoverBorderColor : valueOrDefault(dataset.hoverBorderColor, index, getHoverColor(model.borderColor));
|
2016-05-06 02:47:33 +02:00
|
|
|
model.borderWidth = custom && custom.hoverBorderWidth ? custom.hoverBorderWidth : valueOrDefault(dataset.hoverBorderWidth, index, model.borderWidth);
|
|
|
|
},
|
2016-05-05 03:32:40 +02:00
|
|
|
update: noop
|
2016-02-14 23:06:00 +01:00
|
|
|
});
|
2015-12-13 20:35:40 +01:00
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
Chart.DatasetController.extend = helpers.inherits;
|
|
|
|
};
|