@gravity-ui/charts 1.46.1 → 1.47.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.
Files changed (149) hide show
  1. package/dist/cjs/components/ChartInner/index.js +1 -0
  2. package/dist/cjs/components/ChartInner/useChartInnerProps.d.ts +1 -0
  3. package/dist/cjs/components/ChartInner/useChartInnerProps.js +12 -5
  4. package/dist/cjs/core/brush/index.d.ts +2 -0
  5. package/dist/cjs/core/brush/index.js +2 -0
  6. package/dist/cjs/{hooks/useBrush → core/brush}/types.d.ts +2 -2
  7. package/dist/{esm/hooks/useBrush → cjs/core/brush}/utils.d.ts +1 -1
  8. package/dist/cjs/core/chart/index.d.ts +1 -0
  9. package/dist/cjs/core/chart/index.js +1 -0
  10. package/dist/cjs/core/chart/types.d.ts +8 -0
  11. package/dist/cjs/core/index.d.ts +3 -0
  12. package/dist/cjs/core/index.js +3 -0
  13. package/dist/cjs/core/layout/chart-dimensions.d.ts +1 -1
  14. package/dist/cjs/core/range-slider/index.d.ts +2 -0
  15. package/dist/cjs/core/range-slider/index.js +2 -0
  16. package/dist/cjs/core/range-slider/types.d.ts +4 -0
  17. package/dist/cjs/{hooks/useRangeSlider → core/range-slider}/utils.d.ts +5 -5
  18. package/dist/cjs/{hooks/useRangeSlider → core/range-slider}/utils.js +1 -1
  19. package/dist/cjs/core/scales/x-scale.d.ts +2 -2
  20. package/dist/cjs/core/scales/y-scale.js +21 -0
  21. package/dist/cjs/core/series/prepare-legend.d.ts +1 -1
  22. package/dist/cjs/core/shapes/area/prepare-data.js +6 -1
  23. package/dist/cjs/core/shapes/area/renderer.js +8 -14
  24. package/dist/cjs/core/shapes/area/types.d.ts +6 -5
  25. package/dist/cjs/core/shapes/bar-x/renderer.js +6 -12
  26. package/dist/cjs/core/shapes/bar-y/renderer.js +6 -12
  27. package/dist/cjs/core/shapes/data-labels.d.ts +15 -0
  28. package/dist/cjs/core/shapes/data-labels.js +15 -0
  29. package/dist/cjs/core/shapes/funnel/renderer.js +6 -11
  30. package/dist/cjs/core/shapes/heatmap/prepare-data.js +1 -0
  31. package/dist/cjs/core/shapes/heatmap/renderer.js +6 -11
  32. package/dist/cjs/core/shapes/heatmap/types.d.ts +1 -0
  33. package/dist/cjs/core/shapes/line/prepare-data.js +9 -1
  34. package/dist/cjs/core/shapes/line/renderer.js +7 -13
  35. package/dist/cjs/core/shapes/line/types.d.ts +5 -4
  36. package/dist/cjs/core/shapes/radar/renderer.js +8 -12
  37. package/dist/cjs/core/shapes/sankey/renderer.js +6 -12
  38. package/dist/cjs/core/shapes/utils.d.ts +24 -0
  39. package/dist/cjs/core/shapes/utils.js +48 -0
  40. package/dist/cjs/core/shapes/waterfall/renderer.js +6 -12
  41. package/dist/cjs/core/shapes/x-range/renderer.js +7 -13
  42. package/dist/cjs/core/types/chart/base.d.ts +17 -3
  43. package/dist/cjs/core/types/chart/tooltip.d.ts +3 -3
  44. package/dist/cjs/core/types/formatter.d.ts +1 -40
  45. package/dist/cjs/core/utils/format.d.ts +2 -2
  46. package/dist/cjs/core/utils/get-closest-data.js +13 -8
  47. package/dist/cjs/core/zoom/index.d.ts +2 -0
  48. package/dist/cjs/core/zoom/index.js +2 -0
  49. package/dist/{esm/hooks/useZoom → cjs/core/zoom}/utils.d.ts +3 -3
  50. package/dist/{esm/hooks/useZoom → cjs/core/zoom}/utils.js +1 -1
  51. package/dist/cjs/core/zoom/zoom.d.ts +3 -3
  52. package/dist/cjs/hooks/index.d.ts +2 -2
  53. package/dist/cjs/hooks/index.js +2 -2
  54. package/dist/cjs/hooks/types.d.ts +2 -8
  55. package/dist/cjs/hooks/useBrush/index.d.ts +1 -1
  56. package/dist/cjs/hooks/useBrush/index.js +1 -1
  57. package/dist/cjs/hooks/useRangeSlider/index.js +3 -3
  58. package/dist/cjs/hooks/useRangeSlider/types.d.ts +5 -7
  59. package/dist/cjs/hooks/useShapes/index.d.ts +1 -1
  60. package/dist/cjs/hooks/useShapes/utils.d.ts +1 -1
  61. package/dist/cjs/hooks/useZoom/index.d.ts +1 -1
  62. package/dist/cjs/hooks/useZoom/index.js +1 -1
  63. package/dist/cjs/index.d.ts +1 -0
  64. package/dist/cjs/index.js +1 -0
  65. package/dist/cjs/libs/format-number/index.js +82 -14
  66. package/dist/cjs/libs/format-number/presets.d.ts +40 -0
  67. package/dist/cjs/libs/format-number/presets.js +66 -0
  68. package/dist/cjs/libs/format-number/types.d.ts +82 -3
  69. package/dist/esm/components/ChartInner/index.js +1 -0
  70. package/dist/esm/components/ChartInner/useChartInnerProps.d.ts +1 -0
  71. package/dist/esm/components/ChartInner/useChartInnerProps.js +12 -5
  72. package/dist/esm/core/brush/index.d.ts +2 -0
  73. package/dist/esm/core/brush/index.js +2 -0
  74. package/dist/esm/{hooks/useBrush → core/brush}/types.d.ts +2 -2
  75. package/dist/esm/core/brush/types.js +1 -0
  76. package/dist/{cjs/hooks/useBrush → esm/core/brush}/utils.d.ts +1 -1
  77. package/dist/esm/core/chart/index.d.ts +1 -0
  78. package/dist/esm/core/chart/index.js +1 -0
  79. package/dist/esm/core/chart/types.d.ts +8 -0
  80. package/dist/esm/core/chart/types.js +1 -0
  81. package/dist/esm/core/index.d.ts +3 -0
  82. package/dist/esm/core/index.js +3 -0
  83. package/dist/esm/core/layout/chart-dimensions.d.ts +1 -1
  84. package/dist/esm/core/range-slider/index.d.ts +2 -0
  85. package/dist/esm/core/range-slider/index.js +2 -0
  86. package/dist/esm/core/range-slider/types.d.ts +4 -0
  87. package/dist/esm/core/range-slider/types.js +1 -0
  88. package/dist/esm/{hooks/useRangeSlider → core/range-slider}/utils.d.ts +5 -5
  89. package/dist/esm/{hooks/useRangeSlider → core/range-slider}/utils.js +1 -1
  90. package/dist/esm/core/scales/x-scale.d.ts +2 -2
  91. package/dist/esm/core/scales/y-scale.js +21 -0
  92. package/dist/esm/core/series/prepare-legend.d.ts +1 -1
  93. package/dist/esm/core/shapes/area/prepare-data.js +6 -1
  94. package/dist/esm/core/shapes/area/renderer.js +8 -14
  95. package/dist/esm/core/shapes/area/types.d.ts +6 -5
  96. package/dist/esm/core/shapes/bar-x/renderer.js +6 -12
  97. package/dist/esm/core/shapes/bar-y/renderer.js +6 -12
  98. package/dist/esm/core/shapes/data-labels.d.ts +15 -0
  99. package/dist/esm/core/shapes/data-labels.js +15 -0
  100. package/dist/esm/core/shapes/funnel/renderer.js +6 -11
  101. package/dist/esm/core/shapes/heatmap/prepare-data.js +1 -0
  102. package/dist/esm/core/shapes/heatmap/renderer.js +6 -11
  103. package/dist/esm/core/shapes/heatmap/types.d.ts +1 -0
  104. package/dist/esm/core/shapes/line/prepare-data.js +9 -1
  105. package/dist/esm/core/shapes/line/renderer.js +7 -13
  106. package/dist/esm/core/shapes/line/types.d.ts +5 -4
  107. package/dist/esm/core/shapes/radar/renderer.js +8 -12
  108. package/dist/esm/core/shapes/sankey/renderer.js +6 -12
  109. package/dist/esm/core/shapes/utils.d.ts +24 -0
  110. package/dist/esm/core/shapes/utils.js +48 -0
  111. package/dist/esm/core/shapes/waterfall/renderer.js +6 -12
  112. package/dist/esm/core/shapes/x-range/renderer.js +7 -13
  113. package/dist/esm/core/types/chart/base.d.ts +17 -3
  114. package/dist/esm/core/types/chart/tooltip.d.ts +3 -3
  115. package/dist/esm/core/types/formatter.d.ts +1 -40
  116. package/dist/esm/core/utils/format.d.ts +2 -2
  117. package/dist/esm/core/utils/get-closest-data.js +13 -8
  118. package/dist/esm/core/zoom/index.d.ts +2 -0
  119. package/dist/esm/core/zoom/index.js +2 -0
  120. package/dist/esm/core/zoom/types.js +1 -0
  121. package/dist/{cjs/hooks/useZoom → esm/core/zoom}/utils.d.ts +3 -3
  122. package/dist/{cjs/hooks/useZoom → esm/core/zoom}/utils.js +1 -1
  123. package/dist/esm/core/zoom/zoom.d.ts +3 -3
  124. package/dist/esm/hooks/index.d.ts +2 -2
  125. package/dist/esm/hooks/index.js +2 -2
  126. package/dist/esm/hooks/types.d.ts +2 -8
  127. package/dist/esm/hooks/useBrush/index.d.ts +1 -1
  128. package/dist/esm/hooks/useBrush/index.js +1 -1
  129. package/dist/esm/hooks/useRangeSlider/index.js +3 -3
  130. package/dist/esm/hooks/useRangeSlider/types.d.ts +5 -7
  131. package/dist/esm/hooks/useShapes/index.d.ts +1 -1
  132. package/dist/esm/hooks/useShapes/utils.d.ts +1 -1
  133. package/dist/esm/hooks/useZoom/index.d.ts +1 -1
  134. package/dist/esm/hooks/useZoom/index.js +1 -1
  135. package/dist/esm/index.d.ts +1 -0
  136. package/dist/esm/index.js +1 -0
  137. package/dist/esm/libs/format-number/index.js +82 -14
  138. package/dist/esm/libs/format-number/presets.d.ts +40 -0
  139. package/dist/esm/libs/format-number/presets.js +66 -0
  140. package/dist/esm/libs/format-number/types.d.ts +82 -3
  141. package/package.json +1 -1
  142. /package/dist/cjs/{hooks/useBrush → core/brush}/types.js +0 -0
  143. /package/dist/cjs/{hooks/useBrush → core/brush}/utils.js +0 -0
  144. /package/dist/cjs/{hooks/useZoom → core/chart}/types.js +0 -0
  145. /package/dist/{esm/hooks/useBrush → cjs/core/range-slider}/types.js +0 -0
  146. /package/dist/cjs/{hooks/useZoom → core/zoom}/types.d.ts +0 -0
  147. /package/dist/{esm/hooks/useZoom → cjs/core/zoom}/types.js +0 -0
  148. /package/dist/esm/{hooks/useBrush → core/brush}/utils.js +0 -0
  149. /package/dist/esm/{hooks/useZoom → core/zoom}/types.d.ts +0 -0
