diff --git a/packages/page-referenda/src/Referenda/Referendum.tsx b/packages/page-referenda/src/Referenda/Referendum.tsx index 9910c9222c..eab1e56539 100644 --- a/packages/page-referenda/src/Referenda/Referendum.tsx +++ b/packages/page-referenda/src/Referenda/Referendum.tsx @@ -8,8 +8,8 @@ import type { BaseReferendumProps as Props, CurveGraph, ReferendumProps } from ' import React, { useMemo } from 'react'; import { Chart, Columar, ExpandButton, LinkExternal } from '@polkadot/react-components'; -import { useToggle } from '@polkadot/react-hooks'; -import { BN_MILLION, BN_THOUSAND, formatNumber } from '@polkadot/util'; +import { useBestNumber, useToggle } from '@polkadot/react-hooks'; +import { BN_MILLION, BN_THOUSAND, formatNumber, objectSpread } from '@polkadot/util'; import { useTranslation } from '../translate'; import Killed from './RefKilled'; @@ -46,7 +46,13 @@ interface ChartResult { values: number[][]; } -function getChartProps (totalEligible: BN, isConvictionVote: boolean, info: PalletReferendaReferendumInfoConvictionVotingTally | PalletReferendaReferendumInfoRankedCollectiveTally, trackGraph: CurveGraph): ChartResult[] | null { +interface ChartResultExt extends ChartResult { + currentY: number; + since: BN; + trackGraph: CurveGraph; +} + +function getChartProps (totalEligible: BN, isConvictionVote: boolean, info: PalletReferendaReferendumInfoConvictionVotingTally | PalletReferendaReferendumInfoRankedCollectiveTally, trackGraph: CurveGraph): ChartResultExt[] | null { if (totalEligible && isConvictionVote && info.isOngoing) { const ongoing = info.asOngoing; @@ -59,26 +65,30 @@ function getChartProps (totalEligible: BN, isConvictionVote: boolean, info: Pall : (tally as PalletRankedCollectiveTally).bareAyes; const labels: string[] = []; const values: number[][][] = [[[], [], []], [[], [], []]]; + const supc = totalEligible.isZero() + ? 0 + : currentSupport.mul(BN_THOUSAND).div(totalEligible).toNumber() / 10; + const appc = tally.ayes.isZero() + ? 0 + : tally.ayes.mul(BN_THOUSAND).div(tally.ayes.add(tally.nays)).toNumber() / 10; for (let i = 0; i < approval.length; i++) { labels.push(formatNumber(since.add(x[i]))); const appr = approval[i].div(BN_MILLION).toNumber() / 10; - const appc = tally.ayes.isZero() ? 0 : tally.ayes.mul(BN_THOUSAND).div(tally.ayes.add(tally.nays)).toNumber() / 10; values[0][PT_CUR][i] = appr; values[0][appc < appr ? PT_NEG : PT_POS][i] = appc; const supr = support[i].div(BN_MILLION).toNumber() / 10; - const supc = totalEligible.isZero() ? 0 : currentSupport.mul(BN_THOUSAND).div(totalEligible).toNumber() / 10; values[1][PT_CUR][i] = supr; values[1][supc < supr ? PT_NEG : PT_POS][i] = supc; } return [ - { colors: COLORS, labels, options: OPTIONS, values: values[0] }, - { colors: COLORS, labels, options: OPTIONS, values: values[1] } + { colors: COLORS, currentY: appc, labels, options: OPTIONS, since, trackGraph, values: values[0] }, + { colors: COLORS, currentY: supc, labels, options: OPTIONS, since, trackGraph, values: values[1] } ]; } } @@ -86,8 +96,47 @@ function getChartProps (totalEligible: BN, isConvictionVote: boolean, info: Pall return null; } +function annotateChart (bestNumber: BN, chartProps: ChartResultExt[]): ChartResult[] { + return chartProps.map(({ colors, labels, options, since, trackGraph: { x }, values }, index): ChartResult => { + const currentX = 100 * ( + bestNumber.sub(since.add(x[0])).toNumber() / x[x.length - 1].sub(x[0]).toNumber() + ); + + return { + colors, + labels, + options: objectSpread({ + plugins: { + annotation: { + annotations: { + box1: { + backgroundColor: 'rgba(140, 140, 140, 0.25)', + type: 'box', + xMax: currentX, + xMin: 0, + yMax: index === 0 + ? 100 + : 50, + yMin: 0 + } + // point1: { + // backgroundColor: 'rgba(255, 99, 132, 0.25)', + // type: 'point', + // xValue: currentX, + // yValue: currentY + // } + } + } + } + }, options), + values + }; + }); +} + function Referendum (props: Props): React.ReactElement { const { t } = useTranslation(); + const bestNumber = useBestNumber(); const { activeIssuance, className = '', palletReferenda, value: { id, info, isConvictionVote, trackGraph } } = props; const [isExpanded, toggleExpanded] = useToggle(false); @@ -101,6 +150,11 @@ function Referendum (props: Props): React.ReactElement { [activeIssuance, info, isConvictionVote, trackGraph] ); + const annotatedChartProps = useMemo( + () => bestNumber && chartProps && annotateChart(bestNumber, chartProps), + [bestNumber, chartProps] + ); + const chartLegend = useMemo( () => [ [ @@ -143,20 +197,20 @@ function Referendum (props: Props): React.ReactElement { )} - - {chartProps && isExpanded && ( + + {annotatedChartProps && isExpanded && ( diff --git a/packages/react-components/package.json b/packages/react-components/package.json index 0d41fc573e..347bee3649 100644 --- a/packages/react-components/package.json +++ b/packages/react-components/package.json @@ -27,6 +27,7 @@ "@polkadot/util": "^10.2.1", "@polkadot/util-crypto": "^10.2.1", "chart.js": "^3.9.1", + "chartjs-plugin-annotation": "^2.1.0", "codeflask": "^1.4.1", "i18next": "^21.10.0", "i18next-browser-languagedetector": "^6.1.8", diff --git a/packages/react-components/src/Chart/index.ts b/packages/react-components/src/Chart/index.ts index 9e8b6d204e..b32bbe977a 100644 --- a/packages/react-components/src/Chart/index.ts +++ b/packages/react-components/src/Chart/index.ts @@ -2,12 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 import { CategoryScale, Chart, LinearScale, LineElement, PointElement, Title, Tooltip } from 'chart.js'; +import annotationPlugin from 'chartjs-plugin-annotation'; import Doughnut from './Doughnut'; import HorizBar from './HorizBar'; import Line from './Line'; -Chart.register(CategoryScale, LinearScale, LineElement, PointElement, Title, Tooltip); +Chart.register(CategoryScale, LinearScale, LineElement, PointElement, Title, Tooltip, annotationPlugin); export default { Doughnut, diff --git a/yarn.lock b/yarn.lock index a8603d7ab8..f98652f5d6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3521,6 +3521,7 @@ __metadata: "@types/codeflask": ^1.4.3 "@types/owasp-password-strength-test": ^1.3.0 chart.js: ^3.9.1 + chartjs-plugin-annotation: ^2.1.0 codeflask: ^1.4.1 i18next: ^21.10.0 i18next-browser-languagedetector: ^6.1.8 @@ -7357,6 +7358,15 @@ __metadata: languageName: node linkType: hard +"chartjs-plugin-annotation@npm:^2.1.0": + version: 2.1.0 + resolution: "chartjs-plugin-annotation@npm:2.1.0" + peerDependencies: + chart.js: ">=3.7.0" + checksum: fa6ad1a121c4f9feae991ebf05daaef1da7e1b64721d76f222c33ca54d69a1eb9854beee697ec8509a2578af3560a7fe276b2b597faa7c1435eb6cdf7e022811 + languageName: node + linkType: hard + "chokidar@npm:^3.4.0, chokidar@npm:^3.5.3": version: 3.5.3 resolution: "chokidar@npm:3.5.3"