From d2a4182e0919a5c1918e3488754bfdbcfd9169b2 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 1 Apr 2020 10:00:28 +0200 Subject: [PATCH] Chart background via plugin (#2495) * Chart background via plugin * Sort democracy --- .../src/Overview/Referendum.tsx | 36 +++++-------- .../src/Overview/ReferendumVotes.tsx | 51 +++++++++++++++++++ packages/page-staking/src/index.tsx | 14 +++-- packages/react-components/src/Chart/Line.tsx | 20 +++++++- 4 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 packages/page-democracy/src/Overview/ReferendumVotes.tsx diff --git a/packages/page-democracy/src/Overview/Referendum.tsx b/packages/page-democracy/src/Overview/Referendum.tsx index d40d853a12..fea9629542 100644 --- a/packages/page-democracy/src/Overview/Referendum.tsx +++ b/packages/page-democracy/src/Overview/Referendum.tsx @@ -7,15 +7,15 @@ import { BlockNumber } from '@polkadot/types/interfaces'; import React, { useMemo } from 'react'; import styled from 'styled-components'; -import { Button, Expander, LinkExternal, Tag } from '@polkadot/react-components'; +import { Button, LinkExternal, Tag } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; -import { FormatBalance, BlockToTime } from '@polkadot/react-query'; +import { BlockToTime } from '@polkadot/react-query'; import { formatNumber, isBoolean } from '@polkadot/util'; import { useTranslation } from '../translate'; import PreImageButton from './PreImageButton'; import ProposalCell from './ProposalCell'; -import ReferendumVote from './ReferendumVote'; +import ReferendumVotes from './ReferendumVotes'; import Voting from './Voting'; interface Props { @@ -54,26 +54,16 @@ function Referendum ({ className, value: { allAye, allNay, image, imageHash, ind #{formatNumber(enactBlock)} - - {voteCountAye ? ` (${formatNumber(voteCountAye)})` : '' }}> - {allAye.map((vote) => - - )} - - - - {voteCountNay ? ` (${formatNumber(voteCountNay)})` : '' }}> - {allNay.map((vote) => - - )} - - + + {isBoolean(isPassing) && ( { + const [sorted, setSorted] = useState([]); + + useEffect((): void => { + setSorted( + votes.sort((a, b) => { + const ta = a.balance.muln(LOCKS[a.vote.conviction.toNumber()]).divn(10); + const tb = b.balance.muln(LOCKS[b.vote.conviction.toNumber()]).divn(10); + + return tb.cmp(ta); + }) + ); + }, [votes]); + + return ( + + {count ? ` (${formatNumber(count)})` : '' }}> + {sorted.map((vote) => + + )} + + + ); +} + +export default React.memo(ReferendumVotes); diff --git a/packages/page-staking/src/index.tsx b/packages/page-staking/src/index.tsx index cab890965d..6c13393dd8 100644 --- a/packages/page-staking/src/index.tsx +++ b/packages/page-staking/src/index.tsx @@ -153,11 +153,15 @@ export default React.memo(styled(StakingApp)` display: none; } - .staking--Chart h1 { - margin-bottom: 0.5rem; - } - - .staking--Chart+.staking--Chart { + .staking--Chart { margin-top: 1.5rem; + + h1 { + margin-bottom: 0.5rem; + } + + .ui--Spinner { + margin: 2.5rem auto; + } } `); diff --git a/packages/react-components/src/Chart/Line.tsx b/packages/react-components/src/Chart/Line.tsx index 5182a455eb..ae61daf36f 100644 --- a/packages/react-components/src/Chart/Line.tsx +++ b/packages/react-components/src/Chart/Line.tsx @@ -7,7 +7,7 @@ import { LineProps } from './types'; import BN from 'bn.js'; import React, { useEffect, useState } from 'react'; import ChartJs from 'chart.js'; -import { Line } from 'react-chartjs-2'; +import * as Chart from 'react-chartjs-2'; interface State { chartData?: ChartJs.ChartData; @@ -76,6 +76,22 @@ function calculateOptions (colors: (string | undefined)[] = [], legends: string[ function LineChart ({ className, colors, labels, legends, style, values }: LineProps): React.ReactElement | null { const [{ chartData, chartOptions, jsonValues }, setState] = useState({}); + useEffect((): void => { + // Ok, this does exists, but the export if not there in the typings - so it works, + // but we have to jiggle around here to get it to actually compile :( + (Chart as any).Chart.pluginService.register({ + beforeDraw: (chart: any) => { + const ctx = chart.chart.ctx; + const chartArea = chart.chartArea; + + ctx.save(); + ctx.fillStyle = '#fff'; + ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top); + ctx.restore(); + } + }); + }, []); + useEffect((): void => { const newJsonValues = JSON.stringify(values); @@ -93,7 +109,7 @@ function LineChart ({ className, colors, labels, legends, style, values }: LineP className={className} style={style} > -