From: Simon Brunel Date: Thu, 21 Apr 2016 21:43:47 +0000 (+0200) Subject: Handle dataset type per chart X-Git-Tag: 2.1.0~13^2~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f3457c99417e3e87f67b8b673c6d9ca046b3bc46;p=thirdparty%2FChart.js.git Handle dataset type per chart Dataset effective type is now stored under meta.type, allowing many charts to share the same dataset but with different types. Also move dataset.bar flag to meta.bar. --- diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index a19a26ad0..eb3dd4dc7 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -33,16 +33,17 @@ module.exports = function(Chart) { Chart.DatasetController.prototype.initialize.call(this, chart, datasetIndex); // Use this to indicate that this is a bar dataset. - this.getDataset().bar = true; + this.getMeta().bar = true; }, // Get the number of datasets that display bars. We use this to correctly calculate the bar width getBarCount: function getBarCount() { var barCount = 0; - helpers.each(this.chart.data.datasets, function(dataset) { - if (helpers.isDatasetVisible(dataset) && dataset.bar) { + helpers.each(this.chart.data.datasets, function(dataset, datasetIndex) { + var meta = this.chart.getDatasetMeta(datasetIndex); + if (meta.bar && helpers.isDatasetVisible(dataset)) { ++barCount; } - }); + }, this); return barCount; }, @@ -140,7 +141,7 @@ module.exports = function(Chart) { for (var i = 0; i < datasetIndex; i++) { var negDS = this.chart.data.datasets[i]; var negDSMeta = this.chart.getDatasetMeta(i); - if (helpers.isDatasetVisible(negDS) && negDSMeta.yAxisID === yScale.id && negDS.bar) { + if (negDSMeta.bar && negDSMeta.yAxisID === yScale.id && helpers.isDatasetVisible(negDS)) { base += negDS.data[index] < 0 ? negDS.data[index] : 0; } } @@ -148,7 +149,7 @@ module.exports = function(Chart) { for (var j = 0; j < datasetIndex; j++) { var posDS = this.chart.data.datasets[j]; var posDSMeta = this.chart.getDatasetMeta(j); - if (helpers.isDatasetVisible(posDS) && posDSMeta.yAxisID === yScale.id && posDS.bar) { + if (posDSMeta.bar && posDSMeta.yAxisID === yScale.id && helpers.isDatasetVisible(posDS)) { base += posDS.data[index] > 0 ? posDS.data[index] : 0; } } @@ -216,9 +217,11 @@ module.exports = function(Chart) { // Get bar index from the given dataset index accounting for the fact that not all bars are visible getBarIndex: function(datasetIndex) { var barIndex = 0; + var meta, j; - for (var j = 0; j < datasetIndex; ++j) { - if (helpers.isDatasetVisible(this.chart.data.datasets[j]) && this.chart.data.datasets[j].bar) { + for (j = 0; j < datasetIndex; ++j) { + meta = this.chart.getDatasetMeta(j); + if (meta.bar && helpers.isDatasetVisible(this.chart.data.datasets[j])) { ++barIndex; } } @@ -263,7 +266,7 @@ module.exports = function(Chart) { for (var i = 0; i < datasetIndex; i++) { var ds = this.chart.data.datasets[i]; var dsMeta = this.chart.getDatasetMeta(i); - if (helpers.isDatasetVisible(ds) && ds.bar && dsMeta.yAxisID === yScale.id) { + if (dsMeta.bar && dsMeta.yAxisID === yScale.id && helpers.isDatasetVisible(ds)) { if (ds.data[index] < 0) { sumNeg += ds.data[index] || 0; } else { diff --git a/src/controllers/controller.line.js b/src/controllers/controller.line.js index b4e9553ee..2f8ed7951 100644 --- a/src/controllers/controller.line.js +++ b/src/controllers/controller.line.js @@ -222,7 +222,8 @@ module.exports = function(Chart) { for (var i = 0; i < datasetIndex; i++) { var ds = this.chart.data.datasets[i]; - if (ds.type === 'line' && helpers.isDatasetVisible(ds)) { + var dsMeta = this.chart.getDatasetMeta(i); + if (dsMeta.type === 'line' && helpers.isDatasetVisible(ds)) { if (ds.data[index] < 0) { sumNeg += ds.data[index] || 0; } else { diff --git a/src/core/core.controller.js b/src/core/core.controller.js index a7bb5d16e..3a33a0493 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -208,18 +208,17 @@ module.exports = function(Chart) { var newControllers = []; helpers.each(this.data.datasets, function(dataset, datasetIndex) { - if (!dataset.type) { - dataset.type = this.config.type; + var meta = this.getDatasetMeta(datasetIndex); + if (!meta.type) { + meta.type = dataset.type || this.config.type; } - var meta = this.getDatasetMeta(datasetIndex); - var type = dataset.type; - types.push(type); + types.push(meta.type); if (meta.controller) { meta.controller.updateIndex(datasetIndex); } else { - meta.controller = new Chart.controllers[type](this, datasetIndex); + meta.controller = new Chart.controllers[meta.type](this, datasetIndex); newControllers.push(meta.controller); } }, this); @@ -347,7 +346,7 @@ module.exports = function(Chart) { var elementsArray = []; helpers.each(this.data.datasets, function(dataset, datasetIndex) { - var meta = this.getDatasetMeta(datasetIndex); + var meta = this.getDatasetMeta(datasetIndex); if (helpers.isDatasetVisible(dataset)) { helpers.each(meta.data, function(element, index) { if (element.inRange(eventPosition.x, eventPosition.y)) { @@ -376,7 +375,7 @@ module.exports = function(Chart) { } } } - }; + } } }).call(this); @@ -385,7 +384,7 @@ module.exports = function(Chart) { } helpers.each(this.data.datasets, function(dataset, datasetIndex) { - var meta = this.getDatasetMeta(datasetIndex); + var meta = this.getDatasetMeta(datasetIndex); if (helpers.isDatasetVisible(dataset)) { elementsArray.push(meta.data[found._index]); } @@ -413,12 +412,13 @@ module.exports = function(Chart) { var meta = dataset._meta[this.id]; if (!meta) { meta = dataset._meta[this.id] = { - data: [], - dataset: null, - controller: null, - xAxisID: null, - yAxisID: null - }; + type: null, + data: [], + dataset: null, + controller: null, + xAxisID: null, + yAxisID: null + }; } return meta; diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index dafca9085..80d9f336a 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -48,16 +48,16 @@ module.exports = function(Chart) { helpers.each(this.chart.data.datasets, function(dataset, datasetIndex) { var meta = this.chart.getDatasetMeta(datasetIndex); - if (valuesPerType[dataset.type] === undefined) { - valuesPerType[dataset.type] = { + if (valuesPerType[meta.type] === undefined) { + valuesPerType[meta.type] = { positiveValues: [], negativeValues: [] }; } // Store these per type - var positiveValues = valuesPerType[dataset.type].positiveValues; - var negativeValues = valuesPerType[dataset.type].negativeValues; + var positiveValues = valuesPerType[meta.type].positiveValues; + var negativeValues = valuesPerType[meta.type].negativeValues; if (helpers.isDatasetVisible(dataset) && (this.isHorizontal() ? meta.xAxisID === this.id : meta.yAxisID === this.id)) { helpers.each(dataset.data, function(rawValue, index) { diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 36d81f6a1..a99df1e7e 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -33,12 +33,12 @@ module.exports = function(Chart) { helpers.each(this.chart.data.datasets, function(dataset, datasetIndex) { var meta = this.chart.getDatasetMeta(datasetIndex); if (helpers.isDatasetVisible(dataset) && (this.isHorizontal() ? meta.xAxisID === this.id : meta.yAxisID === this.id)) { - if (valuesPerType[dataset.type] === undefined) { - valuesPerType[dataset.type] = []; + if (valuesPerType[meta.type] === undefined) { + valuesPerType[meta.type] = []; } helpers.each(dataset.data, function(rawValue, index) { - var values = valuesPerType[dataset.type]; + var values = valuesPerType[meta.type]; var value = +this.getRightValue(rawValue); if (isNaN(value)) { return;