-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat(#65): add status chart #89
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
98f1473
refactor(#65): change colored circle to accept other colors and make …
mari1912 45aef89
feat(#65): add status chart component
mari1912 28d9fe9
feat(#65): add a status chart to tree deatils page
mari1912 fac943a
refactor(#65): move the content to tab instead of tree details page
mari1912 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
import { PieChart } from '@mui/x-charts/PieChart'; | ||
|
||
import { MdArrowDownward, MdArrowUpward } from 'react-icons/md'; | ||
import React, { ReactElement, useMemo } from 'react'; | ||
|
||
import { useDrawingArea } from '@mui/x-charts'; | ||
import { styled } from '@mui/material'; | ||
|
||
import ColoredCircle from '../ColoredCircle/ColoredCircle'; | ||
|
||
type StatusChartValues = { | ||
value: number; | ||
label: string; | ||
color: Colors; | ||
}; | ||
|
||
export enum Colors { | ||
Red = '#E15739', | ||
Green = '#53D07C', | ||
Yellow = '#FFD27C', | ||
Gray = '#EAEAEA', | ||
} | ||
|
||
export interface IStatusChart { | ||
elements: StatusChartValues[]; | ||
insideText?: StatusChartValues; | ||
increaseElement?: StatusChartValues; | ||
decreaseElement?: StatusChartValues; | ||
pieCentralLabel?: string; | ||
pieCentralDescription?: ReactElement; | ||
title: ReactElement; | ||
type: 'chart'; | ||
} | ||
|
||
interface IChartLegend { | ||
chartValues: (StatusChartValues | undefined)[]; | ||
} | ||
|
||
interface IRegressionElement { | ||
element: StatusChartValues; | ||
icon: ReactElement; | ||
} | ||
|
||
interface IRegressionStatus { | ||
increaseElement?: StatusChartValues; | ||
decreaseElement?: StatusChartValues; | ||
} | ||
|
||
const StatusChart = ({ | ||
elements, | ||
increaseElement, | ||
decreaseElement, | ||
pieCentralLabel, | ||
pieCentralDescription, | ||
}: IStatusChart): JSX.Element => { | ||
const showChart = elements.some(element => element.value > 0); | ||
|
||
const dataSeries = useMemo(() => { | ||
return [ | ||
{ | ||
data: elements, | ||
innerRadius: 50, | ||
outerRadius: 80, | ||
}, | ||
]; | ||
}, [elements]); | ||
|
||
if (!showChart) { | ||
return <></>; | ||
} | ||
return ( | ||
<div className="flex items-center"> | ||
<PieChart | ||
series={dataSeries} | ||
width={200} | ||
height={200} | ||
slotProps={{ | ||
legend: { | ||
hidden: true, | ||
}, | ||
}} | ||
> | ||
<PieCenterLabel | ||
label={pieCentralLabel ?? ''} | ||
description={pieCentralDescription ?? <></>} | ||
/> | ||
</PieChart> | ||
<div className="flex flex-row gap-4 pt-5"> | ||
<ChartLegend chartValues={elements} /> | ||
<RegressionsStatus | ||
increaseElement={increaseElement} | ||
decreaseElement={decreaseElement} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const StatusChartMemoized = React.memo(StatusChart); | ||
|
||
const getColorClassName = (color: Colors): string => { | ||
switch (color) { | ||
case Colors.Red: | ||
return 'bg-red'; | ||
case Colors.Green: | ||
return 'bg-green'; | ||
case Colors.Yellow: | ||
return 'bg-yellow'; | ||
case Colors.Gray: | ||
return 'bg-mediumGray'; | ||
default: | ||
return ''; | ||
} | ||
}; | ||
|
||
const ChartLegend = ({ chartValues }: IChartLegend): JSX.Element => { | ||
return ( | ||
<div className="flex flex-col gap-2"> | ||
{chartValues.map(chartValue => ( | ||
<div key={chartValue?.label} className="flex flex-row"> | ||
{chartValue && ( | ||
<div className="pt-1 pr-2"> | ||
<ColoredCircle | ||
backgroundClassName={getColorClassName(chartValue.color)} | ||
/> | ||
</div> | ||
)} | ||
<div className="flex flex-col gap-1"> | ||
<span className="font-bold">{chartValue?.value}</span> | ||
<span>{chartValue?.label}</span> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const RegressionsStatus = ({ | ||
increaseElement, | ||
decreaseElement, | ||
}: IRegressionStatus): JSX.Element => { | ||
return ( | ||
<div className="flex flex-col gap-2"> | ||
{increaseElement && ( | ||
<RegressionElement | ||
element={increaseElement} | ||
icon={<MdArrowUpward color={Colors.Red} />} | ||
/> | ||
)} | ||
{decreaseElement && ( | ||
<RegressionElement | ||
element={decreaseElement} | ||
icon={<MdArrowDownward color={Colors.Green} />} | ||
/> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
const RegressionElement = ({ | ||
element, | ||
icon, | ||
}: IRegressionElement): JSX.Element => { | ||
return ( | ||
<div className="flex flex-row gap-2"> | ||
<div className="pt-1">{icon}</div> | ||
<div className="flex flex-col gap-1"> | ||
<span className="font-bold">{element.value}</span> | ||
<span>{element.label}</span> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const PieCenterLabel = ({ | ||
label, | ||
description, | ||
}: { | ||
label: string; | ||
description: ReactElement; | ||
}): JSX.Element => { | ||
const { width, height, left, top } = useDrawingArea(); | ||
// eslint-disable-next-line no-magic-numbers | ||
const yPositionLabel = 9 / 20; | ||
// eslint-disable-next-line no-magic-numbers | ||
const yPositionDescription = 11 / 20; | ||
const xPosition = left + width / 2; | ||
return ( | ||
<> | ||
<StyledText x={xPosition} y={top + yPositionLabel * height}> | ||
mari1912 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{label} | ||
</StyledText> | ||
<StyledText x={xPosition} y={top + yPositionDescription * height}> | ||
{description} | ||
</StyledText> | ||
</> | ||
); | ||
}; | ||
|
||
const StyledText = styled('text')(() => ({ | ||
textAnchor: 'middle', | ||
dominantBaseline: 'central', | ||
})); | ||
|
||
export default StatusChartMemoized; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worries me, do we have a ticket to checkout it out latter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can create one