@gravity-ui/chartkit 4.5.0 → 4.6.1

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 (63) hide show
  1. package/build/plugins/d3/renderer/D3Widget.js +11 -1
  2. package/build/plugins/d3/renderer/components/AxisX.d.ts +1 -2
  3. package/build/plugins/d3/renderer/components/AxisX.js +39 -61
  4. package/build/plugins/d3/renderer/components/AxisY.js +28 -31
  5. package/build/plugins/d3/renderer/components/Chart.js +19 -7
  6. package/build/plugins/d3/renderer/components/Legend.d.ts +5 -6
  7. package/build/plugins/d3/renderer/components/Legend.js +139 -84
  8. package/build/plugins/d3/renderer/components/styles.css +27 -0
  9. package/build/plugins/d3/renderer/constants/defaults/axis.d.ts +5 -0
  10. package/build/plugins/d3/renderer/constants/defaults/axis.js +5 -0
  11. package/build/plugins/d3/renderer/constants/defaults/index.d.ts +2 -0
  12. package/build/plugins/d3/renderer/constants/defaults/index.js +2 -0
  13. package/build/plugins/d3/renderer/constants/defaults/legend.d.ts +4 -0
  14. package/build/plugins/d3/renderer/constants/defaults/legend.js +8 -0
  15. package/build/plugins/d3/renderer/{constants.d.ts → constants/index.d.ts} +1 -1
  16. package/build/plugins/d3/renderer/{constants.js → constants/index.js} +1 -1
  17. package/build/plugins/d3/renderer/hooks/useAxisScales/index.d.ts +3 -1
  18. package/build/plugins/d3/renderer/hooks/useAxisScales/index.js +64 -62
  19. package/build/plugins/d3/renderer/hooks/useChartDimensions/index.d.ts +7 -4
  20. package/build/plugins/d3/renderer/hooks/useChartDimensions/index.js +65 -7
  21. package/build/plugins/d3/renderer/hooks/useChartDimensions/utils.d.ts +6 -0
  22. package/build/plugins/d3/renderer/hooks/useChartDimensions/utils.js +7 -0
  23. package/build/plugins/d3/renderer/hooks/useChartOptions/chart.d.ts +1 -3
  24. package/build/plugins/d3/renderer/hooks/useChartOptions/chart.js +9 -68
  25. package/build/plugins/d3/renderer/hooks/useChartOptions/index.d.ts +3 -1
  26. package/build/plugins/d3/renderer/hooks/useChartOptions/index.js +3 -8
  27. package/build/plugins/d3/renderer/hooks/useChartOptions/types.d.ts +3 -6
  28. package/build/plugins/d3/renderer/hooks/useChartOptions/x-axis.js +4 -2
  29. package/build/plugins/d3/renderer/hooks/useChartOptions/y-axis.d.ts +3 -2
  30. package/build/plugins/d3/renderer/hooks/useChartOptions/y-axis.js +31 -4
  31. package/build/plugins/d3/renderer/hooks/useSeries/constants.d.ts +1 -1
  32. package/build/plugins/d3/renderer/hooks/useSeries/constants.js +1 -1
  33. package/build/plugins/d3/renderer/hooks/useSeries/index.d.ts +19 -7
  34. package/build/plugins/d3/renderer/hooks/useSeries/index.js +26 -8
  35. package/build/plugins/d3/renderer/hooks/useSeries/prepare-legend.d.ts +27 -0
  36. package/build/plugins/d3/renderer/hooks/useSeries/prepare-legend.js +92 -0
  37. package/build/plugins/d3/renderer/hooks/useSeries/prepareSeries.d.ts +1 -2
  38. package/build/plugins/d3/renderer/hooks/useSeries/types.d.ts +26 -1
  39. package/build/plugins/d3/renderer/hooks/useShapes/bar-x.js +2 -1
  40. package/build/plugins/d3/renderer/utils/axis-generators/bottom.d.ts +21 -0
  41. package/build/plugins/d3/renderer/utils/axis-generators/bottom.js +104 -0
  42. package/build/plugins/d3/renderer/utils/axis-generators/index.d.ts +1 -0
  43. package/build/plugins/d3/renderer/utils/axis-generators/index.js +1 -0
  44. package/build/plugins/d3/renderer/utils/axis.d.ts +22 -0
  45. package/build/plugins/d3/renderer/utils/axis.js +43 -0
  46. package/build/plugins/d3/renderer/utils/index.d.ts +7 -4
  47. package/build/plugins/d3/renderer/utils/index.js +16 -6
  48. package/build/plugins/d3/renderer/utils/text.d.ts +20 -2
  49. package/build/plugins/d3/renderer/utils/text.js +51 -1
  50. package/build/plugins/d3/renderer/utils/time.d.ts +3 -0
  51. package/build/plugins/d3/renderer/utils/time.js +34 -0
  52. package/build/plugins/highcharts/renderer/components/HighchartsComponent.js +3 -3
  53. package/build/plugins/shared/format-number/format-number.d.ts +1 -0
  54. package/build/plugins/shared/format-number/format-number.js +19 -20
  55. package/build/types/widget-data/axis.d.ts +13 -1
  56. package/build/types/widget-data/legend.d.ts +24 -7
  57. package/build/utils/common.d.ts +1 -0
  58. package/build/utils/common.js +1 -1
  59. package/build/utils/index.d.ts +1 -1
  60. package/build/utils/index.js +1 -1
  61. package/package.json +1 -1
  62. package/build/plugins/d3/renderer/hooks/useChartOptions/legend.d.ts +0 -6
  63. package/build/plugins/d3/renderer/hooks/useChartOptions/legend.js +0 -12
