@codecademy/gamut 68.0.2-alpha.f8c3b5.0 → 68.1.0-alpha.46f13d.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Anchor/index.d.ts +60 -12
- package/dist/Badge/index.d.ts +74 -14
- package/dist/Box/props.d.ts +204 -40
- package/dist/Button/shared/styles.d.ts +190 -38
- package/dist/Card/elements.d.ts +612 -120
- package/dist/ConnectedForm/utils.d.ts +1 -1
- package/dist/Form/SelectDropdown/styles.d.ts +1 -1
- package/dist/Form/elements/Form.d.ts +191 -39
- package/dist/Form/elements/FormGroupLabel.js +2 -2
- package/dist/Form/inputs/Select.js +5 -6
- package/dist/GridForm/GridFormSections/GridFormSectionBreak.d.ts +144 -28
- package/dist/InternalFloatingCard/InternalFloatingCard.d.ts +30 -6
- package/dist/Layout/Column.d.ts +60 -12
- package/dist/Layout/LayoutGrid.d.ts +60 -12
- package/dist/List/elements.d.ts +265 -53
- package/dist/Menu/elements.d.ts +204 -40
- package/dist/Pagination/AnimatedPaginationButtons.d.ts +190 -38
- package/dist/Pagination/utils.d.ts +190 -38
- package/dist/Tabs/props.d.ts +60 -12
- package/dist/Tag/types.d.ts +60 -12
- package/dist/Toggle/elements.d.ts +134 -26
- package/dist/Typography/Text.d.ts +74 -14
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/package.json +7 -7
- package/dist/BarChart/BarChartProvider.d.ts +0 -20
- package/dist/BarChart/BarChartProvider.js +0 -31
- package/dist/BarChart/BarRow/ValueLabelsContent.d.ts +0 -7
- package/dist/BarChart/BarRow/ValueLabelsContent.js +0 -30
- package/dist/BarChart/BarRow/elements.d.ts +0 -719
- package/dist/BarChart/BarRow/elements.js +0 -97
- package/dist/BarChart/BarRow/index.d.ts +0 -26
- package/dist/BarChart/BarRow/index.js +0 -243
- package/dist/BarChart/GENERIC_EXAMPLE.d.ts +0 -14
- package/dist/BarChart/GENERIC_EXAMPLE.js +0 -328
- package/dist/BarChart/index.d.ts +0 -4
- package/dist/BarChart/index.js +0 -155
- package/dist/BarChart/layout/GridLines.d.ts +0 -7
- package/dist/BarChart/layout/GridLines.js +0 -78
- package/dist/BarChart/layout/ScaleChartHeader.d.ts +0 -10
- package/dist/BarChart/layout/ScaleChartHeader.js +0 -89
- package/dist/BarChart/layout/VerticalSpacer.d.ts +0 -6
- package/dist/BarChart/layout/VerticalSpacer.js +0 -56
- package/dist/BarChart/shared/elements.d.ts +0 -7
- package/dist/BarChart/shared/elements.js +0 -12
- package/dist/BarChart/shared/styles.d.ts +0 -4
- package/dist/BarChart/shared/styles.js +0 -4
- package/dist/BarChart/shared/translations.d.ts +0 -68
- package/dist/BarChart/shared/translations.js +0 -52
- package/dist/BarChart/shared/types.d.ts +0 -94
- package/dist/BarChart/shared/types.js +0 -1
- package/dist/BarChart/utils/hooks.d.ts +0 -91
- package/dist/BarChart/utils/hooks.js +0 -291
- package/dist/BarChart/utils/index.d.ts +0 -100
- package/dist/BarChart/utils/index.js +0 -224
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
import { useColorModes } from '@codecademy/gamut-styles';
|
|
2
|
-
import { getContrast } from 'polished';
|
|
3
|
-
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
-
import { BarChartContext, defaultStyleConfig } from '../BarChartProvider';
|
|
5
|
-
import { calculatePositionPercent, getLabel, sortBars } from './index';
|
|
6
|
-
/**
|
|
7
|
-
* Hook that calculates label positions for a given range and count
|
|
8
|
-
* Returns an array of { value, positionPercent } objects
|
|
9
|
-
*/
|
|
10
|
-
export const useLabelPositions = ({
|
|
11
|
-
min,
|
|
12
|
-
max,
|
|
13
|
-
count
|
|
14
|
-
}) => {
|
|
15
|
-
return useMemo(() => Array.from({
|
|
16
|
-
length: count
|
|
17
|
-
}, (_, i) => {
|
|
18
|
-
const value = getLabel({
|
|
19
|
-
labelCount: count,
|
|
20
|
-
labelIndex: i,
|
|
21
|
-
min,
|
|
22
|
-
max
|
|
23
|
-
});
|
|
24
|
-
const positionPercent = calculatePositionPercent({
|
|
25
|
-
value,
|
|
26
|
-
min,
|
|
27
|
-
max
|
|
28
|
-
});
|
|
29
|
-
return {
|
|
30
|
-
value,
|
|
31
|
-
positionPercent
|
|
32
|
-
};
|
|
33
|
-
}), [min, max, count]);
|
|
34
|
-
};
|
|
35
|
-
export const useBarChartContext = () => {
|
|
36
|
-
return useContext(BarChartContext);
|
|
37
|
-
};
|
|
38
|
-
const MIN_RANGE = 0;
|
|
39
|
-
export const useBarChart = ({
|
|
40
|
-
maxRange,
|
|
41
|
-
xScale,
|
|
42
|
-
unit = '',
|
|
43
|
-
styleConfig,
|
|
44
|
-
animate = false,
|
|
45
|
-
barCount = 0,
|
|
46
|
-
translations
|
|
47
|
-
}) => {
|
|
48
|
-
const [widestLeftLabelWidth, setWidestLeftLabelWidthState] = useState(null);
|
|
49
|
-
const [widestRightLabelWidth, setWidestRightLabelWidthState] = useState(null);
|
|
50
|
-
const [isMeasuring, setIsMeasuring] = useState(true);
|
|
51
|
-
const measuredCountRef = useRef(0);
|
|
52
|
-
const maxLeftWidthRef = useRef(0);
|
|
53
|
-
const maxRightWidthRef = useRef(0);
|
|
54
|
-
const setWidestLeftLabelWidth = useCallback(width => {
|
|
55
|
-
if (width > maxLeftWidthRef.current) {
|
|
56
|
-
maxLeftWidthRef.current = width;
|
|
57
|
-
setWidestLeftLabelWidthState(width);
|
|
58
|
-
}
|
|
59
|
-
measuredCountRef.current += 1;
|
|
60
|
-
// Only stop measuring when we've received measurements from all bars
|
|
61
|
-
if (measuredCountRef.current >= barCount * 2 && barCount > 0) {
|
|
62
|
-
setIsMeasuring(false);
|
|
63
|
-
}
|
|
64
|
-
}, [barCount]);
|
|
65
|
-
const setWidestRightLabelWidth = useCallback(width => {
|
|
66
|
-
if (width > maxRightWidthRef.current) {
|
|
67
|
-
maxRightWidthRef.current = width;
|
|
68
|
-
setWidestRightLabelWidthState(width);
|
|
69
|
-
}
|
|
70
|
-
measuredCountRef.current += 1;
|
|
71
|
-
// Only stop measuring when we've received measurements from all bars (left + right)
|
|
72
|
-
if (measuredCountRef.current >= barCount * 2 && barCount > 0) {
|
|
73
|
-
setIsMeasuring(false);
|
|
74
|
-
}
|
|
75
|
-
}, [barCount]);
|
|
76
|
-
useEffect(() => {
|
|
77
|
-
if (barCount > 0) {
|
|
78
|
-
measuredCountRef.current = 0;
|
|
79
|
-
maxLeftWidthRef.current = 0;
|
|
80
|
-
maxRightWidthRef.current = 0;
|
|
81
|
-
setIsMeasuring(true);
|
|
82
|
-
}
|
|
83
|
-
}, [barCount]);
|
|
84
|
-
return useMemo(() => ({
|
|
85
|
-
minRange: MIN_RANGE,
|
|
86
|
-
maxRange,
|
|
87
|
-
xScale: xScale ?? Math.ceil(maxRange / 5),
|
|
88
|
-
unit,
|
|
89
|
-
styleConfig: {
|
|
90
|
-
...defaultStyleConfig,
|
|
91
|
-
...styleConfig
|
|
92
|
-
},
|
|
93
|
-
animate,
|
|
94
|
-
widestLeftLabelWidth,
|
|
95
|
-
setWidestLeftLabelWidth,
|
|
96
|
-
widestRightLabelWidth,
|
|
97
|
-
setWidestRightLabelWidth,
|
|
98
|
-
isMeasuring,
|
|
99
|
-
translations
|
|
100
|
-
}), [maxRange, xScale, unit, styleConfig, animate, widestLeftLabelWidth, setWidestLeftLabelWidth, widestRightLabelWidth, setWidestRightLabelWidth, isMeasuring, translations]);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Checks if a color is a color alias (exists in the color mode shape)
|
|
105
|
-
*/
|
|
106
|
-
const isColorAlias = (mode, color) => Object.keys(mode).includes(color);
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Hook that returns a function to get the highest contrast border color
|
|
110
|
-
* (white or navy-900) for a given background color.
|
|
111
|
-
*
|
|
112
|
-
* Similar to the Background component, this resolves color aliases and
|
|
113
|
-
* compares contrast ratios to determine the best border color.
|
|
114
|
-
*
|
|
115
|
-
* @returns A function that takes a background color and returns either 'white' or 'navy-900'
|
|
116
|
-
*/
|
|
117
|
-
export const useBarBorderColor = () => {
|
|
118
|
-
const [, activeColors,, getColorValue] = useColorModes();
|
|
119
|
-
const getBorderColor = useCallback(bg => {
|
|
120
|
-
/** If a color alias was used then look up the true color key from the active mode */
|
|
121
|
-
const trueColor = isColorAlias(activeColors, bg) ? activeColors[bg] : bg;
|
|
122
|
-
const backgroundColor = getColorValue(trueColor);
|
|
123
|
-
const whiteContrast = getContrast(getColorValue('white'), backgroundColor);
|
|
124
|
-
const navyContrast = getContrast(getColorValue('navy-900'), backgroundColor);
|
|
125
|
-
return whiteContrast > navyContrast ? 'white' : 'navy-900';
|
|
126
|
-
}, [activeColors, getColorValue]);
|
|
127
|
-
return getBorderColor;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Generic hook for measuring element width and reporting to a callback.
|
|
132
|
-
* Used internally by useMeasureLeftLabelWidth and useMeasureRightLabelWidth.
|
|
133
|
-
*/
|
|
134
|
-
const useMeasureWidth = ({
|
|
135
|
-
ref,
|
|
136
|
-
onMeasure,
|
|
137
|
-
isMeasuring
|
|
138
|
-
}) => {
|
|
139
|
-
const hasMeasuredRef = useRef(false);
|
|
140
|
-
|
|
141
|
-
// Reset measurement flag when a new measurement cycle starts
|
|
142
|
-
const prevIsMeasuringRef = useRef(isMeasuring);
|
|
143
|
-
useEffect(() => {
|
|
144
|
-
if (isMeasuring && !prevIsMeasuringRef.current) {
|
|
145
|
-
// New measurement cycle started
|
|
146
|
-
hasMeasuredRef.current = false;
|
|
147
|
-
}
|
|
148
|
-
prevIsMeasuringRef.current = isMeasuring;
|
|
149
|
-
}, [isMeasuring]);
|
|
150
|
-
useLayoutEffect(() => {
|
|
151
|
-
if (!ref.current || hasMeasuredRef.current || !isMeasuring) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const element = ref.current;
|
|
155
|
-
const width = element?.getBoundingClientRect()?.width;
|
|
156
|
-
if (width > 0) {
|
|
157
|
-
onMeasure(width);
|
|
158
|
-
hasMeasuredRef.current = true;
|
|
159
|
-
}
|
|
160
|
-
}, [ref, onMeasure, isMeasuring]);
|
|
161
|
-
};
|
|
162
|
-
export const useMeasureLeftLabelWidth = ({
|
|
163
|
-
ref
|
|
164
|
-
}) => {
|
|
165
|
-
const {
|
|
166
|
-
setWidestLeftLabelWidth,
|
|
167
|
-
isMeasuring
|
|
168
|
-
} = useBarChartContext();
|
|
169
|
-
useMeasureWidth({
|
|
170
|
-
ref,
|
|
171
|
-
onMeasure: setWidestLeftLabelWidth,
|
|
172
|
-
isMeasuring
|
|
173
|
-
});
|
|
174
|
-
};
|
|
175
|
-
export const useMeasureRightLabelWidth = ({
|
|
176
|
-
ref
|
|
177
|
-
}) => {
|
|
178
|
-
const {
|
|
179
|
-
setWidestRightLabelWidth,
|
|
180
|
-
isMeasuring
|
|
181
|
-
} = useBarChartContext();
|
|
182
|
-
useMeasureWidth({
|
|
183
|
-
ref,
|
|
184
|
-
onMeasure: setWidestRightLabelWidth,
|
|
185
|
-
isMeasuring
|
|
186
|
-
});
|
|
187
|
-
};
|
|
188
|
-
/**
|
|
189
|
-
* Hook that manages bar sorting state and provides memoized sorted bars.
|
|
190
|
-
* Supports predefined sort options (via string literals) and custom sort functions.
|
|
191
|
-
* Only returns selectProps if sortFns is provided.
|
|
192
|
-
*/
|
|
193
|
-
export const useBarChartSort = ({
|
|
194
|
-
bars,
|
|
195
|
-
sortFns,
|
|
196
|
-
translations
|
|
197
|
-
}) => {
|
|
198
|
-
// Build options map and custom sort map from sortFns array
|
|
199
|
-
const {
|
|
200
|
-
allSortOptions,
|
|
201
|
-
customSortMap,
|
|
202
|
-
defaultSortValue
|
|
203
|
-
} = useMemo(() => {
|
|
204
|
-
if (!sortFns || sortFns.length === 0) {
|
|
205
|
-
return {
|
|
206
|
-
allSortOptions: null,
|
|
207
|
-
customSortMap: new Map(),
|
|
208
|
-
defaultSortValue: 'none'
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
const options = {};
|
|
212
|
-
const customMap = new Map();
|
|
213
|
-
const availableValues = [];
|
|
214
|
-
sortFns.forEach(item => {
|
|
215
|
-
if (typeof item === 'string') {
|
|
216
|
-
if (item === 'alphabetically') {
|
|
217
|
-
options['label-asc'] = translations.sortOptions.labelAsc;
|
|
218
|
-
options['label-desc'] = translations.sortOptions.labelDesc;
|
|
219
|
-
availableValues.push('label-asc', 'label-desc');
|
|
220
|
-
} else if (item === 'numerically') {
|
|
221
|
-
options['value-asc'] = translations.sortOptions.valueAsc;
|
|
222
|
-
options['value-desc'] = translations.sortOptions.valueDesc;
|
|
223
|
-
availableValues.push('value-asc', 'value-desc');
|
|
224
|
-
} else if (item === 'none') {
|
|
225
|
-
options.none = translations.sortOptions.none;
|
|
226
|
-
availableValues.push('none');
|
|
227
|
-
}
|
|
228
|
-
} else {
|
|
229
|
-
// CustomSortOption
|
|
230
|
-
options[item.value] = item.label;
|
|
231
|
-
customMap.set(item.value, item.sortFn);
|
|
232
|
-
availableValues.push(item.value);
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
// Default to "none" if available, otherwise first option
|
|
237
|
-
const defaultVal = availableValues.includes('none') ? 'none' : availableValues[0] || 'none';
|
|
238
|
-
return {
|
|
239
|
-
allSortOptions: options,
|
|
240
|
-
customSortMap: customMap,
|
|
241
|
-
defaultSortValue: defaultVal
|
|
242
|
-
};
|
|
243
|
-
}, [sortFns, translations]);
|
|
244
|
-
const [sortValue, setSortValue] = useState(defaultSortValue);
|
|
245
|
-
|
|
246
|
-
// Update sortValue when defaultSortValue changes (e.g., when sortFns changes)
|
|
247
|
-
useEffect(() => {
|
|
248
|
-
setSortValue(defaultSortValue);
|
|
249
|
-
}, [defaultSortValue]);
|
|
250
|
-
const sortedBars = useMemo(() => {
|
|
251
|
-
// Check if current selection is a custom sort function
|
|
252
|
-
const customSortFn = customSortMap.get(sortValue);
|
|
253
|
-
if (customSortFn) {
|
|
254
|
-
return customSortFn([...bars]);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Otherwise use predefined sort options
|
|
258
|
-
if (sortValue === 'none') {
|
|
259
|
-
return bars;
|
|
260
|
-
}
|
|
261
|
-
const [sortBy, order] = sortValue.split('-');
|
|
262
|
-
const sortByValue = sortBy;
|
|
263
|
-
const orderValue = order === 'desc' ? 'descending' : 'ascending';
|
|
264
|
-
return sortBars({
|
|
265
|
-
bars: bars,
|
|
266
|
-
sortBy: sortByValue,
|
|
267
|
-
order: orderValue
|
|
268
|
-
});
|
|
269
|
-
}, [bars, sortValue, customSortMap]);
|
|
270
|
-
const onSortChange = useCallback(value => {
|
|
271
|
-
setSortValue(value);
|
|
272
|
-
}, []);
|
|
273
|
-
const selectProps = useMemo(() => {
|
|
274
|
-
if (!allSortOptions) {
|
|
275
|
-
return null;
|
|
276
|
-
}
|
|
277
|
-
return {
|
|
278
|
-
options: allSortOptions,
|
|
279
|
-
value: sortValue,
|
|
280
|
-
onChange: e => {
|
|
281
|
-
onSortChange(e.target.value);
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
}, [sortValue, onSortChange, allSortOptions]);
|
|
285
|
-
return {
|
|
286
|
-
sortedBars,
|
|
287
|
-
sortValue,
|
|
288
|
-
onSortChange,
|
|
289
|
-
selectProps
|
|
290
|
-
};
|
|
291
|
-
};
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { BarChartTranslations } from '../shared/translations';
|
|
2
|
-
import { BarChartRange, BarChartUnit, BarProps } from '../shared/types';
|
|
3
|
-
export declare const numDigits: ({ num }: {
|
|
4
|
-
num: number;
|
|
5
|
-
}) => number;
|
|
6
|
-
export declare const columnBaseSize: ({ experience }: {
|
|
7
|
-
experience?: number | undefined;
|
|
8
|
-
}) => {
|
|
9
|
-
sm: number;
|
|
10
|
-
md: number;
|
|
11
|
-
lg: number;
|
|
12
|
-
xl: number;
|
|
13
|
-
};
|
|
14
|
-
export declare const calculatePercent: ({ value, total, }: {
|
|
15
|
-
value: number;
|
|
16
|
-
total: number;
|
|
17
|
-
}) => number;
|
|
18
|
-
export declare const calculateBarWidth: ({ value, minRange, maxRange, }: {
|
|
19
|
-
value: number;
|
|
20
|
-
} & BarChartRange) => number;
|
|
21
|
-
/**
|
|
22
|
-
* Calculate tick spacing and nice minimum and maximum data points on the axis.
|
|
23
|
-
*/
|
|
24
|
-
export declare const calculateTicksAndRange: ({ maxTicks, min, max, }: {
|
|
25
|
-
maxTicks: number;
|
|
26
|
-
} & {
|
|
27
|
-
min: BarChartRange['minRange'];
|
|
28
|
-
max: BarChartRange['maxRange'];
|
|
29
|
-
}) => [number, number, number];
|
|
30
|
-
/**
|
|
31
|
-
* Returns a "nice" number approximately equal to range
|
|
32
|
-
* Rounds the number if round = true
|
|
33
|
-
* Takes the ceiling if round = false.
|
|
34
|
-
* A nice number is a simple decimal number, for example if a number is 1234, a nice number would be 1000 or 2000.
|
|
35
|
-
*/
|
|
36
|
-
export declare const niceNum: ({ range, roundDown, }: {
|
|
37
|
-
range: number;
|
|
38
|
-
roundDown: boolean;
|
|
39
|
-
}) => number;
|
|
40
|
-
export declare const getPercentDiff: ({ v1, v2 }: {
|
|
41
|
-
v1: number;
|
|
42
|
-
v2: number;
|
|
43
|
-
}) => number;
|
|
44
|
-
export declare const formatNumberUS: ({ num, locale, }: {
|
|
45
|
-
num: number;
|
|
46
|
-
locale?: string | undefined;
|
|
47
|
-
}) => string;
|
|
48
|
-
/**
|
|
49
|
-
* Formats a numeric value with optional unit for bar chart labels.
|
|
50
|
-
*/
|
|
51
|
-
export declare const formatValueWithUnit: ({ value, unit, locale, }: {
|
|
52
|
-
value: number;
|
|
53
|
-
unit: string;
|
|
54
|
-
locale?: string | undefined;
|
|
55
|
-
}) => string;
|
|
56
|
-
export declare const formatNumberUSCompact: ({ num, locale, }: {
|
|
57
|
-
num: number;
|
|
58
|
-
locale?: string | undefined;
|
|
59
|
-
}) => string;
|
|
60
|
-
/**
|
|
61
|
-
* Sort bars based on sortBy and order configuration
|
|
62
|
-
*/
|
|
63
|
-
export declare const sortBars: <T extends BarProps>({ bars, sortBy, order, }: {
|
|
64
|
-
bars: T[];
|
|
65
|
-
sortBy: 'label' | 'value' | 'none';
|
|
66
|
-
order: 'ascending' | 'descending';
|
|
67
|
-
}) => T[];
|
|
68
|
-
/**
|
|
69
|
-
* Generates an accessible summary of the bar values for aria-label / screen reader.
|
|
70
|
-
* When translations.accessibility keys are functions, they are called with scoped
|
|
71
|
-
* context (values, label, unit, locale) and their return value is used as the full summary.
|
|
72
|
-
*/
|
|
73
|
-
export declare const getValuesSummary: ({ isInteractive, seriesOneValue, seriesTwoValue, unit, yLabel, translations, }: Pick<BarProps, "yLabel" | "seriesOneValue" | "seriesTwoValue"> & Required<Pick<BarChartUnit, "unit">> & {
|
|
74
|
-
isInteractive: boolean;
|
|
75
|
-
translations: BarChartTranslations;
|
|
76
|
-
}) => string;
|
|
77
|
-
/**
|
|
78
|
-
* Calculates the value for a given label position
|
|
79
|
-
*/
|
|
80
|
-
export declare const getLabel: ({ labelCount, labelIndex, min, max, }: {
|
|
81
|
-
labelCount: number;
|
|
82
|
-
labelIndex: number;
|
|
83
|
-
} & {
|
|
84
|
-
min: BarChartRange['minRange'];
|
|
85
|
-
max: BarChartRange['maxRange'];
|
|
86
|
-
}) => number;
|
|
87
|
-
/**
|
|
88
|
-
* Calculates the percentage position for a given value within a range
|
|
89
|
-
* Returns a value between 0 and 100 representing the position percentage
|
|
90
|
-
*/
|
|
91
|
-
export declare const calculatePositionPercent: ({ value, min, max, }: {
|
|
92
|
-
value: number;
|
|
93
|
-
} & {
|
|
94
|
-
min: BarChartRange['minRange'];
|
|
95
|
-
max: BarChartRange['maxRange'];
|
|
96
|
-
}) => number;
|
|
97
|
-
/**
|
|
98
|
-
* Generates a stable key for a bar row (for React list keys).
|
|
99
|
-
*/
|
|
100
|
-
export declare const getBarRowKey: (bar: Pick<BarProps, 'yLabel' | 'seriesOneValue' | 'seriesTwoValue'>, index: number) => string;
|
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
export const numDigits = ({
|
|
2
|
-
num
|
|
3
|
-
}) => {
|
|
4
|
-
return Math.max(Math.floor(Math.log10(Math.abs(num))), 0) + 1;
|
|
5
|
-
};
|
|
6
|
-
export const columnBaseSize = ({
|
|
7
|
-
experience = 3
|
|
8
|
-
}) => {
|
|
9
|
-
const digits = numDigits({
|
|
10
|
-
num: experience
|
|
11
|
-
});
|
|
12
|
-
return {
|
|
13
|
-
sm: digits > 4 ? 5 : 4,
|
|
14
|
-
md: digits > 4 ? 5 : 4,
|
|
15
|
-
lg: digits > 4 ? 4 : 5,
|
|
16
|
-
xl: digits > 4 ? 5 : 4
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
export const calculatePercent = ({
|
|
20
|
-
value,
|
|
21
|
-
total
|
|
22
|
-
}) => {
|
|
23
|
-
return value / total * 100;
|
|
24
|
-
};
|
|
25
|
-
export const calculateBarWidth = ({
|
|
26
|
-
value,
|
|
27
|
-
minRange,
|
|
28
|
-
maxRange
|
|
29
|
-
}) => {
|
|
30
|
-
const range = maxRange - minRange;
|
|
31
|
-
const adjustedValue = Math.max(0, Math.min(range, value - minRange));
|
|
32
|
-
return Math.floor(calculatePercent({
|
|
33
|
-
value: adjustedValue,
|
|
34
|
-
total: range
|
|
35
|
-
}));
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Calculate tick spacing and nice minimum and maximum data points on the axis.
|
|
40
|
-
*/
|
|
41
|
-
export const calculateTicksAndRange = ({
|
|
42
|
-
maxTicks,
|
|
43
|
-
min,
|
|
44
|
-
max
|
|
45
|
-
}) => {
|
|
46
|
-
const range = niceNum({
|
|
47
|
-
range: max - min,
|
|
48
|
-
roundDown: false
|
|
49
|
-
});
|
|
50
|
-
const tickSpacing = niceNum({
|
|
51
|
-
range: range / (maxTicks - 1),
|
|
52
|
-
roundDown: true
|
|
53
|
-
});
|
|
54
|
-
const niceMin = Math.floor(min / tickSpacing) * tickSpacing;
|
|
55
|
-
const niceMax = Math.ceil(max / tickSpacing) * tickSpacing;
|
|
56
|
-
const tickCount = range / tickSpacing;
|
|
57
|
-
return [tickCount, niceMin, niceMax];
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Returns a "nice" number approximately equal to range
|
|
62
|
-
* Rounds the number if round = true
|
|
63
|
-
* Takes the ceiling if round = false.
|
|
64
|
-
* A nice number is a simple decimal number, for example if a number is 1234, a nice number would be 1000 or 2000.
|
|
65
|
-
*/
|
|
66
|
-
export const niceNum = ({
|
|
67
|
-
range,
|
|
68
|
-
roundDown
|
|
69
|
-
}) => {
|
|
70
|
-
const exponent = Math.floor(Math.log10(range));
|
|
71
|
-
const fraction = range / 10 ** exponent;
|
|
72
|
-
let niceFraction;
|
|
73
|
-
if (roundDown) {
|
|
74
|
-
if (fraction >= 10) niceFraction = 10;else if (fraction >= 5) niceFraction = 5;else if (fraction >= 2) niceFraction = 2;else if (fraction >= 1) niceFraction = 1;else niceFraction = 1;
|
|
75
|
-
} else if (fraction <= 1) niceFraction = 1;else if (fraction <= 2) niceFraction = 2;else if (fraction <= 5) niceFraction = 5;else niceFraction = 10;
|
|
76
|
-
return niceFraction * 10 ** exponent;
|
|
77
|
-
};
|
|
78
|
-
export const getPercentDiff = ({
|
|
79
|
-
v1,
|
|
80
|
-
v2
|
|
81
|
-
}) => {
|
|
82
|
-
return Math.abs(v1 - v2) / ((v1 + v2) / 2) * 100;
|
|
83
|
-
};
|
|
84
|
-
export const formatNumberUS = ({
|
|
85
|
-
num,
|
|
86
|
-
locale = 'en'
|
|
87
|
-
}) => Intl.NumberFormat(locale).format(num);
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Formats a numeric value with optional unit for bar chart labels.
|
|
91
|
-
*/
|
|
92
|
-
export const formatValueWithUnit = ({
|
|
93
|
-
value,
|
|
94
|
-
unit,
|
|
95
|
-
locale = 'en'
|
|
96
|
-
}) => {
|
|
97
|
-
const formatted = Intl.NumberFormat(locale).format(value);
|
|
98
|
-
return unit ? `${formatted} ${unit}` : formatted;
|
|
99
|
-
};
|
|
100
|
-
export const formatNumberUSCompact = ({
|
|
101
|
-
num,
|
|
102
|
-
locale = 'en'
|
|
103
|
-
}) => Intl.NumberFormat(locale, {
|
|
104
|
-
notation: 'compact',
|
|
105
|
-
compactDisplay: 'short'
|
|
106
|
-
}).format(num);
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Sort bars based on sortBy and order configuration
|
|
110
|
-
*/
|
|
111
|
-
export const sortBars = ({
|
|
112
|
-
bars,
|
|
113
|
-
sortBy,
|
|
114
|
-
order
|
|
115
|
-
}) => {
|
|
116
|
-
if (sortBy === 'none' || !sortBy) {
|
|
117
|
-
return bars;
|
|
118
|
-
}
|
|
119
|
-
const sorted = [...bars].sort((a, b) => {
|
|
120
|
-
if (sortBy === 'label') {
|
|
121
|
-
return a.yLabel.localeCompare(b.yLabel);
|
|
122
|
-
}
|
|
123
|
-
// sortBy === 'value' - use seriesTwoValue if available, otherwise seriesOneValue
|
|
124
|
-
const aValue = a.seriesTwoValue ?? a.seriesOneValue;
|
|
125
|
-
const bValue = b.seriesTwoValue ?? b.seriesOneValue;
|
|
126
|
-
return aValue - bValue;
|
|
127
|
-
});
|
|
128
|
-
return order === 'descending' ? sorted.reverse() : sorted;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Generates an accessible summary of the bar values for aria-label / screen reader.
|
|
133
|
-
* When translations.accessibility keys are functions, they are called with scoped
|
|
134
|
-
* context (values, label, unit, locale) and their return value is used as the full summary.
|
|
135
|
-
*/
|
|
136
|
-
export const getValuesSummary = ({
|
|
137
|
-
isInteractive,
|
|
138
|
-
seriesOneValue,
|
|
139
|
-
seriesTwoValue,
|
|
140
|
-
unit,
|
|
141
|
-
yLabel,
|
|
142
|
-
translations
|
|
143
|
-
}) => {
|
|
144
|
-
const unitText = unit ? ` ${unit}` : '';
|
|
145
|
-
const {
|
|
146
|
-
locale
|
|
147
|
-
} = translations;
|
|
148
|
-
if (seriesTwoValue !== undefined) {
|
|
149
|
-
const {
|
|
150
|
-
gainedNowAt,
|
|
151
|
-
inLabel
|
|
152
|
-
} = translations.accessibility;
|
|
153
|
-
if (typeof gainedNowAt === 'function') {
|
|
154
|
-
return gainedNowAt({
|
|
155
|
-
yLabel,
|
|
156
|
-
seriesOneValue,
|
|
157
|
-
seriesTwoValue,
|
|
158
|
-
unit,
|
|
159
|
-
locale
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
const gained = seriesOneValue;
|
|
163
|
-
return `${gained}${unitText} ${gainedNowAt} ${seriesTwoValue}${unitText} ${inLabel} ${yLabel}`;
|
|
164
|
-
}
|
|
165
|
-
if (isInteractive) {
|
|
166
|
-
const {
|
|
167
|
-
inLabel
|
|
168
|
-
} = translations.accessibility;
|
|
169
|
-
if (typeof inLabel === 'function') {
|
|
170
|
-
return inLabel({
|
|
171
|
-
yLabel,
|
|
172
|
-
value: seriesOneValue,
|
|
173
|
-
unit,
|
|
174
|
-
locale
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
return `${seriesOneValue}${unitText} ${inLabel} ${yLabel}`;
|
|
178
|
-
}
|
|
179
|
-
const {
|
|
180
|
-
inOnly
|
|
181
|
-
} = translations.accessibility;
|
|
182
|
-
if (typeof inOnly === 'function') {
|
|
183
|
-
return inOnly({
|
|
184
|
-
value: seriesOneValue,
|
|
185
|
-
unit,
|
|
186
|
-
locale
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
return `${seriesOneValue}${unitText} ${inOnly}`;
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Calculates the value for a given label position
|
|
194
|
-
*/
|
|
195
|
-
export const getLabel = ({
|
|
196
|
-
labelCount,
|
|
197
|
-
labelIndex,
|
|
198
|
-
min,
|
|
199
|
-
max
|
|
200
|
-
}) => {
|
|
201
|
-
if (labelCount <= 1) return max;
|
|
202
|
-
const incrementalDecimal = labelIndex / (labelCount - 1);
|
|
203
|
-
return Math.floor(min + incrementalDecimal * (max - min));
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Calculates the percentage position for a given value within a range
|
|
208
|
-
* Returns a value between 0 and 100 representing the position percentage
|
|
209
|
-
*/
|
|
210
|
-
export const calculatePositionPercent = ({
|
|
211
|
-
value,
|
|
212
|
-
min,
|
|
213
|
-
max
|
|
214
|
-
}) => {
|
|
215
|
-
if (max === min) return 0;
|
|
216
|
-
const range = max - min;
|
|
217
|
-
const adjustedValue = value - min;
|
|
218
|
-
return adjustedValue / range * 100;
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Generates a stable key for a bar row (for React list keys).
|
|
223
|
-
*/
|
|
224
|
-
export const getBarRowKey = (bar, index) => bar.yLabel && bar.seriesOneValue ? `${bar.yLabel}-${bar.seriesOneValue}-${bar.seriesTwoValue ?? ''}-${index}` : String(index);
|