Skip to content

Commit

Permalink
fix(view): extract label function to widget
Browse files Browse the repository at this point in the history
  • Loading branch information
MauriceNino committed Jun 8, 2022
1 parent f5e59b2 commit 8718f5e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions apps/view/src/components/chart-components.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FC, useMemo, useState } from 'react';
import { Area, AreaChart, Pie, PieChart, Sector } from 'recharts';
import styled, { useTheme } from 'styled-components';
import { bytePrettyPrint } from '../utils/calculations';
import ThemedText from './text';

type DefaultAreaChartProps = {
Expand Down Expand Up @@ -57,7 +56,8 @@ const HoverLabel = styled(ThemedText)<{ top: number; left: number }>`

const renderLabel = (
{ cx, cy, midAngle, innerRadius, outerRadius, payload }: any,
theme: any
theme: any,
labelRenderer: (value: number) => string
) => {
const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
const centerx = cx + radius * Math.cos((-midAngle * Math.PI) / 180);
Expand All @@ -75,7 +75,7 @@ const renderLabel = (
pointerEvents: 'none',
}}
>
{bytePrettyPrint(value)}
{labelRenderer(value)}
</text>
) : null;
};
Expand All @@ -91,13 +91,15 @@ type DefaultPieChartProps = {
color: string;
children: React.ReactNode;
data: any[];
labelRenderer: (value: number) => string;
};
export const DefaultPieChart: FC<DefaultPieChartProps> = ({
data,
height,
width,
color,
children,
labelRenderer,
}) => {
const id = useMemo(() => {
return `pie-chart-${++globalId}`;
Expand Down Expand Up @@ -162,7 +164,7 @@ export const DefaultPieChart: FC<DefaultPieChartProps> = ({
}}
activeShape={renderActiveShape}
labelLine={false}
label={props => renderLabel(props, theme)}
label={props => renderLabel(props, theme, labelRenderer)}
>
{children}
</Pie>
Expand Down
1 change: 1 addition & 0 deletions apps/view/src/widgets/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const StorageWidget: FC<StorageWidgetProps> = ({
width={size.width}
height={size.height}
color={theme.colors.storagePrimary}
labelRenderer={val => bytePrettyPrint(val)}
>
<Cell
key='cell-used'
Expand Down

0 comments on commit 8718f5e

Please sign in to comment.