@@ -11,26 +11,30 @@ function getUnitRate(value, exponent, unitsI18nKeys) {
11
11
  }
12
12
  return resultUnitRate - 1;
13
13
  }
14
+ const formatScaledNumber = (value, options) => {
15
+ const { precision, showRankDelimiter = true, lang } = options;
16
+ let result = value;
17
+ if (typeof precision === 'number') {
18
+ result = Number(result.toFixed(precision));
19
+ }
20
+ else if (precision === 'auto' && result % 1 !== 0) {
21
+ result = Number(result.toFixed(Math.abs(result) > 1 ? 2 : 4));
22
+ }
23
+ return new Intl.NumberFormat(lang !== null && lang !== void 0 ? lang : i18nInstance.lang, {
24
+ minimumFractionDigits: typeof precision === 'number' ? precision : 0,
25
+ maximumFractionDigits: 20,
26
+ useGrouping: showRankDelimiter,
27
+ }).format(result);
28
+ };
14
29
  const unitFormatter = ({ exponent, unitsI18nKeys, unitDelimiterI18nKey, }) => {
15
30
  return function formatUnit(value, options = {}) {
16
- const { precision, unitRate, showRankDelimiter = true, lang } = options;
31
+ const { unitRate, lang } = options;
17
32
  const i18nLang = i18nInstance.lang;
18
33
  if (lang) {
19
34
  i18nInstance.setLang(lang);
20
35
  }
21
36
  const resultUnitRate = typeof unitRate === 'number' ? unitRate : getUnitRate(value, exponent, unitsI18nKeys);
22
- let result = value / Math.pow(exponent, resultUnitRate);
23
- if (typeof precision === 'number') {
24
- result = Number(result.toFixed(precision));
25
- }
26
- else if (precision === 'auto' && result % 1 !== 0) {
27
- result = Number(result.toFixed(Math.abs(result) > 1 ? 2 : 4));
28
- }
29
- result = new Intl.NumberFormat(lang !== null && lang !== void 0 ? lang : i18nLang, {
30
- minimumFractionDigits: typeof precision === 'number' ? precision : 0,
31
- maximumFractionDigits: 20,
32
- useGrouping: showRankDelimiter,
33
- }).format(result);
37
+ const result = formatScaledNumber(value / Math.pow(exponent, resultUnitRate), options);
34
38
  const unit = i18n(unitsI18nKeys[resultUnitRate]);
35
39
  const delimiter = i18n(unitDelimiterI18nKey);
36
40
  i18nInstance.setLang(i18nLang);
@@ -59,6 +63,65 @@ const baseFormatNumber = unitFormatter({
59
63
  unitDelimiterI18nKey: 'value_number-delimiter',
60
64
  unitsI18nKeys: BASE_NUMBER_FORMAT_UNIT_KEYS,
61
65
  });
66
+ const resolvePostfix = (postfix, lang) => {
67
+ var _a, _b;
68
+ if (typeof postfix === 'string') {
69
+ return postfix;
70
+ }
71
+ return (_b = (_a = postfix[lang]) !== null && _a !== void 0 ? _a : postfix.en) !== null && _b !== void 0 ? _b : '';
72
+ };
73
+ const FALLBACK_UNIT_ENTRY = { factor: 1, postfix: '' };
74
+ const normalizedUnitsCache = new WeakMap();
75
+ const normalizeUnits = (units) => {
76
+ const cached = normalizedUnitsCache.get(units);
77
+ if (cached) {
78
+ return cached;
79
+ }
80
+ const { scale } = units;
81
+ const rawEntries = Array.isArray(scale)
82
+ ? scale.map((e) => ({ factor: e.factor, postfix: e.postfix }))
83
+ : scale.postfixes.map((postfix, i) => ({ factor: Math.pow(scale.base, i), postfix }));
84
+ const valid = rawEntries.filter((e) => Number.isFinite(e.factor) && e.factor > 0);
85
+ valid.sort((a, b) => a.factor - b.factor);
86
+ const deduped = [];
87
+ for (const e of valid) {
88
+ if (deduped.length === 0 || deduped[deduped.length - 1].factor !== e.factor) {
89
+ deduped.push(e);
90
+ }
91
+ }
92
+ const result = deduped.length > 0 ? deduped : [FALLBACK_UNIT_ENTRY];
93
+ normalizedUnitsCache.set(units, result);
94
+ return result;
95
+ };
96
+ const pickUnitEntry = (value, scale) => {
97
+ const av = Math.abs(value);
98
+ let chosen = scale[0];
99
+ for (const e of scale) {
100
+ if (av / e.factor >= 1) {
101
+ chosen = e;
102
+ }
103
+ else {
104
+ break;
105
+ }
106
+ }
107
+ return chosen;
108
+ };
109
+ const customUnitFormatter = (value, units, options) => {
110
+ const { lang } = options;
111
+ const i18nLang = i18nInstance.lang;
112
+ if (lang) {
113
+ i18nInstance.setLang(lang);
114
+ }
115
+ const normalizedScale = normalizeUnits(units);
116
+ const entry = pickUnitEntry(value, normalizedScale);
117
+ const result = formatScaledNumber(value / entry.factor, options);
118
+ const effectiveLang = lang !== null && lang !== void 0 ? lang : i18nLang;
119
+ const resolvedPostfix = resolvePostfix(entry.postfix, effectiveLang);
120
+ const resolvedDelimiter = typeof units.delimiter === 'string' ? units.delimiter : ' ';
121
+ const delimiter = resolvedPostfix ? resolvedDelimiter : '';
122
+ i18nInstance.setLang(i18nLang);
123
+ return `${result}${delimiter}${resolvedPostfix}`;
124
+ };
62
125
  const NUMBER_UNIT_RATE_BY_UNIT = {
63
126
  default: 0,
64
127
  auto: undefined,
@@ -77,7 +140,7 @@ export const formatNumber = (value, options = {}) => {
77
140
  if (Number.isNaN(value) || Number.isNaN(Number(value))) {
78
141
  return new Intl.NumberFormat('en').format(Number(value));
79
142
  }
80
- const { format = 'number', multiplier = 1, prefix = '', postfix = '', unit, labelMode } = options;
143
+ const { format = 'number', multiplier = 1, prefix = '', postfix = '', unit, units, labelMode, } = options;
81
144
  let changedMultiplier = multiplier;
82
145
  let prePostfix = '';
83
146
  if (format === 'percent') {
@@ -87,6 +150,11 @@ export const formatNumber = (value, options = {}) => {
87
150
  if (labelMode === 'percent') {
88
151
  prePostfix = '%';
89
152
  }
153
+ if (units) {
154
+ const unitScale = 'scale' in units ? units : { scale: [units] };
155
+ const formattedValue = customUnitFormatter(Number(value) * changedMultiplier, unitScale, options);
156
+ return `${prefix}${formattedValue}${prePostfix}${postfix}`;
157
+ }
90
158
  const formattedValue = baseFormatNumber(Number(value) * changedMultiplier, Object.assign(Object.assign({}, options), { unitRate: NUMBER_UNIT_RATE_BY_UNIT[unit !== null && unit !== void 0 ? unit : 'default'] }));
91
159
  return `${prefix}${formattedValue}${prePostfix}${postfix}`;
92
160
  };
@@ -0,0 +1,40 @@
1
+ import type { FormatUnitScale } from './types';
2
+ /**
3
+ * Byte scale with binary base (`1024`) and localized postfixes.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * tooltip: {
8
+ * valueFormat: {type: 'number', units: FORMAT_UNITS_BYTES, precision: 1},
9
+ * }
10
+ * ```
11
+ */
12
+ export declare const FORMAT_UNITS_BYTES: FormatUnitScale;
13
+ /**
14
+ * Short number scale with decimal base (`1000`). Renders large numbers
15
+ * compactly as `K/M/B/T`. Postfixes are plain Latin letters and stay the
16
+ * same regardless of language. The first entry has an empty postfix, so
17
+ * values below `1000` render as plain numbers without a delimiter.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * tooltip: {
22
+ * valueFormat: {type: 'number', units: FORMAT_UNITS_NUMBERS, precision: 1},
23
+ * }
24
+ * // 300 → "300"
25
+ * // 1_500 → "1.5 K"
26
+ * // 1_500_000 → "1.5 M"
27
+ * ```
28
+ */
29
+ export declare const FORMAT_UNITS_NUMBERS: FormatUnitScale;
30
+ /**
31
+ * Bit scale with decimal base (`1000`) and localized postfixes.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * tooltip: {
36
+ * valueFormat: {type: 'number', units: FORMAT_UNITS_BITS, precision: 1},
37
+ * }
38
+ * ```
39
+ */
40
+ export declare const FORMAT_UNITS_BITS: FormatUnitScale;
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Byte scale with binary base (`1024`) and localized postfixes.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * tooltip: {
7
+ * valueFormat: {type: 'number', units: FORMAT_UNITS_BYTES, precision: 1},
8
+ * }
9
+ * ```
10
+ */
11
+ export const FORMAT_UNITS_BYTES = {
12
+ scale: {
13
+ base: 1024,
14
+ postfixes: [
15
+ { en: 'B', ru: 'Б' },
16
+ { en: 'KB', ru: 'КБ' },
17
+ { en: 'MB', ru: 'МБ' },
18
+ { en: 'GB', ru: 'ГБ' },
19
+ { en: 'TB', ru: 'ТБ' },
20
+ ],
21
+ },
22
+ };
23
+ /**
24
+ * Short number scale with decimal base (`1000`). Renders large numbers
25
+ * compactly as `K/M/B/T`. Postfixes are plain Latin letters and stay the
26
+ * same regardless of language. The first entry has an empty postfix, so
27
+ * values below `1000` render as plain numbers without a delimiter.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * tooltip: {
32
+ * valueFormat: {type: 'number', units: FORMAT_UNITS_NUMBERS, precision: 1},
33
+ * }
34
+ * // 300 → "300"
35
+ * // 1_500 → "1.5 K"
36
+ * // 1_500_000 → "1.5 M"
37
+ * ```
38
+ */
39
+ export const FORMAT_UNITS_NUMBERS = {
40
+ scale: {
41
+ base: 1000,
42
+ postfixes: ['', 'K', 'M', 'B', 'T'],
43
+ },
44
+ };
45
+ /**
46
+ * Bit scale with decimal base (`1000`) and localized postfixes.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * tooltip: {
51
+ * valueFormat: {type: 'number', units: FORMAT_UNITS_BITS, precision: 1},
52
+ * }
53
+ * ```
54
+ */
55
+ export const FORMAT_UNITS_BITS = {
56
+ scale: {
57
+ base: 1000,
58
+ postfixes: [
59
+ { en: 'bit', ru: 'бит' },
60
+ { en: 'Kbit', ru: 'Кбит' },
61
+ { en: 'Mbit', ru: 'Мбит' },
62
+ { en: 'Gbit', ru: 'Гбит' },
63
+ { en: 'Tbit', ru: 'Тбит' },
64
+ ],
65
+ },
66
+ };
@@ -1,13 +1,92 @@
1
- export type FormatOptions = {
1
+ export interface FormatOptions {
2
+ /**
3
+ * Number of decimal places to display.
4
+ * Use `'auto'` to determine precision automatically based on the value magnitude.
5
+ */
2
6
  precision?: number | 'auto';
7
+ /** When `true`, inserts a thousands separator (e.g. `1 500 000`). */
3
8
  showRankDelimiter?: boolean;
9
+ /**
10
+ * BCP 47 language tag used for locale-aware formatting (e.g. `'en'`, `'ru'`).
11
+ * Defaults to the application locale when omitted.
12
+ */
4
13
  lang?: string;
14
+ /** Internal rendering hint for axis label layout. Not intended for public use. */
5
15
  labelMode?: string;
16
+ }
17
+ /**
18
+ * Plain string or a per-language dictionary of strings.
19
+ * When a dictionary is used, the lookup chain is:
20
+ * current `lang` → `en` → empty string.
21
+ */
22
+ export type FormatI18nString = string | Partial<Record<string, string>>;
23
+ /** Single entry of a custom unit scale. */
24
+ export type FormatUnitScaleEntry = {
25
+ /** Positive multiplier applied to the value. The entry wins when `|value| / factor >= 1`. */
26
+ factor: number;
27
+ /**
28
+ * String appended after the scaled value (e.g. `'KB'`).
29
+ * Use an empty string to suppress the suffix and the delimiter.
30
+ * Pass a `{lang: string}` dictionary to localize the postfix — resolution falls back to `en`, then to empty.
31
+ */
32
+ postfix: FormatI18nString;
6
33
  };
7
- export type FormatNumberOptions = FormatOptions & {
34
+ /**
35
+ * Custom unit scale for numeric formatting.
36
+ *
37
+ * `scale` describes the unit table in one of two forms:
38
+ * - `{base, postfixes}` — geometric progression: `postfixes[i]` is bound to `factor = base^i` (e.g. `{base: 1024, postfixes: ['B','KB','MB']}`).
39
+ * - `FormatUnitScaleEntry[]` — arbitrary factors; required for non-linear scales (e.g. `[{factor:1,postfix:'s'},{factor:60,postfix:'min'},{factor:3600,postfix:'h'}]`).
40
+ *
41
+ * `delimiter` is the shared string placed between the scaled value and the
42
+ * postfix. Defaults to a locale-aware space; pass `''` to suppress it.
43
+ *
44
+ * The normalized scale is cached by object identity, so for best performance
45
+ * reuse the same `units` object across `formatNumber` calls instead of
46
+ * creating a new inline literal on every render.
47
+ */
48
+ export type FormatUnitScale = {
49
+ scale: {
50
+ base: number;
51
+ postfixes: FormatI18nString[];
52
+ } | FormatUnitScaleEntry[];
53
+ delimiter?: string;
54
+ };
55
+ export interface FormatNumberOptions extends FormatOptions {
56
+ /**
57
+ * Display mode for the numeric value.
58
+ * - `'number'` — plain number (default).
59
+ * - `'percent'` — value is multiplied by 100 and rendered with a `%` suffix.
60
+ *
61
+ * Combining `'percent'` with `units` is not meaningful and should be avoided.
62
+ */
8
63
  format?: 'number' | 'percent';
64
+ /** Factor applied to the value before formatting. For example, `multiplier: 1000` converts seconds to milliseconds. */
9
65
  multiplier?: number;
66
+ /** String prepended to the formatted value (e.g. `'$'`). */
10
67
  prefix?: string;
68
+ /** String appended to the formatted value (e.g. `' USD'`). */
11
69
  postfix?: string;
70
+ /**
71
+ * Compact unit suffix applied to large numbers.
72
+ * - `'auto'` — picks the most appropriate unit automatically (`k`, `m`, `b`, `t`).
73
+ * - `'k'` — thousands (÷ 1 000).
74
+ * - `'m'` — millions (÷ 1 000 000).
75
+ * - `'b'` — billions (÷ 1 000 000 000).
76
+ * - `'t'` — trillions (÷ 1 000 000 000 000).
77
+ * - `null` — no unit suffix.
78
+ *
79
+ * @deprecated Use `units` for custom scales. This option is fully ignored when `units` is set.
80
+ */
12
81
  unit?: 'auto' | 'k' | 'm' | 'b' | 't' | null;
13
- };
82
+ /**
83
+ * Custom unit scale, or a single `FormatUnitScaleEntry` as sugar for
84
+ * locking every value to one unit (equivalent to the legacy `unit: 'k'`).
85
+ * When set, fully overrides `unit`.
86
+ *
87
+ * Unit selection is always automatic: the entry with the largest `factor`
88
+ * such that `|value| / factor >= 1`, clamped to the smallest entry for
89
+ * very small values.
90
+ */
91
+ units?: FormatUnitScale | FormatUnitScaleEntry;
92
+ }
@@ -55,6 +55,7 @@ export const ChartInner = (props) => {
55
55
  const { activeLegendItems, allPreparedSeries, boundsHeight, boundsOffsetLeft, boundsOffsetTop, boundsWidth, handleLegendItemClick, legendConfig, legendItems, preparedLegend, preparedSeries, preparedSeriesOptions, preparedSplit, shapes, shapesData, shapesReady, xAxis, xScale, yAxis, yScale, preparedTitle, preparedChart, } = useChartInnerProps(Object.assign(Object.assign({}, props), { clipPathId,
56
56
  dispatcher,
57
57
  htmlLayout, plotNode: plotRef.current, rangeSliderState,
58
+ updateRangeSliderState,
58
59
  updateZoomState,
59
60
  zoomState }));
60
61
  const prevWidth = usePrevious(width);
@@ -9,6 +9,7 @@ type Props = ChartInnerProps & {
9
9
  dispatcher: Dispatch<object>;
10
10
  htmlLayout: HTMLElement | null;
11
11
  plotNode: SVGGElement | null;
12
+ updateRangeSliderState: (nextState?: RangeSliderState) => void;
12
13
  updateZoomState: (nextZoomState: Partial<ZoomState>) => void;
13
14
  zoomState: Partial<ZoomState>;
14
15
  rangeSliderState?: RangeSliderState;
@@ -38,7 +38,7 @@ function getBoundsOffsetLeft(args) {
38
38
  }
39
39
  export function useChartInnerProps(props) {
40
40
  var _a, _b, _c, _d, _e, _f, _g, _h;
41
- const { clipPathId, data, dispatcher, height, htmlLayout, plotNode, rangeSliderState, width, updateZoomState, zoomState, } = props;
41
+ const { clipPathId, data, dispatcher, height, htmlLayout, plotNode, rangeSliderState, width, updateRangeSliderState, updateZoomState, zoomState, } = props;
42
42
  const [selectedLegendItems, setSelectedLegendItems] = React.useState(null);
43
43
  const [chartState, setState] = React.useState(null);
44
44
  const prevStateValue = React.useRef(chartState);
@@ -48,20 +48,26 @@ export function useChartInnerProps(props) {
48
48
  currentRunRef.current++;
49
49
  const currentRun = currentRunRef.current;
50
50
  (async function () {
51
- var _a, _b, _c, _d;
51
+ var _a, _b, _c, _d, _e, _f, _g;
52
52
  const chartDataChanged = !(previousChartData.current && isEqual(previousChartData.current, data));
53
+ const axisTypeChanged = ((_b = (_a = previousChartData.current) === null || _a === void 0 ? void 0 : _a.xAxis) === null || _b === void 0 ? void 0 : _b.type) !== undefined &&
54
+ previousChartData.current.xAxis.type !== ((_c = data.xAxis) === null || _c === void 0 ? void 0 : _c.type);
55
+ if (axisTypeChanged && rangeSliderState !== undefined) {
56
+ updateRangeSliderState(undefined);
57
+ return;
58
+ }
53
59
  const preparedTitle = await getPreparedTitle({
54
60
  title: data.title,
55
61
  chartWidth: width,
56
62
  chartHeight: height,
57
- chartMargin: (_a = data.chart) === null || _a === void 0 ? void 0 : _a.margin,
63
+ chartMargin: (_d = data.chart) === null || _d === void 0 ? void 0 : _d.margin,
58
64
  });
59
65
  const preparedChart = getPreparedChart({
60
66
  chart: data.chart,
61
67
  seriesData: data.series.data,
62
68
  preparedTitle,
63
69
  });
64
- const colors = (_b = data.colors) !== null && _b !== void 0 ? _b : DEFAULT_PALETTE;
70
+ const colors = (_e = data.colors) !== null && _e !== void 0 ? _e : DEFAULT_PALETTE;
65
71
  const normalizedSeriesData = getSortedSeriesData({
66
72
  seriesData: data.series.data,
67
73
  xAxis: data.xAxis,
@@ -84,7 +90,7 @@ export function useChartInnerProps(props) {
84
90
  });
85
91
  }
86
92
  else {
87
- allPreparedSeries = (_d = (_c = prevStateValue.current) === null || _c === void 0 ? void 0 : _c.allPreparedSeries) !== null && _d !== void 0 ? _d : [];
93
+ allPreparedSeries = (_g = (_f = prevStateValue.current) === null || _f === void 0 ? void 0 : _f.allPreparedSeries) !== null && _g !== void 0 ? _g : [];
88
94
  }
89
95
  const activeLegendItems = selectedLegendItems !== null && selectedLegendItems !== void 0 ? selectedLegendItems : getActiveLegendItems(allPreparedSeries);
90
96
  const visiblePreparedSeries = getVisibleSeries({
@@ -243,6 +249,7 @@ export function useChartInnerProps(props) {
243
249
  rangeSliderState,
244
250
  dispatcher,
245
251
  htmlLayout,
252
+ updateRangeSliderState,
246
253
  clipPathId,
247
254
  ]);
248
255
  // additional start
@@ -0,0 +1,2 @@
1
+ export * from './types';
2
+ export * from './utils';
@@ -0,0 +1,2 @@
1
+ export * from './types';
2
+ export * from './utils';
@@ -1,6 +1,6 @@
1
1
  import type { BrushBehavior } from 'd3-brush';
2
- import type { ZoomType } from '../../core/constants';
3
- import type { ChartBrush, DeepRequired } from '../../types';
2
+ import type { ZoomType } from '../constants';
3
+ import type { ChartBrush, DeepRequired } from '../types';
4
4
  export type BrushSelection = [number, number] | [[number, number], [number, number]];
5
5
  export interface BrushArea {
6
6
  /**
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,5 @@
1
1
  import type { BrushBehavior } from 'd3-brush';
2
- import type { ChartBrush, DeepRequired } from '../../types';
2
+ import type { ChartBrush, DeepRequired } from '../types';
3
3
  import type { BrushSelection } from './types';
4
4
  export declare function isOneDimensionalSelection(selection?: BrushSelection | null): selection is [number, number];
5
5
  export declare function setBrushBorder(this: SVGGElement, _brushInstance: BrushBehavior<unknown>, selection: BrushSelection | null, options: {
@@ -0,0 +1 @@
1
+ export * from './types';
@@ -0,0 +1 @@
1
+ export * from './types';
@@ -0,0 +1,8 @@
1
+ import type { ChartBrush, ChartMargin, ChartZoom, DeepRequired } from '../types';
2
+ export type PreparedZoom = DeepRequired<Omit<ChartZoom, 'enabled' | 'brush'>> & DeepRequired<{
3
+ brush: ChartBrush;
4
+ }>;
5
+ export type PreparedChart = {
6
+ margin: ChartMargin;
7
+ zoom: PreparedZoom | null;
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -8,3 +8,6 @@ export * from './scales';
8
8
  export * from './axes';
9
9
  export * from './layout';
10
10
  export * from './zoom';
11
+ export * from './brush';
12
+ export * from './range-slider';
13
+ export * from './chart';
@@ -8,3 +8,6 @@ export * from './scales';
8
8
  export * from './axes';
9
9
  export * from './layout';
10
10
  export * from './zoom';
11
+ export * from './brush';
12
+ export * from './range-slider';
13
+ export * from './chart';
@@ -1,6 +1,6 @@
1
- import type { PreparedChart } from '../../hooks/types';
2
1
  import type { ChartMargin, LegendConfig } from '../../types';
3
2
  import type { PreparedXAxis, PreparedYAxis } from '../axes/types';
3
+ import type { PreparedChart } from '../chart/types';
4
4
  import type { PreparedLegend, PreparedSeries } from '../series';
5
5
  export declare const getBoundsWidth: (args: {
6
6
  chartWidth: number;
@@ -0,0 +1,2 @@
1
+ export * from './types';
2
+ export * from './utils';
@@ -0,0 +1,2 @@
1
+ export * from './types';
2
+ export * from './utils';
@@ -0,0 +1,4 @@
1
+ export type RangeSliderState = {
2
+ max: number;
3
+ min: number;
4
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,8 +1,8 @@
1
- import type { ChartScale } from '../../core/scales/types';
2
- import type { PreparedLegend } from '../../core/series/types';
3
- import type { PreparedChart } from '../types';
4
- import type { PreparedRangeSlider } from '../useAxis/types';
5
- import type { BrushSelection } from '../useBrush/types';
1
+ import type { PreparedRangeSlider } from '../axes/types';
2
+ import type { BrushSelection } from '../brush/types';
3
+ import type { PreparedChart } from '../chart/types';
4
+ import type { ChartScale } from '../scales/types';
5
+ import type { PreparedLegend } from '../series/types';
6
6
  import type { RangeSliderState } from './types';
7
7
  export declare function getRangeSliderOffsetTop(args: {
8
8
  height: number;
@@ -1,4 +1,4 @@
1
- import { isBandScale } from '../../core/utils';
1
+ import { isBandScale } from '../utils';
2
2
  export function getRangeSliderOffsetTop(args) {
3
3
  var _a, _b;
4
4
  const { height, preparedChart, preparedLegend, preparedRangeSlider } = args;
@@ -1,7 +1,7 @@
1
- import type { RangeSliderState } from '../../hooks';
2
- import type { ChartAxis, ChartSeries } from '../../types';
3
1
  import type { PreparedAxis } from '../axes/types';
2
+ import type { RangeSliderState } from '../range-slider/types';
4
3
  import type { PreparedSeries } from '../series';
4
+ import type { ChartAxis, ChartSeries } from '../types';
5
5
  export declare function createXScale(args: {
6
6
  axis: PreparedAxis | ChartAxis;
7
7
  boundsWidth: number;
@@ -218,6 +218,27 @@ export function createYScale(args) {
218
218
  else {
219
219
  yMax = hasSeriesWithVolumeOnYAxis ? Math.max(yMaxDomain, 0) : yMaxDomain;
220
220
  }
221
+ // When the user pins only one of min/max on the wrong side of all data,
222
+ // d3 produces either an inverted scale (yMax<yMin, silently flips the
223
+ // axis) or a degenerate one (yMax===yMin, maps everything to the range
224
+ // midpoint); both leave phantom hover targets inside an empty plot.
225
+ // Expand the auto-computed side so the scale stays sane. Only applies
226
+ // when the opposite bound is auto-derived — if the user supplied both
227
+ // bounds, trust the explicit range and let point filtering hide the
228
+ // rest. Comparison uses the raw data extent because upstream coercions
229
+ // like volume-series `yMax = Math.max(yMaxDomain, 0)` can make the
230
+ // post-coerced yMin/yMax lie about where data actually sits.
231
+ // Logarithmic has its own guard.
232
+ if (axis.type === 'linear') {
233
+ const minIsUserSet = typeof yMinPropsOrState === 'number';
234
+ const maxIsUserSet = typeof yMaxPropsOrState === 'number';
235
+ if (minIsUserSet && !maxIsUserSet && yMaxDomain < yMin) {
236
+ yMax = yMin + Math.max(Math.abs(yMin), 1);
237
+ }
238
+ else if (maxIsUserSet && !minIsUserSet && yMinDomain > yMax) {
239
+ yMin = yMax - Math.max(Math.abs(yMax), 1);
240
+ }
241
+ }
221
242
  const scaleFn = axis.type === 'logarithmic' ? scaleLog : scaleLinear;
222
243
  let scale = scaleFn().domain([yMin, yMax]).range(range);
223
244
  let offsetMin = 0;
@@ -1,5 +1,5 @@
1
- import type { PreparedChart } from '../../hooks/types';
2
1
  import type { ChartData } from '../../types';
2
+ import type { PreparedChart } from '../chart/types';
3
3
  import type { LegendItem, PreparedLegend, PreparedSeries } from './types';
4
4
  export declare function getPreparedLegend(args: {
5
5
  legend: ChartData['legend'];
@@ -2,7 +2,7 @@ import { group, min, sort } from 'd3-array';
2
2
  import isNil from 'lodash/isNil';
3
3
  import round from 'lodash/round';
4
4
  import { prepareAnnotation } from '../../series/prepare-annotation';
5
- import { getXValue, getYValue } from '../../shapes/utils';
5
+ import { getXValue, getYValue, markHiddenPointsOutOfYRange } from '../../shapes/utils';
6
6
  import { getDataCategoryValue, getLabelsSize, getTextSizeFn } from '../../utils';
7
7
  import { getFormattedValue } from '../../utils/format';
8
8
  function getXValues(series, xAxis, xScale) {
@@ -315,6 +315,11 @@ export const prepareAreaData = async (args) => {
315
315
  }
316
316
  return result;
317
317
  }, []);
318
+ markHiddenPointsOutOfYRange({
319
+ points,
320
+ yScale: seriesYScale,
321
+ yAxisTop,
322
+ });
318
323
  seriesStackData.push({
319
324
  annotations,
320
325
  points,
@@ -5,6 +5,7 @@ import get from 'lodash/get';
5
5
  import { block } from '../../../utils';
6
6
  import { filterOverlappingLabels } from '../../utils';
7
7
  import { renderAnnotations } from '../annotation';
8
+ import { renderDataLabels } from '../data-labels';
8
9
  import { getMarkerHaloVisibility, getMarkerVisibility, renderMarker, selectMarkerHalo, selectMarkerSymbol, setMarker, } from '../marker';
9
10
  import { setActiveState } from '../utils';
10
11
  const b = block('area');
@@ -17,7 +18,7 @@ export function renderArea(elements, preparedData, seriesOptions, allowOverlapDa
17
18
  const inactiveOptions = get(seriesOptions, 'area.states.inactive');
18
19
  const line = lineGenerator()
19
20
  .x((d) => d.x)
20
- .defined((d) => d.y !== null)
21
+ .defined((d) => d.y !== null && !d.hiddenInLine)
21
22
  .y((d) => d.y);
22
23
  plotSvgElement.selectAll('*').remove();
23
24
  markersSvgElement.selectAll('*').remove();
@@ -37,7 +38,7 @@ export function renderArea(elements, preparedData, seriesOptions, allowOverlapDa
37
38
  .attr('stroke-linejoin', 'round')
38
39
  .attr('stroke-linecap', 'round');
39
40
  const area = areaGenerator()
40
- .defined((d) => d.y !== null)
41
+ .defined((d) => d.y !== null && !d.hiddenInLine)
41
42
  .x((d) => d.x)
42
43
  .y0((d) => d.y0)
43
44
  .y1((d) => d.y);
@@ -53,18 +54,11 @@ export function renderArea(elements, preparedData, seriesOptions, allowOverlapDa
53
54
  if (!allowOverlapDataLabels) {
54
55
  dataLabels = filterOverlappingLabels(dataLabels);
55
56
  }
56
- const labelsSelection = plotSvgElement
57
- .selectAll('text')
58
- .data(dataLabels)
59
- .join('text')
60
- .html((d) => d.text)
61
- .attr('class', b('label'))
62
- .attr('x', (d) => d.x)
63
- .attr('y', (d) => d.y)
64
- .attr('text-anchor', (d) => d.textAnchor)
65
- .style('font-size', (d) => d.style.fontSize)
66
- .style('font-weight', (d) => d.style.fontWeight || null)
67
- .style('fill', (d) => d.style.fontColor || null);
57
+ const labelsSelection = renderDataLabels({
58
+ container: plotSvgElement,
59
+ data: dataLabels,
60
+ className: b('label'),
61
+ });
68
62
  const markers = preparedData.reduce((acc, d) => acc.concat(d.markers), []);
69
63
  const markerSelection = markersSvgElement
70
64
  .selectAll('marker')