From 8113ea5f29f0c65b96f6746006b0143f02387454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 11 May 2023 12:41:59 +0200 Subject: [PATCH] MOBILE-4270 feedback: Avoid division by zero on doughnut charts --- src/addons/mod/feedback/components/index/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/addons/mod/feedback/components/index/index.ts b/src/addons/mod/feedback/components/index/index.ts index 2f877c17a..e9e4defce 100644 --- a/src/addons/mod/feedback/components/index/index.ts +++ b/src/addons/mod/feedback/components/index/index.ts @@ -334,15 +334,16 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity return label; }); - item.chartData = parsedData.map((dataItem) => dataItem.answercount); + item.chartData = parsedData.map((dataItem) => Number(dataItem.answercount)); - if (item.typ == 'multichoicerated') { + if (item.typ === 'multichoicerated') { item.average = parsedData.reduce((prev, current) => prev + Number(current.avg), 0.0); } const subtype = item.presentation.charAt(0); - const single = subtype != 'c'; + // Display bar chart if there are no answers to avoid division by 0 error. + const single = subtype !== 'c' && item.chartData.some((count) => count > 0); item.chartType = single ? 'doughnut' : 'bar'; item.templateName = 'chart'; break;