forked from kubev2v/migration-planner-ui-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationChart.tsx
More file actions
256 lines (243 loc) · 8.34 KB
/
MigrationChart.tsx
File metadata and controls
256 lines (243 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { css } from "@emotion/css";
import {
Button,
Content,
ContentVariants,
Flex,
FlexItem,
Popover,
} from "@patternfly/react-core";
import { InfoCircleIcon } from "@patternfly/react-icons";
import { Table, Tbody, Td, Tr } from "@patternfly/react-table";
import React, { useMemo } from "react";
interface OSData {
name: string;
count: number;
legendCategory: string;
infoText?: string;
}
interface MigrationChartProps {
data: OSData[];
legend?: Record<string, string>;
dataLength?: DLength;
maxHeight?: string;
barHeight?: number;
}
type DLength =
| 10
| 15
| 20
| 25
| 30
| 35
| 40
| 45
| 50
| 60
| 70
| 80
| 90
| 100;
// ---------------------------------------------------------------------------
// Styles
// ---------------------------------------------------------------------------
const upgradeRecommendationPopover = css`
.popover-override .pf-v5-c-popover__close .pf-v5-c-button.pf-m-plain {
color: var(--pf-t--global--text--color--regular);
}
.popover-override .pf-v5-c-popover__close .pf-v5-c-button.pf-m-plain:hover {
color: var(--pf-t--global--text--color--regular);
}
`;
const legendColors = ["#28a745", "#f0ad4e", "#d9534f", "#C9190B"];
const MigrationChart: React.FC<MigrationChartProps> = ({
data,
legend,
dataLength = 40,
maxHeight = "200px",
barHeight = 8,
}: MigrationChartProps) => {
// Ensure tiny percentages still render a visible colored segment
const MIN_BAR_PX = 3;
const dynamicLegend = useMemo<Record<string, string>>(() => {
const legendMap: Record<string, string> = {};
const seen = new Set<string>();
data.forEach((item) => {
const key = item.legendCategory;
if (!seen.has(key)) {
seen.add(key);
legendMap[key] = legendColors[seen.size - 1] ?? legendColors[0];
}
});
return legendMap;
}, [data]);
// Calculate the sum of all count values to normalize bar widths
const sumOfAllCounts = useMemo(() => {
if (!data || data.length === 0) return 1;
return data.reduce((sum, item) => sum + item.count, 0) || 1;
}, [data]);
const chartLegend = legend ?? dynamicLegend;
const getColor = (name: string): string =>
chartLegend[name] ?? legendColors[0];
return (
<Flex
direction={{ default: "column" }}
spaceItems={{ default: "spaceItemsLg" }}
>
{/* Legend */}
<FlexItem>
<Flex
spaceItems={{ default: "spaceItemsLg" }}
justifyContent={{ default: "justifyContentFlexEnd" }}
>
{Object.entries(chartLegend).map(([key, color]) => (
<FlexItem key={key}>
<Flex
alignItems={{ default: "alignItemsCenter" }}
spaceItems={{ default: "spaceItemsSm" }}
>
<FlexItem>
<div
style={{
width: "12px",
height: "12px",
backgroundColor: color,
borderRadius: "2px",
}}
/>
</FlexItem>
<FlexItem>
<Content component={ContentVariants.small}>{key}</Content>
</FlexItem>
</Flex>
</FlexItem>
))}
</Flex>
</FlexItem>
{/* Chart Area */}
<FlexItem>
<Flex
direction={{ default: "column" }}
spaceItems={{ default: "spaceItemsMd" }}
>
<div
style={{
maxHeight: maxHeight.includes("auto") ? "none" : maxHeight,
overflowY: maxHeight.includes("auto") ? "visible" : "auto",
}}
>
<Table variant="compact" borders={false}>
<Tbody>
{data.map((item, index) => (
<Tr key={index}>
<Td
width={dataLength}
style={{ paddingLeft: "0px", paddingTop: "4px" }}
>
<Flex
alignItems={{ default: "alignItemsCenter" }}
spaceItems={{ default: "spaceItemsXs" }}
flexWrap={{ default: "nowrap" }}
>
<FlexItem>
<Content
component={ContentVariants.p}
style={{
fontSize: "clamp(0.4rem, 0.7vw, 1.1rem)",
overflow: "hidden",
textOverflow: "ellipsis",
wordBreak: "break-word",
display: "-webkit-box",
WebkitLineClamp: 1,
textTransform: "capitalize",
WebkitBoxOrient: "vertical",
}}
>
{item.name}
</Content>
</FlexItem>
{item.infoText ? (
<FlexItem shrink={{ default: "shrink" }}>
<Popover
className={upgradeRecommendationPopover}
position="bottom"
headerContent="Upgrade to get support"
bodyContent={<div>{item.infoText}</div>}
>
<Button
type="button"
aria-label="Open operating system upgrade information"
variant="plain"
style={{
padding: "0",
verticalAlign: "middle",
}}
>
<InfoCircleIcon color="var(--pf-t--global--icon--color--status--info--default)" />
</Button>
</Popover>
</FlexItem>
) : null}
</Flex>
</Td>
<Td>
{/* Visual Bar */}
<div>
<div
style={{
position: "relative",
height: `${barHeight}px`,
backgroundColor:
"var(--pf-t--global--background--color--secondary--default)",
overflow: "hidden",
}}
>
{((): React.ReactNode => {
const barWidth =
sumOfAllCounts > 0
? (item.count / sumOfAllCounts) * 100
: 0;
const hasValue = barWidth > 0;
return (
<div
style={{
height: "100%",
width: `${barWidth}%`,
minWidth: hasValue ? `${MIN_BAR_PX}px` : "0",
backgroundColor: `${getColor(
item.legendCategory,
)}`,
transition: "width 0.3s ease",
}}
/>
);
})()}
</div>
</div>
</Td>
<Td
width={10}
style={{
paddingRight: "0px",
textAlign: "center",
paddingTop: "5px",
}}
>
<Content
component="p"
style={{ fontSize: "clamp(0.4rem, 0.7vw, 1.1rem)" }}
>
{item.count}
</Content>
</Td>
</Tr>
))}
</Tbody>
</Table>
</div>
</Flex>
</FlexItem>
</Flex>
);
};
export default MigrationChart;