Horizontal bar layout for referendum results (#1317)

* Initial stab, gremlins remain

* It is ok.
This commit is contained in:
Jaco Greeff 2019-06-19 18:15:53 +02:00 committed by GitHub
parent c76161263b
commit 9295fb060f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 8 deletions

View File

@ -157,16 +157,16 @@ class Referendum extends React.PureComponent<Props, State> {
return (
<div className='democracy--Referendum-results chart'>
<Chart.Doughnut
<Chart.HorizBar
values={[
{
colors: COLORS_YAY,
label: `${formatBalance(votedYay)} (${formatNumber(voteCountYay)})`,
label: `Yay, ${formatBalance(votedYay)} (${formatNumber(voteCountYay)})`,
value: votedYay.muln(10000).div(votedTotal).toNumber() / 100
},
{
colors: COLORS_NAY,
label: `${formatBalance(votedNay)} (${formatNumber(voteCountNay)})`,
label: `Nay, ${formatBalance(votedNay)} (${formatNumber(voteCountNay)})`,
value: votedNay.muln(10000).div(votedTotal).toNumber() / 100
}
]}

View File

@ -25,13 +25,13 @@
"@types/react-tooltip": "^3.9.1",
"@types/styled-components": "4.1.8",
"@types/styled-theming": "2.2.0",
"chart.js": "^2.7.2",
"chart.js": "^2.8.0",
"codeflask": "^1.4.0",
"i18next": "^15.0.9",
"i18next-browser-languagedetector": "^3.0.1",
"i18next-xhr-backend": "^2.0.1",
"react": "^16.8.2",
"react-chartjs-2": "^2.7.4",
"react-chartjs-2": "^2.7.6",
"react-copy-to-clipboard": "^5.0.1",
"react-dom": "^16.8.2",
"react-dropzone": "^7.0.1",

View File

@ -0,0 +1,78 @@
// Copyright 2017-2019 @polkadot/ui-app authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BareProps } from '../types';
import BN from 'bn.js';
import React from 'react';
import ChartJs from 'chart.js';
import { HorizontalBar } from 'react-chartjs-2';
import { bnToBn } from '@polkadot/util';
type Value = {
colors: Array<string>,
label: string,
value: number | BN
};
type Props = BareProps & {
aspectRatio?: number,
values: Array<Value>
};
type Options = {
colorNormal: Array<string>,
colorHover: Array<string>,
data: Array<number>,
labels: Array<string>
};
const alphaColor = (hexColor: string): string =>
ChartJs.helpers.color(hexColor).alpha(0.65).rgbString();
export default class ChartHorizBar extends React.PureComponent<Props> {
render () {
const { aspectRatio = 4, className, style, values } = this.props;
const options = values.reduce((options, { colors: [normalColor = '#00f', hoverColor], label, value }) => {
options.colorNormal.push(alphaColor(normalColor));
options.colorHover.push(alphaColor(hoverColor || normalColor));
options.data.push(bnToBn(value).toNumber());
options.labels.push(label);
return options;
}, {
colorNormal: [],
colorHover: [],
data: [],
labels: []
} as Options);
return (
<div
className={className}
style={style}
>
<HorizontalBar
data={{
labels: options.labels,
datasets: [{
data: options.data,
backgroundColor: options.colorNormal,
hoverBackgroundColor: options.colorHover
}]
}}
options={{
// width/height by default this is "1", i.e. a square box
aspectRatio,
// no need for the legend, expect the labels contain everything
legend: {
display: false
}
}}
/>
</div>
);
}
}

View File

@ -3,7 +3,9 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import Doughnut from './Doughnut';
import HorizBar from './HorizBar';
export default {
Doughnut
Doughnut,
HorizBar
};

View File

@ -4116,7 +4116,7 @@ chardet@^0.7.0:
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==
chart.js@^2.7.2:
chart.js@^2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-2.8.0.tgz#b703b10d0f4ec5079eaefdcd6ca32dc8f826e0e9"
integrity sha512-Di3wUL4BFvqI5FB5K26aQ+hvWh8wnP9A3DWGvXHVkO13D3DSnaSsdZx29cXlEsYKVkn1E2az+ZYFS4t0zi8x0w==
@ -11565,7 +11565,7 @@ rc@^1.2.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"
react-chartjs-2@^2.7.4:
react-chartjs-2@^2.7.6:
version "2.7.6"
resolved "https://registry.yarnpkg.com/react-chartjs-2/-/react-chartjs-2-2.7.6.tgz#b8cd29bed00bf55b9e8172b06466b4ecf2b86bfb"
integrity sha512-xDr0jhgt/o26atftXxTVsepz+QYZI2GNKBYpxtLvYgwffLUm18a9n562reUJAHvuwKsy2v+qMlK5HyjFtSW0mg==