Skip to content

Commit b6c0791

Browse files
Refactor unit base and label calculation in NonSymmetricalQuantileChart and SymmetricalQuantileChart to remove duplicate calculation
1 parent c5568c4 commit b6c0791

File tree

2 files changed

+18
-54
lines changed

2 files changed

+18
-54
lines changed

ui/src/components/NonSymmetricalQuantileChart.tsx

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,14 @@ const NonSymmetricalQuantileChart = ({
8989
),
9090
});
9191

92-
// Calculate unit base and label
93-
const valueBase = useMemo(() => {
94-
if (!unitRange || !seriesQuantile.length) return 1;
95-
96-
const allValues = seriesQuantile.flatMap((serie: any) =>
97-
serie.data
98-
.map(([_, value]: [number, any]) =>
99-
typeof value === 'string' ? parseFloat(value) : value,
100-
)
101-
.filter((v: any) => v !== null && !isNaN(v)),
102-
);
103-
104-
const maxValue = Math.max(...allValues);
105-
const unit = unitRange
106-
.slice()
107-
.reverse()
108-
.find((range) => maxValue >= range.threshold);
109-
110-
return unit ? unit.threshold || 1 : 1;
111-
}, [unitRange, seriesQuantile]);
112-
113-
const unitLabel = useMemo(() => {
92+
const { valueBase, unitLabel } = useMemo(() => {
11493
if (yAxisType === 'percentage') {
115-
return '%';
94+
return { valueBase: 1, unitLabel: '%' };
95+
}
96+
97+
if (!unitRange || !seriesQuantile.length) {
98+
return { valueBase: 1, unitLabel: '' };
11699
}
117-
if (!unitRange || !seriesQuantile.length) return '';
118100

119101
const allValues = seriesQuantile.flatMap((serie: any) =>
120102
serie.data
@@ -130,7 +112,10 @@ const NonSymmetricalQuantileChart = ({
130112
.reverse()
131113
.find((range) => maxValue >= range.threshold);
132114

133-
return unit ? unit.label : '';
115+
return {
116+
valueBase: unit ? unit.threshold || 1 : 1,
117+
unitLabel: unit ? unit.label : '',
118+
};
134119
}, [unitRange, seriesQuantile, yAxisType]);
135120

136121
const timeFormat = useMemo(() => {

ui/src/components/SymmetricalQuantileChart.tsx

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,10 @@ const SymmetricalQuantileChart = ({
100100
),
101101
});
102102

103-
// Calculate unit base (using first series data)
104-
const valueBase = useMemo(() => {
105-
if (!seriesQuantile.above?.length && !seriesQuantile.below?.length)
106-
return 1;
107-
108-
const allSeries = [
109-
...(seriesQuantile.above || []),
110-
...(seriesQuantile.below || []),
111-
];
112-
const allValues = allSeries.flatMap((serie: any) =>
113-
serie.data
114-
.map(([_, value]: [number, any]) =>
115-
typeof value === 'string' ? parseFloat(value) : Math.abs(value),
116-
)
117-
.filter((v: any) => v !== null && !isNaN(v)),
118-
);
119-
120-
const maxValue = Math.max(...allValues);
121-
const unit = UNIT_RANGE_BS.slice()
122-
.reverse()
123-
.find((range: any) => maxValue >= range.threshold);
124-
125-
return unit ? unit.threshold || 1 : 1;
126-
}, [seriesQuantile]);
127-
128-
const unitLabel = useMemo(() => {
129-
if (!seriesQuantile.above?.length && !seriesQuantile.below?.length)
130-
return '';
103+
const { valueBase, unitLabel } = useMemo(() => {
104+
if (!seriesQuantile.above?.length && !seriesQuantile.below?.length) {
105+
return { valueBase: 1, unitLabel: '' };
106+
}
131107

132108
const allSeries = [
133109
...(seriesQuantile.above || []),
@@ -146,7 +122,10 @@ const SymmetricalQuantileChart = ({
146122
.reverse()
147123
.find((range: any) => maxValue >= range.threshold);
148124

149-
return unit ? unit.label : '';
125+
return {
126+
valueBase: unit ? unit.threshold || 1 : 1,
127+
unitLabel: unit ? unit.label : '',
128+
};
150129
}, [seriesQuantile]);
151130

152131
const colorSet = useMemo(() => {

0 commit comments

Comments
 (0)