From 5f431c7f47dda724c89142ee49ed55a2f4c90c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zu=CC=88rn?= Date: Thu, 23 Mar 2017 11:08:23 +0100 Subject: [PATCH 1/5] Fixed issue, that category scale shows data points misplaced. See #4060 --- src/scales/scale.category.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 3cee4ebf71f..1ff47ac48a3 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -72,6 +72,15 @@ module.exports = function(Chart) { index = idx !== -1 ? idx : index; } + // If value is a data object, then index is the index in the data array, + // not the index of the scale. We need to change that. + var valueCategory = me.isHorizontal() ? value.x : value.y; + if (valueCategory !== undefined) { + var labelsCategory = me.getLabels(); + var idxCategory = labelsCategory.indexOf(valueCategory); + index = idxCategory !== -1 ? idxCategory : index; + } + if (me.isHorizontal()) { var valueWidth = me.width / offsetAmt; var widthOffset = (valueWidth * (index - me.minIndex)); From 30b5ed20b06dd94d46cf877cebcc08f15b33481e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zu=CC=88rn?= Date: Thu, 23 Mar 2017 11:18:37 +0100 Subject: [PATCH 2/5] Cleaned code --- src/scales/scale.category.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 1ff47ac48a3..a2bd150e165 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -66,19 +66,14 @@ module.exports = function(Chart) { // 1 is added because we need the length but we have the indexes var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - ((me.options.gridLines.offsetGridLines) ? 0 : 1)), 1); - if (value !== undefined && isNaN(index)) { - var labels = me.getLabels(); - var idx = labels.indexOf(value); - index = idx !== -1 ? idx : index; - } - // If value is a data object, then index is the index in the data array, // not the index of the scale. We need to change that. var valueCategory = me.isHorizontal() ? value.x : value.y; - if (valueCategory !== undefined) { - var labelsCategory = me.getLabels(); - var idxCategory = labelsCategory.indexOf(valueCategory); - index = idxCategory !== -1 ? idxCategory : index; + if (valueCategory !== undefined || (value !== undefined && isNaN(index))) { + var labels = me.getLabels(); + value = valueCategory || value; + var idx = labels.indexOf(value); + index = idx !== -1 ? idx : index; } if (me.isHorizontal()) { From fed517857835542afc4086c66ee47a7849d02dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zu=CC=88rn?= Date: Thu, 23 Mar 2017 11:21:36 +0100 Subject: [PATCH 3/5] Fixed Irregular whitespace --- src/scales/scale.category.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index a2bd150e165..e4222d9dadb 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -69,9 +69,9 @@ module.exports = function(Chart) { // If value is a data object, then index is the index in the data array, // not the index of the scale. We need to change that. var valueCategory = me.isHorizontal() ? value.x : value.y; - if (valueCategory !== undefined || (value !== undefined && isNaN(index))) { + if (valueCategory !== undefined || (value !== undefined && isNaN(index))) { var labels = me.getLabels(); - value = valueCategory || value; + value = valueCategory || value; var idx = labels.indexOf(value); index = idx !== -1 ? idx : index; } From 66245dbe166af6c3212ece08067f9b04310764de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zu=CC=88rn?= Date: Fri, 24 Mar 2017 10:11:28 +0100 Subject: [PATCH 4/5] Fixed error in case value is undefined --- src/scales/scale.category.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index e4222d9dadb..eb678a42d69 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -68,7 +68,10 @@ module.exports = function(Chart) { // If value is a data object, then index is the index in the data array, // not the index of the scale. We need to change that. - var valueCategory = me.isHorizontal() ? value.x : value.y; + var valueCategory; + if (value !== undefined) { + valueCategory = me.isHorizontal() ? value.x : value.y; + } if (valueCategory !== undefined || (value !== undefined && isNaN(index))) { var labels = me.getLabels(); value = valueCategory || value; From b8e43893f1acb084ff49b428bfbdf95b578f7d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Zu=CC=88rn?= Date: Mon, 27 Mar 2017 12:58:22 +0200 Subject: [PATCH 5/5] Verified that no error is thrown in case value === null --- src/scales/scale.category.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index eb678a42d69..0388baa00de 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -69,7 +69,7 @@ module.exports = function(Chart) { // If value is a data object, then index is the index in the data array, // not the index of the scale. We need to change that. var valueCategory; - if (value !== undefined) { + if (value !== undefined && value !== null) { valueCategory = me.isHorizontal() ? value.x : value.y; } if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {