@codecademy/gamut 67.6.2 → 67.6.3-alpha.45eda4.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.
@@ -0,0 +1,83 @@
1
+ import { BarChartProps, BarProps } from '../types';
2
+ export declare const numDigits: ({ num }: {
3
+ num: number;
4
+ }) => number;
5
+ export declare const columnBaseSize: ({ experience }: {
6
+ experience?: number | undefined;
7
+ }) => {
8
+ sm: number;
9
+ md: number;
10
+ lg: number;
11
+ xl: number;
12
+ };
13
+ export declare const calculatePercent: ({ value, total, }: {
14
+ value: number;
15
+ total: number;
16
+ }) => number;
17
+ export declare const calculateBarWidth: ({ value, minRange, maxRange, }: {
18
+ value: number;
19
+ minRange: number;
20
+ maxRange: number;
21
+ }) => number;
22
+ export declare const calculateTicksAndRange: ({ maxTicks, minPoint, maxPoint, }: {
23
+ maxTicks: number;
24
+ minPoint: number;
25
+ maxPoint: number;
26
+ }) => [number, number, number];
27
+ /**
28
+ * Returns a "nice" number approximately equal to range
29
+ * Rounds the number if round = true
30
+ * Takes the ceiling if round = false.
31
+ * A nice number is a simple decimal number, for example if a number is 1234, a nice number would be 1000 or 2000.
32
+ */
33
+ export declare const niceNum: ({ range, roundDown, }: {
34
+ range: number;
35
+ roundDown: boolean;
36
+ }) => number;
37
+ export declare const getPercentDiff: ({ v1, v2 }: {
38
+ v1: number;
39
+ v2: number;
40
+ }) => number;
41
+ export declare const formatNumberUS: ({ num }: {
42
+ num: number;
43
+ }) => string;
44
+ export declare const formatNumberUSCompact: ({ num }: {
45
+ num: number;
46
+ }) => string;
47
+ /**
48
+ * Sort bars based on sortBy and order configuration
49
+ */
50
+ export declare const sortBars: ({ bars, sortBy, order, }: {
51
+ bars: BarProps[];
52
+ sortBy: BarChartProps['sortBy'];
53
+ order: BarChartProps['order'];
54
+ }) => BarProps[];
55
+ /**
56
+ * Generates an accessible summary of the bar values
57
+ */
58
+ export declare const getValuesSummary: ({ yLabel, seriesOneValue, seriesTwoValue, unit, }: {
59
+ yLabel: string;
60
+ seriesOneValue: number;
61
+ seriesTwoValue?: number | undefined;
62
+ unit: string;
63
+ }) => string;
64
+ /**
65
+ * Calculates the value for a given label position
66
+ */
67
+ export declare const getLabel: ({ labelCount, labelIndex, min, max, }: {
68
+ labelCount: number;
69
+ labelIndex: number;
70
+ min: number;
71
+ max: number;
72
+ }) => number;
73
+ /**
74
+ * Calculates the percentage position for a given value within a range
75
+ * Returns a value between 0 and 100 representing the position percentage
76
+ */
77
+ export declare const calculatePositionPercent: ({ value, min, max, }: {
78
+ value: number;
79
+ min: number;
80
+ max: number;
81
+ }) => number;
82
+ export { useBarChart, useBarChartContext, useLabelPositions } from './hooks';
83
+ export type { LabelPosition, UseBarChartOptions } from './hooks';
@@ -0,0 +1,162 @@
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 = value - minRange;
32
+ return Math.floor(calculatePercent({
33
+ value: adjustedValue,
34
+ total: range
35
+ }));
36
+ };
37
+
38
+ // Calculate tick spacing and nice minimum and maximum data points on the axis.
39
+
40
+ export const calculateTicksAndRange = ({
41
+ maxTicks,
42
+ minPoint,
43
+ maxPoint
44
+ }) => {
45
+ const range = niceNum({
46
+ range: maxPoint - minPoint,
47
+ roundDown: false
48
+ });
49
+ const tickSpacing = niceNum({
50
+ range: range / (maxTicks - 1),
51
+ roundDown: true
52
+ });
53
+ const niceMin = Math.floor(minPoint / tickSpacing) * tickSpacing;
54
+ const niceMax = Math.ceil(maxPoint / tickSpacing) * tickSpacing;
55
+ const tickCount = range / tickSpacing;
56
+ return [tickCount, niceMin, niceMax];
57
+ };
58
+
59
+ /**
60
+ * Returns a "nice" number approximately equal to range
61
+ * Rounds the number if round = true
62
+ * Takes the ceiling if round = false.
63
+ * A nice number is a simple decimal number, for example if a number is 1234, a nice number would be 1000 or 2000.
64
+ */
65
+ export const niceNum = ({
66
+ range,
67
+ roundDown
68
+ }) => {
69
+ const exponent = Math.floor(Math.log10(range));
70
+ const fraction = range / 10 ** exponent;
71
+ let niceFraction;
72
+ if (roundDown) {
73
+ if (fraction < 1.5) niceFraction = 1;else if (fraction < 3) niceFraction = 2;else if (fraction < 7) niceFraction = 5;else niceFraction = 10;
74
+ } else if (fraction <= 1) niceFraction = 1;else if (fraction <= 2) niceFraction = 2;else if (fraction <= 5) niceFraction = 5;else niceFraction = 10;
75
+ return niceFraction * 10 ** exponent;
76
+ };
77
+ export const getPercentDiff = ({
78
+ v1,
79
+ v2
80
+ }) => {
81
+ return Math.abs(v1 - v2) / ((v1 + v2) / 2) * 100;
82
+ };
83
+ export const formatNumberUS = ({
84
+ num
85
+ }) => Intl.NumberFormat('en').format(num);
86
+ export const formatNumberUSCompact = ({
87
+ num
88
+ }) => Intl.NumberFormat('en', {
89
+ notation: 'compact',
90
+ compactDisplay: 'short'
91
+ }).format(num);
92
+
93
+ /**
94
+ * Sort bars based on sortBy and order configuration
95
+ */
96
+ export const sortBars = ({
97
+ bars,
98
+ sortBy,
99
+ order
100
+ }) => {
101
+ if (sortBy === 'none' || !sortBy) {
102
+ return bars;
103
+ }
104
+ const sorted = [...bars].sort((a, b) => {
105
+ if (sortBy === 'label') {
106
+ return a.yLabel.localeCompare(b.yLabel);
107
+ }
108
+ // sortBy === 'value' - use seriesTwoValue if available, otherwise seriesOneValue
109
+ const aValue = a.seriesTwoValue ?? a.seriesOneValue;
110
+ const bValue = b.seriesTwoValue ?? b.seriesOneValue;
111
+ return aValue - bValue;
112
+ });
113
+ return order === 'descending' ? sorted.reverse() : sorted;
114
+ };
115
+
116
+ /**
117
+ * Generates an accessible summary of the bar values
118
+ */
119
+ export const getValuesSummary = ({
120
+ yLabel,
121
+ seriesOneValue,
122
+ seriesTwoValue,
123
+ unit
124
+ }) => {
125
+ if (seriesTwoValue !== undefined) {
126
+ const gained = seriesOneValue;
127
+ return `${gained} ${unit} gained - now at ${seriesTwoValue} ${unit} in ${yLabel} category`;
128
+ }
129
+ return `${seriesOneValue} ${unit} in ${yLabel} category`;
130
+ };
131
+
132
+ /**
133
+ * Calculates the value for a given label position
134
+ */
135
+ export const getLabel = ({
136
+ labelCount,
137
+ labelIndex,
138
+ min,
139
+ max
140
+ }) => {
141
+ if (labelCount <= 1) return max;
142
+ const incrementalDecimal = labelIndex / (labelCount - 1);
143
+ return Math.floor(min + incrementalDecimal * (max - min));
144
+ };
145
+
146
+ /**
147
+ * Calculates the percentage position for a given value within a range
148
+ * Returns a value between 0 and 100 representing the position percentage
149
+ */
150
+ export const calculatePositionPercent = ({
151
+ value,
152
+ min,
153
+ max
154
+ }) => {
155
+ if (max === min) return 0;
156
+ const range = max - min;
157
+ const adjustedValue = value - min;
158
+ return adjustedValue / range * 100;
159
+ };
160
+
161
+ // Re-export hooks
162
+ export { useBarChart, useBarChartContext, useLabelPositions } from './hooks';
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from './Anchor';
6
6
  export * from './Animation';
7
7
  export * from './AppWrapper';
8
8
  export * from './Badge';
9
+ export * from './BarChart';
9
10
  export * from './BodyPortal';
10
11
  export * from './Box';
11
12
  export * from './Breadcrumbs';
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export * from './Anchor';
6
6
  export * from './Animation';
7
7
  export * from './AppWrapper';
8
8
  export * from './Badge';
9
+ export * from './BarChart';
9
10
  export * from './BodyPortal';
10
11
  export * from './Box';
11
12
  export * from './Breadcrumbs';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/gamut",
3
3
  "description": "Styleguide & Component library for Codecademy",
4
- "version": "67.6.2",
4
+ "version": "67.6.3-alpha.45eda4.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "dependencies": {
7
7
  "@codecademy/gamut-icons": "9.54.0",
@@ -56,5 +56,5 @@
56
56
  "dist/**/[A-Z]**/[A-Z]*.js",
57
57
  "dist/**/[A-Z]**/index.js"
58
58
  ],
59
- "gitHead": "598e43114b75437e1b5db66ad6a416e532780abe"
59
+ "gitHead": "a36d41279895ba26cf87ad6a6d759d593a9b74ad"
60
60
  }