"use strict"; module.exports = function(Chart) { var helpers = Chart.helpers; var noop = helpers.noop; // 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() { var meta = this.getMeta(); var dataset = this.getDataset(); 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; } }, getDataset: function() { return this.chart.data.datasets[this.index]; }, getMeta: function() { return this.chart.getDatasetMeta(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 meta = this.getMeta(), md = meta.data, numData = this.getDataset().data.length, numMetaData = md.length; // Make sure that we handle number of datapoints changing if (numData < numMetaData) { // Remove excess bars for data points that have been removed md.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: noop, addElementAndReset: noop, draw: noop, 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, model = element._model; model.backgroundColor = custom && custom.hoverBackgroundColor ? custom.hoverBackgroundColor : valueOrDefault(dataset.hoverBackgroundColor, index, color(model.backgroundColor).saturate(0.5).darken(0.1).rgbString()); model.borderColor = custom && custom.hoverBorderColor ? custom.hoverBorderColor : valueOrDefault(dataset.hoverBorderColor, index, color(model.borderColor).saturate(0.5).darken(0.1).rgbString()); model.borderWidth = custom && custom.hoverBorderWidth ? custom.hoverBorderWidth : valueOrDefault(dataset.hoverBorderWidth, index, model.borderWidth); }, update: noop }); Chart.DatasetController.extend = helpers.inherits; };