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.
This commit is contained in:
Simon Brunel 2016-04-21 23:43:47 +02:00
parent 51aa9b4a27
commit f3457c9941
5 changed files with 36 additions and 32 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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;

View File

@ -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) {

View File

@ -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;