From 3ec3c873ff1bf0fd1e3505f6fcff7390b8b67da9 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 4 May 2022 01:58:34 +0400 Subject: [PATCH] feat: sort chart options by current probability --- src/web/questions/components/HistoryChart.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/web/questions/components/HistoryChart.tsx b/src/web/questions/components/HistoryChart.tsx index caff32f..4623ce9 100644 --- a/src/web/questions/components/HistoryChart.tsx +++ b/src/web/questions/components/HistoryChart.tsx @@ -56,12 +56,11 @@ const getVictoryGroup = ({ data, i }: { data: DataSet; i: number }) => { }; export const HistoryChart: React.FC = ({ question }) => { - let dataSetsNames: string[] = []; - question.history.forEach((item) => { - let optionNames = item.options.map((option) => option.name); - dataSetsNames.push(...optionNames); - }); + let dataSetsNames = question.options + .sort((a, b) => (a.probability < b.probability ? -1 : 1)) + .map((o) => o.name); dataSetsNames = [...new Set(dataSetsNames)].slice(0, 5); // take the first 5 + const isBinary = (dataSetsNames[0] === "Yes" && dataSetsNames[1] === "No") || (dataSetsNames[0] === "No" && dataSetsNames[1] === "Yes");