From 2803a0543b2b59b50fc36e2a9129fd5f5b421b0e Mon Sep 17 00:00:00 2001 From: mp3000mp Date: Thu, 15 Aug 2019 21:36:19 +0200 Subject: [PATCH 1/5] angles option for polar chart --- docs/charts/polar.md | 1 + src/controllers/controller.polarArea.js | 49 ++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/docs/charts/polar.md b/docs/charts/polar.md index 8403a387230..e11dfadeb0d 100644 --- a/docs/charts/polar.md +++ b/docs/charts/polar.md @@ -96,6 +96,7 @@ These are the customisation options specific to Polar Area charts. These options | `startAngle` | `number` | `-0.5 * Math.PI` | Starting angle to draw arcs for the first item in a dataset. | `animation.animateRotate` | `boolean` | `true` | If true, the chart will animate in with a rotation animation. This property is in the `options.animation` object. | `animation.animateScale` | `boolean` | `true` | If true, will animate scaling the chart from the center outwards. +| `angles` | `array` | `(2*Math.PI)/serieCount` | Computed angles to draw each item arc. ## Default Options diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js index 1e0e30ba2dd..65de0006c36 100644 --- a/src/controllers/controller.polarArea.js +++ b/src/controllers/controller.polarArea.js @@ -131,15 +131,17 @@ module.exports = DatasetController.extend({ var starts = me._starts = []; var angles = me._angles = []; var arcs = meta.data; - var i, ilen, angle; + var i, ilen, angle, convertedAngles; + var optionsAngles = me.chart.options.angles || []; me._updateRadius(); meta.count = me.countVisibleElements(); + convertedAngles = me._convertAngles(optionsAngles, meta.count); for (i = 0, ilen = dataset.data.length; i < ilen; i++) { starts[i] = start; - angle = me._computeAngle(i); + angle = me._computeAngle(i, convertedAngles[i]); angles[i] = angle; start += angle; } @@ -251,9 +253,8 @@ module.exports = DatasetController.extend({ /** * @private */ - _computeAngle: function(index) { + _computeAngle: function(index, angle) { var me = this; - var count = this.getMeta().count; var dataset = me.getDataset(); var meta = me.getMeta(); @@ -271,7 +272,45 @@ module.exports = DatasetController.extend({ return resolve([ me.chart.options.elements.arc.angle, - (2 * Math.PI) / count + angle ], context, index); + }, + + /** + * if no angles options, circumference = 2*PI and each arc will be (2*PI)/arcCount + * if angles options, circumference = sum(angles) + * if angles.length < arcCount => undefined angles will be set to (2*PI)/arcCount, + * defined angles will be recompute to fill circumference-sum(undefinedAngles) stay proportional to original values + * if angles.length > arcCount => circumference remains the same and existing angles will be recompute to stay proportional to original values + * @private + */ + _convertAngles: function(angles, visibleElementsCount) { + var angle, diff, i; + var convertedAngles = []; + var circumference = angles.length ? angles.reduce(function(a, b) { + return a + b; + }) : Math.PI * 2; + var defaultAngle = circumference / visibleElementsCount; + var undefinedAnglesCount = visibleElementsCount - angles.length; + + if (undefinedAnglesCount >= 0) { + diff = undefinedAnglesCount * defaultAngle; + } else { + diff = angles.slice(undefinedAnglesCount).reduce(function(a, b) { + return a + b; + }); + } + + for (i = 0; i < visibleElementsCount; i++) { + if (i >= angles.length) { + angle = defaultAngle; + } else { + angle = undefinedAnglesCount >= 0 ? angles[i] * (circumference - diff) / circumference : angles[i] * circumference / (circumference - diff); + } + convertedAngles.push(angle); + } + + return convertedAngles; } + }); From facfc69552c024831773e56cbfb4d94f60c335f3 Mon Sep 17 00:00:00 2001 From: mp3000mp Date: Thu, 15 Aug 2019 23:28:08 +0200 Subject: [PATCH 2/5] trailing spaces --- src/controllers/controller.polarArea.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js index 65de0006c36..6c529302855 100644 --- a/src/controllers/controller.polarArea.js +++ b/src/controllers/controller.polarArea.js @@ -279,8 +279,8 @@ module.exports = DatasetController.extend({ /** * if no angles options, circumference = 2*PI and each arc will be (2*PI)/arcCount * if angles options, circumference = sum(angles) - * if angles.length < arcCount => undefined angles will be set to (2*PI)/arcCount, - * defined angles will be recompute to fill circumference-sum(undefinedAngles) stay proportional to original values + * if angles.length < arcCount => undefined angles will be set to (2*PI)/arcCount, + * defined angles will be recompute to fill circumference-sum(undefinedAngles) stay proportional to original values * if angles.length > arcCount => circumference remains the same and existing angles will be recompute to stay proportional to original values * @private */ From c77c9c391d1ba28c376613704d36dea86b5d6ee2 Mon Sep 17 00:00:00 2001 From: mp3000mp Date: Sat, 17 Aug 2019 19:38:28 +0200 Subject: [PATCH 3/5] Fix bug with hidden data --- src/controllers/controller.polarArea.js | 43 +++++++++++++------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js index 6c529302855..2ffe653a5b0 100644 --- a/src/controllers/controller.polarArea.js +++ b/src/controllers/controller.polarArea.js @@ -256,11 +256,6 @@ module.exports = DatasetController.extend({ _computeAngle: function(index, angle) { var me = this; var dataset = me.getDataset(); - var meta = me.getMeta(); - - if (isNaN(dataset.data[index]) || meta.data[index].hidden) { - return 0; - } // Scriptable options var context = { @@ -284,28 +279,36 @@ module.exports = DatasetController.extend({ * if angles.length > arcCount => circumference remains the same and existing angles will be recompute to stay proportional to original values * @private */ - _convertAngles: function(angles, visibleElementsCount) { - var angle, diff, i; + _convertAngles: function(angles) { + var me = this; + var dataset = me.getDataset(); + var meta = me.getMeta(); + var angle, i, ratio; var convertedAngles = []; var circumference = angles.length ? angles.reduce(function(a, b) { return a + b; }) : Math.PI * 2; - var defaultAngle = circumference / visibleElementsCount; - var undefinedAnglesCount = visibleElementsCount - angles.length; - - if (undefinedAnglesCount >= 0) { - diff = undefinedAnglesCount * defaultAngle; - } else { - diff = angles.slice(undefinedAnglesCount).reduce(function(a, b) { - return a + b; - }); + var defaultAngle = circumference / dataset.data.length; + var initialCircumference = 0; + + for (i = 0; i < dataset.data.length; i++) { + if (!meta.data[i].hidden) { + if (i >= angles.length) { + initialCircumference += defaultAngle; + } else { + initialCircumference += angles[i]; + } + } } + ratio = circumference / initialCircumference; - for (i = 0; i < visibleElementsCount; i++) { - if (i >= angles.length) { - angle = defaultAngle; + for (i = 0; i < dataset.data.length; i++) { + if (meta.data[i].hidden) { + angle = 0; + } else if (i >= angles.length) { + angle = defaultAngle * ratio; } else { - angle = undefinedAnglesCount >= 0 ? angles[i] * (circumference - diff) / circumference : angles[i] * circumference / (circumference - diff); + angle = angles[i] * ratio; } convertedAngles.push(angle); } From 1fcc9ad6bda8efc9d077f56144f9b213a8f54f76 Mon Sep 17 00:00:00 2001 From: mp3000mp Date: Sat, 17 Aug 2019 19:50:03 +0200 Subject: [PATCH 4/5] Reduces cognitive complexity --- src/controllers/controller.polarArea.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js index 2ffe653a5b0..c25a85ebece 100644 --- a/src/controllers/controller.polarArea.js +++ b/src/controllers/controller.polarArea.js @@ -291,27 +291,22 @@ module.exports = DatasetController.extend({ var defaultAngle = circumference / dataset.data.length; var initialCircumference = 0; - for (i = 0; i < dataset.data.length; i++) { - if (!meta.data[i].hidden) { - if (i >= angles.length) { - initialCircumference += defaultAngle; - } else { - initialCircumference += angles[i]; - } - } - } - ratio = circumference / initialCircumference; - for (i = 0; i < dataset.data.length; i++) { if (meta.data[i].hidden) { angle = 0; } else if (i >= angles.length) { - angle = defaultAngle * ratio; + angle = defaultAngle; } else { - angle = angles[i] * ratio; + angle = angles[i]; } convertedAngles.push(angle); + initialCircumference += angle; } + ratio = circumference / initialCircumference; + + convertedAngles = convertedAngles.map(function(item) { + return item * ratio; + }); return convertedAngles; } From ce1c4fde67ef6572d06922dee0d5a28ff5d4d4b3 Mon Sep 17 00:00:00 2001 From: mp3000mp Date: Sat, 17 Aug 2019 21:35:49 +0200 Subject: [PATCH 5/5] Climate --- src/controllers/controller.polarArea.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/controllers/controller.polarArea.js b/src/controllers/controller.polarArea.js index c25a85ebece..0a48ff66b91 100644 --- a/src/controllers/controller.polarArea.js +++ b/src/controllers/controller.polarArea.js @@ -137,7 +137,7 @@ module.exports = DatasetController.extend({ me._updateRadius(); meta.count = me.countVisibleElements(); - convertedAngles = me._convertAngles(optionsAngles, meta.count); + convertedAngles = me._convertAngles(optionsAngles); for (i = 0, ilen = dataset.data.length; i < ilen; i++) { starts[i] = start; @@ -275,7 +275,7 @@ module.exports = DatasetController.extend({ * if no angles options, circumference = 2*PI and each arc will be (2*PI)/arcCount * if angles options, circumference = sum(angles) * if angles.length < arcCount => undefined angles will be set to (2*PI)/arcCount, - * defined angles will be recompute to fill circumference-sum(undefinedAngles) stay proportional to original values + * defined angles will be recompute to fill circumference-sum(undefinedAngles) stay proportional to original values * if angles.length > arcCount => circumference remains the same and existing angles will be recompute to stay proportional to original values * @private */ @@ -283,32 +283,30 @@ module.exports = DatasetController.extend({ var me = this; var dataset = me.getDataset(); var meta = me.getMeta(); - var angle, i, ratio; + var angle, ratio; + var initialCircumference = 0; var convertedAngles = []; var circumference = angles.length ? angles.reduce(function(a, b) { return a + b; }) : Math.PI * 2; var defaultAngle = circumference / dataset.data.length; - var initialCircumference = 0; - for (i = 0; i < dataset.data.length; i++) { - if (meta.data[i].hidden) { + meta.data.forEach(function(element, index) { + if (isNaN(dataset.data[index]) || element.hidden) { angle = 0; - } else if (i >= angles.length) { + } else if (index >= angles.length) { angle = defaultAngle; } else { - angle = angles[i]; + angle = angles[index]; } - convertedAngles.push(angle); initialCircumference += angle; - } + convertedAngles.push(angle); + }); ratio = circumference / initialCircumference; - convertedAngles = convertedAngles.map(function(item) { + return convertedAngles.map(function(item) { return item * ratio; }); - - return convertedAngles; } });