Skip to content

Commit c09f2e7

Browse files
committed
chore: segmented bar updated for proportional
1 parent 6433a45 commit c09f2e7

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.2.17",
3+
"version": "1.2.17-beta-5",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/SegmentedBarChart/SegmentedBarChart.tsx

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,58 @@ const SegmentedBarChart: React.FC<SegmentedBarChartProps> = ({
2424
rootClassName,
2525
countClassName,
2626
labelClassName,
27+
isProportional,
2728
}) => {
2829
const total = entities.reduce((sum, entity) => entity.value + sum, 0)
2930
const filteredEntities = entities.filter((entity) => entity.value)
3031

3132
const calcSegmentWidth = (entity: Entity) => `${(entity.value / total) * 100}%`
3233

34+
const renderLabel = (label: string) => (
35+
<span className={labelClassName} data-testid={`segmented-bar-chart-${label}-label`}>
36+
{label}
37+
</span>
38+
)
39+
40+
const renderValue = (value: string | number, label: string) => (
41+
<span className={countClassName} data-testid={`segmented-bar-chart-${label}-value`}>
42+
{value}
43+
</span>
44+
)
45+
46+
const renderContent = (entity: Entity) => {
47+
if (entity.proportionalValue) {
48+
if (entity.value === 0) {
49+
return null
50+
}
51+
// render proportional value only if value count is present
52+
return (
53+
<>
54+
{renderValue(entity.proportionalValue, entity.label)}
55+
<div className="flex left dc__gap-6">
56+
<span style={{ backgroundColor: entity.color }} className="h-12 dc__border-radius-2 w-4" />
57+
{renderLabel(entity.label)}
58+
</div>
59+
</>
60+
)
61+
}
62+
return (
63+
<>
64+
<div className="dot" style={{ backgroundColor: entity.color, width: '10px', height: '10px' }} />
65+
{renderValue(entity.value, entity.label)}
66+
{renderLabel(entity.label)}
67+
</>
68+
)
69+
}
70+
3371
return (
3472
<div className={`flexbox-col w-100 dc__gap-12 ${rootClassName}`}>
35-
<div className="flexbox dc__gap-16">
73+
<div className={`flexbox ${isProportional ? 'dc__gap-24' : 'dc__gap-16'}`}>
3674
{entities?.map((entity) => (
37-
<div className="flexbox dc__gap-4 dc__align-items-center">
38-
<div className="dot" style={{ backgroundColor: entity.color, width: '10px', height: '10px' }} />
39-
<span className={countClassName} data-testid={`segmented-bar-chart-${entity.label}-value`}>
40-
{entity.value}
41-
</span>
42-
<span className={labelClassName} data-testid={`segmented-bar-chart-${entity.label}-label`}>
43-
{entity.label}
44-
</span>
75+
<div
76+
className={`${entity.proportionalValue ? 'flexbox-col' : 'flexbox dc__gap-4 dc__align-items-center'}`}
77+
>
78+
{renderContent(entity)}
4579
</div>
4680
))}
4781
</div>

src/Common/SegmentedBarChart/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ export const FALLBACK_ENTITY = {
1818
color: '#B1B7BC',
1919
label: '-',
2020
value: 0,
21+
proportionalValue: '0/1',
2122
}

src/Common/SegmentedBarChart/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ export type Entity = {
1818
color: string
1919
label: string
2020
value: number
21+
proportionalValue?: string
2122
}
2223

2324
export interface SegmentedBarChartProps {
2425
entities: NonNullable<Entity[]>
2526
rootClassName?: string
2627
countClassName?: string
2728
labelClassName?: string
29+
isProportional?: boolean
2830
}

0 commit comments

Comments
 (0)