From 2643f4f1c3716d85b039372f916693be220adffb Mon Sep 17 00:00:00 2001 From: Jaco Date: Sat, 17 Dec 2022 11:43:01 +0200 Subject: [PATCH] Referendum chart labels (#8564) --- .../src/Referenda/Referendum.tsx | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/packages/page-referenda/src/Referenda/Referendum.tsx b/packages/page-referenda/src/Referenda/Referendum.tsx index 3275874767..7a38200038 100644 --- a/packages/page-referenda/src/Referenda/Referendum.tsx +++ b/packages/page-referenda/src/Referenda/Referendum.tsx @@ -6,6 +6,7 @@ import type { BN } from '@polkadot/util'; import type { BaseReferendumProps as Props, CurveGraph, ReferendumProps } from '../types'; import React, { useMemo } from 'react'; +import styled from 'styled-components'; import { Chart, Columar, ExpandButton, LinkExternal } from '@polkadot/react-components'; import { useBestNumber, useToggle } from '@polkadot/react-hooks'; @@ -41,6 +42,11 @@ const OPTIONS = { }; interface ChartResult { + progress: { + percent: number; + value: BN; + total: BN; + }; labels: string[]; values: number[][]; } @@ -57,7 +63,7 @@ interface ChartProps extends ChartResult { options: typeof OPTIONS; } -function getChartProps (totalEligible: BN, isConvictionVote: boolean, info: PalletReferendaReferendumInfoConvictionVotingTally | PalletReferendaReferendumInfoRankedCollectiveTally, trackGraph: CurveGraph): ChartResultExt[] | null { +function getChartResult (totalEligible: BN, isConvictionVote: boolean, info: PalletReferendaReferendumInfoConvictionVotingTally | PalletReferendaReferendumInfoRankedCollectiveTally, trackGraph: CurveGraph): ChartResultExt[] | null { if (totalEligible && isConvictionVote && info.isOngoing) { const ongoing = info.asOngoing; @@ -98,8 +104,8 @@ function getChartProps (totalEligible: BN, isConvictionVote: boolean, info: Pall } return [ - { changeX: appx, currentY: appc, labels, since, trackGraph, values: values[0] }, - { changeX: supx, currentY: supc, labels, since, trackGraph, values: values[1] } + { changeX: appx, currentY: appc, labels, progress: { percent: appc, total: ongoing.tally.ayes.add(ongoing.tally.nays), value: ongoing.tally.ayes }, since, trackGraph, values: values[0] }, + { changeX: supx, currentY: supc, labels, progress: { percent: supc, total: totalEligible, value: currentSupport }, since, trackGraph, values: values[1] } ]; } } @@ -107,8 +113,8 @@ function getChartProps (totalEligible: BN, isConvictionVote: boolean, info: Pall return null; } -function annotateChart (bestNumber: BN, chartProps: ChartResultExt[]): ChartProps[] { - return chartProps.map(({ changeX, currentY, labels, since, trackGraph: { x }, values }, index): ChartProps => { +function getChartProps (bestNumber: BN, chartProps: ChartResultExt[]): ChartProps[] { + return chartProps.map(({ changeX, currentY, labels, progress, since, trackGraph: { x }, values }, index): ChartProps => { const currentX = 100 * ( bestNumber.sub(since.add(x[0])).toNumber() / x[x.length - 1].sub(x[0]).toNumber() ); @@ -161,15 +167,10 @@ function annotateChart (bestNumber: BN, chartProps: ChartResultExt[]): ChartProp } : {} ) - // point1: { - // backgroundColor: 'rgba(255, 99, 132, 0.25)', - // type: 'point', - // xValue: currentX, - // yValue: currentY - // } } } }, OPTIONS), + progress, values }; }); @@ -186,14 +187,14 @@ function Referendum (props: Props): React.ReactElement { [info] ); - const chartProps = useMemo( - () => activeIssuance && trackGraph && getChartProps(activeIssuance, isConvictionVote, info, trackGraph), + const chartResult = useMemo( + () => activeIssuance && trackGraph && getChartResult(activeIssuance, isConvictionVote, info, trackGraph), [activeIssuance, info, isConvictionVote, trackGraph] ); - const annotatedChartProps = useMemo( - () => bestNumber && chartProps && annotateChart(bestNumber, chartProps), - [bestNumber, chartProps] + const chartProps = useMemo( + () => bestNumber && chartResult && getChartProps(bestNumber, chartResult), + [bestNumber, chartResult] ); const chartLegend = useMemo( @@ -238,20 +239,22 @@ function Referendum (props: Props): React.ReactElement { )} - - {annotatedChartProps && isExpanded && ( + + {chartProps && isExpanded && ( - + +

{t('approval / {{percent}}%', { replace: { percent: chartProps[0].progress.percent.toFixed(1) } })}

- + +

{t('support / {{percent}}%', { replace: { percent: chartProps[1].progress.percent.toFixed(1) } })}

@@ -262,4 +265,13 @@ function Referendum (props: Props): React.ReactElement { ); } -export default React.memo(Referendum); +export default React.memo(styled(Referendum)` + .chartColumn { + h1 { + font-size: 1.25rem; + margin-bottom: 0; + margin-top: 1rem; + text-align: center; + } + } +`);