@@ -43,7 +43,7 @@ export class HighchartsComponent extends React.PureComponent {
43
43
  };
44
44
  }
45
45
  static getDerivedStateFromProps(nextProps, prevState) {
46
- var _a;
46
+ var _a, _b;
47
47
  const isCurrentTooltipSplitted = get(prevState, 'options.tooltip.splitTooltip');
48
48
  const tooltipTypeWasChanged = isCurrentTooltipSplitted !== nextProps.splitTooltip;
49
49
  if (nextProps.data === prevState.prevData && !tooltipTypeWasChanged) {
@@ -54,14 +54,14 @@ export class HighchartsComponent extends React.PureComponent {
54
54
  const entryId = get(nextProps, 'data.entryId');
55
55
  const configOptions = Object.assign({
56
56
  nonBodyScroll,
57
- drillDownData: nextProps.data.config.drillDown,
57
+ drillDownData: (_a = nextProps.data.config) === null || _a === void 0 ? void 0 : _a.drillDown,
58
58
  splitTooltip: nextProps.splitTooltip,
59
59
  highcharts: libraryConfig,
60
60
  entryId,
61
61
  }, config);
62
62
  const { config: options, callback } = getGraph({
63
63
  options: configOptions,
64
- holidays: (_a = settings.get('extra')) === null || _a === void 0 ? void 0 : _a.holidays,
64
+ holidays: (_b = settings.get('extra')) === null || _b === void 0 ? void 0 : _b.holidays,
65
65
  data,
66
66
  comments,
67
67
  isMobile,
@@ -1,4 +1,5 @@
1
1
  import type { FormatOptions, FormatNumberOptions } from './types';
2
2
  export declare const formatBytes: (value: number, options?: FormatOptions) => string;
3
3
  export declare const formatDuration: (value: number, options?: FormatOptions) => string;
4
+ export declare const getNumberUnitRate: (value: number) => number;
4
5
  export declare const formatNumber: (value: number | string, options?: FormatNumberOptions) => string;
@@ -2,6 +2,15 @@ import ru from './i18n/ru.json';
2
2
  import en from './i18n/en.json';
3
3
  import { i18nInstance, makeInstance } from './i18n/i18n';
4
4
  const i18n = makeInstance('chartkit-units', { ru, en });
5
+ function getUnitRate(value, exponent, unitsI18nKeys) {
6
+ let resultUnitRate = 1;
7
+ while (Math.abs(value / Math.pow(exponent, resultUnitRate)) >= 1 &&
8
+ resultUnitRate < 10 &&
9
+ i18n(unitsI18nKeys[resultUnitRate])) {
10
+ resultUnitRate++;
11
+ }
12
+ return resultUnitRate - 1;
13
+ }
5
14
  const unitFormatter = ({ exponent, unitsI18nKeys, unitDelimiterI18nKey, }) => {
6
15
  return function formatUnit(value, options = {}) {
7
16
  const { precision, unitRate, showRankDelimiter = true, lang } = options;
@@ -9,19 +18,7 @@ const unitFormatter = ({ exponent, unitsI18nKeys, unitDelimiterI18nKey, }) => {
9
18
  if (lang) {
10
19
  i18nInstance.setLang(lang);
11
20
  }
12
- let resultUnitRate;
13
- if (typeof unitRate === 'number') {
14
- resultUnitRate = unitRate;
15
- }
16
- else {
17
- resultUnitRate = 1;
18
- while (Math.abs(value / Math.pow(exponent, resultUnitRate)) >= 1 &&
19
- resultUnitRate < 10 &&
20
- i18n(unitsI18nKeys[resultUnitRate])) {
21
- resultUnitRate++;
22
- }
23
- resultUnitRate--;
24
- }
21
+ const resultUnitRate = typeof unitRate === 'number' ? unitRate : getUnitRate(value, exponent, unitsI18nKeys);
25
22
  let result = value / Math.pow(exponent, resultUnitRate);
26
23
  if (typeof precision === 'number') {
27
24
  result = Number(result.toFixed(precision));
@@ -50,17 +47,19 @@ export const formatDuration = unitFormatter({
50
47
  unitDelimiterI18nKey: 'value_space-delimiter',
51
48
  unitsI18nKeys: ['value_short-milliseconds', 'value_short-seconds', 'value_short-minutes'],
52
49
  });
50
+ const BASE_NUMBER_FORMAT_UNIT_KEYS = [
51
+ 'value_short-empty',
52
+ 'value_short-k',
53
+ 'value_short-m',
54
+ 'value_short-b',
55
+ 'value_short-t',
56
+ ];
53
57
  const baseFormatNumber = unitFormatter({
54
58
  exponent: 1000,
55
59
  unitDelimiterI18nKey: 'value_number-delimiter',
56
- unitsI18nKeys: [
57
- 'value_short-empty',
58
- 'value_short-k',
59
- 'value_short-m',
60
- 'value_short-b',
61
- 'value_short-t',
62
- ],
60
+ unitsI18nKeys: BASE_NUMBER_FORMAT_UNIT_KEYS,
63
61
  });
62
+ export const getNumberUnitRate = (value) => getUnitRate(value, 1000, BASE_NUMBER_FORMAT_UNIT_KEYS);
64
63
  const NUMBER_UNIT_RATE_BY_UNIT = {
65
64
  default: 0,
66
65
  auto: undefined,
@@ -4,11 +4,23 @@ export type ChartKitWidgetAxisType = 'category' | 'datetime' | 'linear';
4
4
  export type ChartKitWidgetAxisLabels = {
5
5
  /** Enable or disable the axis labels. */
6
6
  enabled?: boolean;
7
- /** The pixel padding for axis labels. */
7
+ /** The label's pixel distance from the perimeter of the plot area.
8
+ *
9
+ * @default: 10
10
+ */
11
+ margin?: number;
12
+ /** The pixel padding for axis labels, to ensure white space between them.
13
+ *
14
+ * @defaults: 5
15
+ * */
8
16
  padding?: number;
9
17
  dateFormat?: string;
10
18
  numberFormat?: FormatNumberOptions;
11
19
  style?: Partial<BaseTextStyle>;
20
+ /** For horizontal axes, enable label rotation to prevent overlapping labels.
21
+ * If there is enough space, labels are not rotated.
22
+ * As the chart gets narrower, it will start rotating the labels -45 degrees. */
23
+ autoRotation?: boolean;
12
24
  };
13
25
  export type ChartKitWidgetAxis = {
14
26
  categories?: string[];
@@ -1,35 +1,52 @@
1
+ import type { BaseTextStyle } from './base';
1
2
  export type ChartKitWidgetLegend = {
2
3
  enabled?: boolean;
3
- /** The horizontal alignment of the legend box within the chart area.
4
+ /**
5
+ * The horizontal alignment of the legend box within the chart area.
4
6
  *
5
7
  * @default center
6
8
  * */
7
9
  align?: 'left' | 'center' | 'right';
8
- /** Defines the pixel distance between each legend item
10
+ /**
11
+ * Defines the pixel distance between each legend item
9
12
  *
10
13
  * @default 20
11
14
  * */
12
15
  itemDistance?: number;
16
+ /** CSS styles for each legend item */
17
+ itemStyle?: BaseTextStyle;
18
+ /**
19
+ * The space between the legend and the axis labels or chart area.
20
+ *
21
+ * @default 15
22
+ */
23
+ margin?: number;
13
24
  };
14
25
  export type BaseLegendSymbol = {
15
- /** The pixel padding between the legend item symbol and the legend item text.
26
+ /**
27
+ * The pixel padding between the legend item symbol and the legend item text.
16
28
  *
17
29
  * @default 5
18
30
  * */
19
31
  padding?: number;
20
32
  };
21
33
  export type RectLegendSymbolOptions = BaseLegendSymbol & {
22
- /** The pixel width of the symbol for series types that use a rectangle in the legend
34
+ /**
35
+ * The pixel width of the symbol for series types that use a rectangle in the legend
23
36
  *
24
37
  * @default 10
25
38
  * */
26
39
  width?: number;
27
- /** The pixel width of the symbol for series types that use a rectangle in the legend
40
+ /**
41
+ * The pixel width of the symbol for series types that use a rectangle in the legend
28
42
  *
29
43
  * @default 10
30
44
  * */
31
45
  height?: number;
32
- /** The border radius of the symbol for series types that use a rectangle in the legend.
33
- * Defaults to half the symbolHeight, effectively creating a circle. */
46
+ /**
47
+ * The border radius of the symbol for series types that use a rectangle in the legend.
48
+ *
49
+ * Defaults to half the symbolHeight, effectively creating a circle.
50
+ */
34
51
  radius?: number;
35
52
  };
@@ -1 +1,2 @@
1
+ export declare const randomString: (length: number, chars: string) => string;
1
2
  export declare const getRandomCKId: () => string;
@@ -1,4 +1,4 @@
1
- const randomString = (length, chars) => {
1
+ export const randomString = (length, chars) => {
2
2
  let result = '';
3
3
  for (let i = length; i > 0; --i) {
4
4
  result += chars[Math.floor(Math.random() * chars.length)];
@@ -1,3 +1,3 @@
1
- export { getRandomCKId } from './common';
1
+ export { getRandomCKId, randomString } from './common';
2
2
  export { typedMemo } from './react';
3
3
  export { getChartPerformanceDuration, markChartPerformance } from './performance';
@@ -1,3 +1,3 @@
1
- export { getRandomCKId } from './common';
1
+ export { getRandomCKId, randomString } from './common';
2
2
  export { typedMemo } from './react';
3
3
  export { getChartPerformanceDuration, markChartPerformance } from './performance';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "4.5.0",
3
+ "version": "4.6.1",
4
4
  "description": "React component used to render charts based on any sources you need",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:gravity-ui/ChartKit.git",
@@ -1,6 +0,0 @@
1
- import type { ChartKitWidgetData } from '../../../../../types/widget-data';
2
- import type { PreparedLegend } from './types';
3
- export declare const getPreparedLegend: (args: {
4
- legend: ChartKitWidgetData['legend'];
5
- series: ChartKitWidgetData['series'];
6
- }) => PreparedLegend;
@@ -1,12 +0,0 @@
1
- const LEGEND_LINE_HEIGHT = 15;
2
- export const getPreparedLegend = (args) => {
3
- const { legend, series } = args;
4
- const enabled = typeof (legend === null || legend === void 0 ? void 0 : legend.enabled) === 'boolean' ? legend === null || legend === void 0 ? void 0 : legend.enabled : series.data.length > 1;
5
- const height = enabled ? LEGEND_LINE_HEIGHT : 0;
6
- return {
7
- align: (legend === null || legend === void 0 ? void 0 : legend.align) || 'center',
8
- enabled,
9
- itemDistance: (legend === null || legend === void 0 ? void 0 : legend.itemDistance) || 20,
10
- height,
11
- };
12
- };