ghost-portal/packages/react-components/src/UsageBar.tsx
Daria 7c9f388698
Coretime Chain - Overview UI Tab (#10898)
* added overview page

* sanitized task

* added workplan table

* staged missing files

* updated filters

* fixed some types

* updated mask graph

* copyright

* linting

* install

* added relay overview

* linting

* refactoring hooks

* translations

* updated coretime deps

* updated tsconfig

* fixed build

* root tsconfig

* fix tsconfig

* app-coretime and app-broker

* filter bug fix

* prettied mask bar

* updated deps

* updated deps broker

* removed TS filter and added graph

* linting

* fixed headers

* fixed filter

* updated coretime summary

* updated relay tables

* fixed bulk price display

* updated regions

* styling

* fixed pallet version mismaatch info

* fixed expand button

* fixed alignment issues

* yarn install

* integrated some suggestions

* linting

* updated version

* Fix Header on table

* Small nit around naming

* Usage bar console err fix: NaN

* rearranged the layout for page broker on Coretime chain, changed UsageBar element to be more generic

* filtering parachains by id

* moved a type to a seprate type file

* skip build and npm

* generated locales

* added default ui colours to test chains and kusama

* abstracted logic from the rendering

* default ui highlight fallback

* table responsive - hiding timeslice info from the rows on mobile

* version updates for react-components and react-query

* exporting defaultHighlight

* responsive summary

* small fix for workplan filtering

* separated coretime chain page

* removed unused references

* added useBrokerReservations call and marked reserved chains

* useBrokerLease, added Legacy Lease type

* showing last block for legacy leases

* added processing at data retrieval

* fix: missing icon

* linting

* organized files, minor refactor

* moved filters out of the Overview componennt

* minor refactor

* version update

* regenerated yarn lock

* renamed column

* linter :)

* ts fixes :)

---------

Co-authored-by: bee344 <alberto.penayo@parity.io>
Co-authored-by: Alberto Nicolas Penayo <74352651+bee344@users.noreply.github.com>
Co-authored-by: tarikgul <tariksnow37@gmail.com>
2024-09-10 08:30:41 -04:00

108 lines
2.7 KiB
TypeScript

// Copyright 2017-2024 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import { styled } from '@polkadot/react-components';
interface PieChartProps {
data: { label: string; value: number; color: string }[];
}
const Container = styled.div`
display: flex;
align-items: center;
`;
const GraphContainer = styled.div`
position: relative;
`;
const LegendContainer = styled.div`
display: flex;
flex-direction: column;
margin-left: 20px;
`;
const LegendItem = styled.div`
display: flex;
align-items: center;
margin-bottom: 10px;
`;
const ColorBox = styled.div<{ color: string }>`
width: 20px;
height: 20px;
background-color: ${(props: { color: string }) => props.color};
margin-right: 10px;
`;
function UsageBar ({ data }: PieChartProps): React.ReactElement<PieChartProps> {
const radius = 50;
const strokeWidth = 15;
const circumference = 2 * Math.PI * radius;
const total = data.reduce((acc, item) => acc + item.value, 0);
let cumulativeOffset = 0;
if (!total) {
return <></>;
}
return (
<Container>
<GraphContainer>
<svg
height={radius * 2}
viewBox={`0 0 ${radius * 2} ${radius * 2}`}
width={radius * 2}
>
<circle
cx={radius}
cy={radius}
fill='none'
r={radius - strokeWidth / 2}
stroke='#f0f0f0'
strokeWidth={strokeWidth}
/>
{data.map((item, index) => {
const percentage = (item.value / total) * 100;
const dashArray = (percentage / 100) * circumference;
const dashOffset = (cumulativeOffset / 100) * circumference;
cumulativeOffset += percentage;
return (
<circle
cx={radius}
cy={radius}
fill='none'
key={index}
r={radius - strokeWidth / 2}
stroke={item.color}
strokeDasharray={`${dashArray} ${circumference - dashArray}`}
strokeDashoffset={-dashOffset}
strokeWidth={strokeWidth}
transform={`rotate(-90 ${radius} ${radius})`}
>
<title>{`${item.label}: ${percentage.toFixed(2)}%`}</title>
</circle>
);
})}
</svg>
</GraphContainer>
<LegendContainer>
{data.map((item, index) => (
<LegendItem key={index}>
<ColorBox color={item.color} />
<span>{`${item.label}: ${((item.value / total) * 100).toFixed(2)}%`}</span>
</LegendItem>
))}
</LegendContainer>
</Container>
);
}
export default React.memo(UsageBar);