From 7a4ba6b50cf24946220aec2a421fea427074510d Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Sat, 24 Feb 2018 16:39:13 +0800 Subject: [PATCH 1/8] fix the data missing bar bug. --- src/core/core.controller.js | 59 +++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index e29a5b0769c..b71ee6b4a63 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -691,7 +691,7 @@ module.exports = function(Chart) { if (!dataset._meta) { dataset._meta = {}; } - + dataset.data = me.formatDataset(dataset.data); var meta = dataset._meta[me.id]; if (!meta) { meta = dataset._meta[me.id] = { @@ -933,7 +933,62 @@ module.exports = function(Chart) { me.lastActive = me.active; return changed; - } + }, + /** + * format the data list if the data is missing some points in the datasets. + * @private + * @param {array} dataArray array to format + * @return {array} the formated array + */ + formatDataset: function(dataArray) { + var labels = this.chart.data.labels; + var tmp = dataArray.slice(0); + var labelLen = labels.length; + var dataLen = dataArray.length; + var result = dataArray; + var match = this.dataInLabel(tmp, labels) + if (match && dataLen < labelLen) { + for (var i = 0; i < labels.length; i++) { + var label = labels[i]; + result[i] = {x: label, y: this.getY(label, tmp)}; + } + } + return result; + }, + /** + * return the y data of the label, if no this label, return null + * @private + * @param {string} label the label in the labels array + * @param {array} dataArray the data in the datasets. + * @return {number} the y data according to the label + */ + getY: function(label, dataArray) { + var y = null; + for (var i = 0; i < dataArray.length; i++) { + var item = dataArray[i]; + if (item.x === label) { + y = item.y; + } + } + return y; + }, + /** + * find if the data in datasets is existed in the labels + * @private + * @param {array} dataArray the data in the datasets + * @param {array} labelArray the label in the labels array + * @return {boolean} the match result, true is included + */ + dataInLabel: function(dataArray, labelArray) { + var result = false + for (var i = 0; i < dataArray.length; i++) { + var item = dataArray[i] + if (labelArray.indexOf(item.x) > -1) { + result = true + } + } + return result + }, }); /** From d1809f79518ae19095ac40b5fa45ef7f7dd5e0bc Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Sat, 24 Feb 2018 16:44:50 +0800 Subject: [PATCH 2/8] adjust the gulp lint --- src/core/core.controller.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index b71ee6b4a63..42efbfa166c 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -946,7 +946,7 @@ module.exports = function(Chart) { var labelLen = labels.length; var dataLen = dataArray.length; var result = dataArray; - var match = this.dataInLabel(tmp, labels) + var match = this.dataInLabel(tmp, labels); if (match && dataLen < labelLen) { for (var i = 0; i < labels.length; i++) { var label = labels[i]; @@ -971,23 +971,23 @@ module.exports = function(Chart) { } } return y; - }, - /** + }, + /** * find if the data in datasets is existed in the labels * @private * @param {array} dataArray the data in the datasets * @param {array} labelArray the label in the labels array * @return {boolean} the match result, true is included */ - dataInLabel: function(dataArray, labelArray) { - var result = false + dataInLabel: function(dataArray, labelArray) { + var result = false; for (var i = 0; i < dataArray.length; i++) { - var item = dataArray[i] + var item = dataArray[i]; if (labelArray.indexOf(item.x) > -1) { - result = true + result = true; } } - return result + return result; }, }); From e3872852b624a23ff43721032821bcc6c022834d Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Sat, 24 Feb 2018 17:55:40 +0800 Subject: [PATCH 3/8] adjust the unit test --- src/core/core.controller.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 42efbfa166c..a306a387762 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -691,6 +691,9 @@ module.exports = function(Chart) { if (!dataset._meta) { dataset._meta = {}; } + if (!dataset.data) { + dataset.data = []; + } dataset.data = me.formatDataset(dataset.data); var meta = dataset._meta[me.id]; if (!meta) { @@ -983,6 +986,9 @@ module.exports = function(Chart) { var result = false; for (var i = 0; i < dataArray.length; i++) { var item = dataArray[i]; + if (item === null || item === undefined || item.x === undefined) { + break; + } if (labelArray.indexOf(item.x) > -1) { result = true; } From 98ec449e6c05a096703a21b21b7cef6a1a48f3b4 Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Sat, 24 Feb 2018 18:04:57 +0800 Subject: [PATCH 4/8] add the comment --- src/core/core.controller.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index a306a387762..675cdfdd11c 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -694,6 +694,7 @@ module.exports = function(Chart) { if (!dataset.data) { dataset.data = []; } + // format the data if the data array is missing some point according to the labels dataset.data = me.formatDataset(dataset.data); var meta = dataset._meta[me.id]; if (!meta) { From 996c6af9692d71a0b10ac50f0eeac25b105bc279 Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Mon, 21 May 2018 10:10:10 +0800 Subject: [PATCH 5/8] fix the typo as required. --- src/core/core.controller.js | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 675cdfdd11c..dd9b69be91d 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -694,7 +694,7 @@ module.exports = function(Chart) { if (!dataset.data) { dataset.data = []; } - // format the data if the data array is missing some point according to the labels + // Format the data if the data array is missing some point according to the labels dataset.data = me.formatDataset(dataset.data); var meta = dataset._meta[me.id]; if (!meta) { @@ -939,7 +939,7 @@ module.exports = function(Chart) { return changed; }, /** - * format the data list if the data is missing some points in the datasets. + * Format the data list if the data is missing some points in the datasets. * @private * @param {array} dataArray array to format * @return {array} the formated array @@ -947,11 +947,9 @@ module.exports = function(Chart) { formatDataset: function(dataArray) { var labels = this.chart.data.labels; var tmp = dataArray.slice(0); - var labelLen = labels.length; - var dataLen = dataArray.length; var result = dataArray; var match = this.dataInLabel(tmp, labels); - if (match && dataLen < labelLen) { + if (match && dataArray.length < labels.length) { for (var i = 0; i < labels.length; i++) { var label = labels[i]; result[i] = {x: label, y: this.getY(label, tmp)}; @@ -960,12 +958,12 @@ module.exports = function(Chart) { return result; }, /** - * return the y data of the label, if no this label, return null - * @private - * @param {string} label the label in the labels array - * @param {array} dataArray the data in the datasets. - * @return {number} the y data according to the label - */ + * Return the y data of the label, if no this label, return null + * @private + * @param {string} label the label in the labels array + * @param {array} dataArray the data in the datasets. + * @return {number} the y data according to the label + */ getY: function(label, dataArray) { var y = null; for (var i = 0; i < dataArray.length; i++) { @@ -977,24 +975,23 @@ module.exports = function(Chart) { return y; }, /** - * find if the data in datasets is existed in the labels - * @private - * @param {array} dataArray the data in the datasets - * @param {array} labelArray the label in the labels array - * @return {boolean} the match result, true is included - */ + * Find if the data in datasets exists in the labels + * @private + * @param {array} dataArray the data in the datasets + * @param {array} labelArray the label in the labels array + * @return {boolean} the match result, true is included + */ dataInLabel: function(dataArray, labelArray) { - var result = false; for (var i = 0; i < dataArray.length; i++) { var item = dataArray[i]; if (item === null || item === undefined || item.x === undefined) { break; } if (labelArray.indexOf(item.x) > -1) { - result = true; + return true } } - return result; + return false }, }); From 740e0bb9f64fbc52fa143e0171e312e031e3cf72 Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Mon, 21 May 2018 10:24:30 +0800 Subject: [PATCH 6/8] fix the space tab mix issue. --- src/core/core.controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index dd9b69be91d..8a094912290 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -988,10 +988,10 @@ module.exports = function(Chart) { break; } if (labelArray.indexOf(item.x) > -1) { - return true + return true; } } - return false + return false; }, }); From 5d9dc40df681145173e3c1f60595a3ab0990ba5d Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Mon, 21 May 2018 11:05:06 +0800 Subject: [PATCH 7/8] put some funs to other place --- src/core/core.controller.js | 81 +++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 8a094912290..42f0d9cb5d4 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -67,6 +67,43 @@ module.exports = function(Chart) { return position === 'top' || position === 'bottom'; } + /** + * Return the y data of the label, if no this label, return null + * @private + * @param {string} label the label in the labels array + * @param {array} dataArray the data in the datasets. + * @return {number} the y data according to the label + */ + function getY(label, dataArray) { + var y = null; + for (var i = 0; i < dataArray.length; i++) { + var item = dataArray[i]; + if (item.x === label) { + y = item.y; + } + } + return y; + } + /** + * Find if the data in datasets exists in the labels + * @private + * @param {array} dataArray the data in the datasets + * @param {array} labelArray the label in the labels array + * @return {boolean} the match result, true is included + */ + function dataInLabel(dataArray, labelArray) { + for (var i = 0; i < dataArray.length; i++) { + var item = dataArray[i]; + if (item === null || item === undefined || item.x === undefined) { + break; + } + if (labelArray.indexOf(item.x) > -1) { + return true; + } + } + return false; + } + helpers.extend(Chart.prototype, /** @lends Chart */ { /** * @private @@ -695,7 +732,7 @@ module.exports = function(Chart) { dataset.data = []; } // Format the data if the data array is missing some point according to the labels - dataset.data = me.formatDataset(dataset.data); + dataset.data = me._formatDataset(dataset.data); var meta = dataset._meta[me.id]; if (!meta) { meta = dataset._meta[me.id] = { @@ -944,55 +981,19 @@ module.exports = function(Chart) { * @param {array} dataArray array to format * @return {array} the formated array */ - formatDataset: function(dataArray) { + _formatDataset: function(dataArray) { var labels = this.chart.data.labels; var tmp = dataArray.slice(0); var result = dataArray; - var match = this.dataInLabel(tmp, labels); + var match = dataInLabel(tmp, labels); if (match && dataArray.length < labels.length) { for (var i = 0; i < labels.length; i++) { var label = labels[i]; - result[i] = {x: label, y: this.getY(label, tmp)}; + result[i] = {x: label, y: getY(label, tmp)}; } } return result; }, - /** - * Return the y data of the label, if no this label, return null - * @private - * @param {string} label the label in the labels array - * @param {array} dataArray the data in the datasets. - * @return {number} the y data according to the label - */ - getY: function(label, dataArray) { - var y = null; - for (var i = 0; i < dataArray.length; i++) { - var item = dataArray[i]; - if (item.x === label) { - y = item.y; - } - } - return y; - }, - /** - * Find if the data in datasets exists in the labels - * @private - * @param {array} dataArray the data in the datasets - * @param {array} labelArray the label in the labels array - * @return {boolean} the match result, true is included - */ - dataInLabel: function(dataArray, labelArray) { - for (var i = 0; i < dataArray.length; i++) { - var item = dataArray[i]; - if (item === null || item === undefined || item.x === undefined) { - break; - } - if (labelArray.indexOf(item.x) > -1) { - return true; - } - } - return false; - }, }); /** From 4ab606c2b238602ad2f709a464530a35ae9964e2 Mon Sep 17 00:00:00 2001 From: Winter Zhong Date: Thu, 24 May 2018 09:40:52 +0800 Subject: [PATCH 8/8] make less code --- src/core/core.controller.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 42f0d9cb5d4..6a6442c67c3 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -75,14 +75,13 @@ module.exports = function(Chart) { * @return {number} the y data according to the label */ function getY(label, dataArray) { - var y = null; for (var i = 0; i < dataArray.length; i++) { var item = dataArray[i]; if (item.x === label) { - y = item.y; + return item.y; } } - return y; + return null; } /** * Find if the data in datasets exists in the labels