@gravity-ui/charts 1.33.0 → 1.34.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 (49) hide show
  1. package/dist/cjs/components/ChartInner/useChartInnerProps.js +3 -2
  2. package/dist/cjs/components/Tooltip/DefaultTooltipContent/index.js +5 -1
  3. package/dist/cjs/hooks/index.d.ts +1 -0
  4. package/dist/cjs/hooks/index.js +1 -0
  5. package/dist/cjs/hooks/useAxis/index.d.ts +5 -3
  6. package/dist/cjs/hooks/useAxis/index.js +3 -3
  7. package/dist/cjs/hooks/useAxis/types.d.ts +6 -0
  8. package/dist/cjs/hooks/useAxis/y-axis.d.ts +10 -0
  9. package/dist/cjs/hooks/useAxis/y-axis.js +30 -21
  10. package/dist/cjs/hooks/useAxisScales/index.d.ts +2 -16
  11. package/dist/cjs/hooks/useAxisScales/index.js +17 -475
  12. package/dist/cjs/hooks/useAxisScales/utils.d.ts +10 -13
  13. package/dist/cjs/hooks/useAxisScales/utils.js +29 -63
  14. package/dist/cjs/hooks/useAxisScales/x-scale.d.ts +15 -0
  15. package/dist/cjs/hooks/useAxisScales/x-scale.js +247 -0
  16. package/dist/cjs/hooks/useAxisScales/y-scale.d.ts +10 -0
  17. package/dist/cjs/hooks/useAxisScales/y-scale.js +299 -0
  18. package/dist/cjs/hooks/useYAxisLabelWidth/index.d.ts +11 -0
  19. package/dist/cjs/hooks/useYAxisLabelWidth/index.js +48 -0
  20. package/dist/cjs/hooks/utils/bar-x.js +1 -1
  21. package/dist/cjs/hooks/utils/bar-y.js +1 -1
  22. package/dist/cjs/types/chart/tooltip.d.ts +3 -2
  23. package/dist/cjs/utils/chart/axis/common.d.ts +1 -0
  24. package/dist/cjs/utils/chart/axis/common.js +6 -0
  25. package/dist/esm/components/ChartInner/useChartInnerProps.js +3 -2
  26. package/dist/esm/components/Tooltip/DefaultTooltipContent/index.js +5 -1
  27. package/dist/esm/hooks/index.d.ts +1 -0
  28. package/dist/esm/hooks/index.js +1 -0
  29. package/dist/esm/hooks/useAxis/index.d.ts +5 -3
  30. package/dist/esm/hooks/useAxis/index.js +3 -3
  31. package/dist/esm/hooks/useAxis/types.d.ts +6 -0
  32. package/dist/esm/hooks/useAxis/y-axis.d.ts +10 -0
  33. package/dist/esm/hooks/useAxis/y-axis.js +30 -21
  34. package/dist/esm/hooks/useAxisScales/index.d.ts +2 -16
  35. package/dist/esm/hooks/useAxisScales/index.js +17 -475
  36. package/dist/esm/hooks/useAxisScales/utils.d.ts +10 -13
  37. package/dist/esm/hooks/useAxisScales/utils.js +29 -63
  38. package/dist/esm/hooks/useAxisScales/x-scale.d.ts +15 -0
  39. package/dist/esm/hooks/useAxisScales/x-scale.js +247 -0
  40. package/dist/esm/hooks/useAxisScales/y-scale.d.ts +10 -0
  41. package/dist/esm/hooks/useAxisScales/y-scale.js +299 -0
  42. package/dist/esm/hooks/useYAxisLabelWidth/index.d.ts +11 -0
  43. package/dist/esm/hooks/useYAxisLabelWidth/index.js +48 -0
  44. package/dist/esm/hooks/utils/bar-x.js +1 -1
  45. package/dist/esm/hooks/utils/bar-y.js +1 -1
  46. package/dist/esm/types/chart/tooltip.d.ts +3 -2
  47. package/dist/esm/utils/chart/axis/common.d.ts +1 -0
  48. package/dist/esm/utils/chart/axis/common.js +6 -0
  49. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { DEFAULT_PALETTE, SERIES_TYPE } from '../../constants';
3
- import { useAxis, useAxisScales, useChartDimensions, useNormalizedOriginalData, usePrevious, useSeries, useShapes, useSplit, useZoom, } from '../../hooks';
3
+ import { useAxis, useAxisScales, useChartDimensions, useNormalizedOriginalData, usePrevious, useSeries, useShapes, useSplit, useYAxisLabelWidth, useZoom, } from '../../hooks';
4
4
  import { getYAxisWidth } from '../../hooks/useChartDimensions/utils';
5
5
  import { getLegendComponents } from '../../hooks/useSeries/prepare-legend';
6
6
  import { getPreparedOptions } from '../../hooks/useSeries/prepare-options';
@@ -77,7 +77,7 @@ export function useChartInnerProps(props) {
77
77
  preparedLegend,
78
78
  });
79
79
  }, [width, height, preparedChart.margin, preparedSeries, preparedLegend]);
80
- const { xAxis, yAxis } = useAxis({
80
+ const { xAxis, yAxis, setAxes } = useAxis({
81
81
  height,
82
82
  preparedChart,
83
83
  preparedLegend,
@@ -107,6 +107,7 @@ export function useChartInnerProps(props) {
107
107
  yAxis,
108
108
  zoomState,
109
109
  });
110
+ useYAxisLabelWidth({ seriesData: preparedSeries, setAxes, yAxis, yScale });
110
111
  const isOutsideBounds = React.useCallback((x, y) => {
111
112
  return x < 0 || x > boundsWidth || y < 0 || y > boundsHeight;
112
113
  }, [boundsHeight, boundsWidth]);
@@ -23,7 +23,7 @@ export const DefaultTooltipContent = ({ hovered, pinned, rowRenderer, totals, va
23
23
  const restHoveredValues = pinned || !visibleRows ? [] : hoveredValues.slice(visibleRows);
24
24
  const renderRow = ({ id, name, color, active, striped, value, formattedValue, series, }) => {
25
25
  if (typeof rowRenderer === 'function') {
26
- return rowRenderer({
26
+ const result = rowRenderer({
27
27
  id,
28
28
  name,
29
29
  color,
@@ -34,6 +34,10 @@ export const DefaultTooltipContent = ({ hovered, pinned, rowRenderer, totals, va
34
34
  className: b('content-row', { active, striped }),
35
35
  hovered,
36
36
  });
37
+ if (typeof result === 'string') {
38
+ return React.createElement("div", { key: id, dangerouslySetInnerHTML: { __html: result } });
39
+ }
40
+ return result;
37
41
  }
38
42
  const colorSymbol = getTooltipRowColorSymbol({ series, color });
39
43
  return (React.createElement(Row, { key: id, active: active, color: color, colorSymbol: colorSymbol ? (React.createElement("div", { dangerouslySetInnerHTML: { __html: colorSymbol.outerHTML } })) : undefined, label: React.createElement("span", { dangerouslySetInnerHTML: { __html: name } }), striped: striped, value: formattedValue }));
@@ -17,5 +17,6 @@ export * from './useShapes';
17
17
  export * from './useSplit';
18
18
  export * from './useSplit/types';
19
19
  export * from './useTooltip';
20
+ export * from './useYAxisLabelWidth';
20
21
  export * from './useZoom';
21
22
  export * from './useZoom/types';
@@ -17,5 +17,6 @@ export * from './useShapes';
17
17
  export * from './useSplit';
18
18
  export * from './useSplit/types';
19
19
  export * from './useTooltip';
20
+ export * from './useYAxisLabelWidth';
20
21
  export * from './useZoom';
21
22
  export * from './useZoom/types';
@@ -1,7 +1,8 @@
1
+ import React from 'react';
1
2
  import type { ChartXAxis, ChartYAxis } from '../../types';
2
3
  import type { PreparedChart } from '../useChartOptions/types';
3
4
  import type { PreparedLegend, PreparedSeries, PreparedSeriesOptions } from '../useSeries/types';
4
- import type { PreparedXAxis, PreparedYAxis } from './types';
5
+ import type { AxesState } from './types';
5
6
  interface UseAxesProps {
6
7
  height: number;
7
8
  preparedChart: PreparedChart;
@@ -14,7 +15,8 @@ interface UseAxesProps {
14
15
  yAxis?: ChartYAxis[];
15
16
  }
16
17
  export declare function useAxis(props: UseAxesProps): {
17
- xAxis: PreparedXAxis | null;
18
- yAxis: PreparedYAxis[];
18
+ setAxes: React.Dispatch<React.SetStateAction<AxesState>>;
19
+ xAxis: import("./types").PreparedXAxis | null;
20
+ yAxis: import("./types").PreparedYAxis[];
19
21
  };
20
22
  export {};
@@ -5,7 +5,7 @@ import { getPreparedXAxis } from './x-axis';
5
5
  import { getPreparedYAxis } from './y-axis';
6
6
  export function useAxis(props) {
7
7
  const { boundsHeight, height, preparedChart, preparedLegend, preparedSeries, preparedSeriesOptions, width, xAxis, yAxis, } = props;
8
- const [axesState, setValue] = React.useState({ xAxis: null, yAxis: [] });
8
+ const [axesState, setAxes] = React.useState({ xAxis: null, yAxis: [] });
9
9
  const axesStateRunRef = React.useRef(0);
10
10
  const prevAxesStateValue = React.useRef(axesState);
11
11
  const axesStateReady = React.useRef(false);
@@ -55,7 +55,7 @@ export function useAxis(props) {
55
55
  const newStateValue = { xAxis: preparedXAxis, yAxis: preparedYAxis };
56
56
  if (axesStateRunRef.current === currentRun) {
57
57
  if (!isEqual(prevAxesStateValue.current, newStateValue)) {
58
- setValue(newStateValue);
58
+ setAxes(newStateValue);
59
59
  prevAxesStateValue.current = newStateValue;
60
60
  }
61
61
  axesStateReady.current = true;
@@ -72,5 +72,5 @@ export function useAxis(props) {
72
72
  xAxis,
73
73
  yAxis,
74
74
  ]);
75
- return axesStateReady.current ? axesState : { xAxis: null, yAxis: [] };
75
+ return axesStateReady.current ? Object.assign(Object.assign({}, axesState), { setAxes }) : { xAxis: null, yAxis: [], setAxes };
76
76
  }
@@ -1,3 +1,4 @@
1
+ import type React from 'react';
1
2
  import type { DashStyle } from '../../constants';
2
3
  import type { AxisCrosshair, AxisPlotBand, BaseTextStyle, ChartAxis, ChartAxisLabels, ChartAxisRangeSlider, ChartAxisTitleAlignment, ChartAxisType, DeepRequired, PlotLayerPlacement } from '../../types';
3
4
  type PreparedAxisLabels = Omit<ChartAxisLabels, 'enabled' | 'padding' | 'style' | 'autoRotation'> & Required<Pick<ChartAxisLabels, 'enabled' | 'padding' | 'margin' | 'rotation' | 'html'>> & {
@@ -68,4 +69,9 @@ export type PreparedXAxis = PreparedBaseAxis & {
68
69
  };
69
70
  export type PreparedYAxis = PreparedBaseAxis;
70
71
  export type PreparedAxis = PreparedXAxis | PreparedYAxis;
72
+ export type AxesState = {
73
+ xAxis: PreparedXAxis | null;
74
+ yAxis: PreparedYAxis[];
75
+ };
76
+ export type SetAxes = React.Dispatch<React.SetStateAction<AxesState>>;
71
77
  export {};
@@ -1,5 +1,15 @@
1
1
  import type { ChartSeries, ChartYAxis } from '../../types';
2
+ import type { ChartScale } from '../useAxisScales/types';
3
+ import type { PreparedSeries } from '../useSeries/types';
2
4
  import type { PreparedYAxis } from './types';
5
+ export declare const getYAxisLabelMaxWidth: (args: {
6
+ axis: PreparedYAxis;
7
+ seriesData: PreparedSeries[] | ChartSeries[];
8
+ scale?: ChartScale;
9
+ }) => Promise<{
10
+ height: number;
11
+ width: number;
12
+ }>;
3
13
  export declare const getPreparedYAxis: ({ height, boundsHeight, width, seriesData, yAxis, }: {
4
14
  height: number;
5
15
  boundsHeight: number;
@@ -1,20 +1,12 @@
1
1
  import get from 'lodash/get';
2
2
  import { getTickValues } from '../../components/AxisY/utils';
3
3
  import { DASH_STYLE, DEFAULT_AXIS_LABEL_FONT_SIZE, DEFAULT_AXIS_TYPE, SERIES_TYPE, axisCrosshairDefaults, axisLabelsDefaults, yAxisTitleDefaults, } from '../../constants';
4
- import { calculateNumericProperty, formatAxisTickLabel, getDefaultDateFormat, getDefaultMinYAxisValue, getHorizontalHtmlTextHeight, getHorizontalSvgTextHeight, getLabelsSize, getMinSpaceBetween, getTextSizeFn, isAxisRelatedSeries, wrapText, } from '../../utils';
4
+ import { calculateNumericProperty, formatAxisTickLabel, getDefaultDateFormat, getDefaultMinYAxisValue, getHorizontalHtmlTextHeight, getHorizontalSvgTextHeight, getLabelsSize, getMinSpaceBetween, getTextSizeFn, isAxisRelatedSeries, shouldSyncAxisWithPrimary, wrapText, } from '../../utils';
5
5
  import { createYScale } from '../useAxisScales';
6
6
  import { prepareAxisPlotLabel } from './utils';
7
- const getAxisLabelMaxWidth = async (args) => {
8
- const { axis, seriesData, height } = args;
9
- if (!axis.labels.enabled) {
10
- return { height: 0, width: 0 };
11
- }
12
- const scale = createYScale({
13
- axis,
14
- boundsHeight: height,
15
- series: seriesData,
16
- });
17
- if (!scale) {
7
+ export const getYAxisLabelMaxWidth = async (args) => {
8
+ const { axis, scale, seriesData } = args;
9
+ if (!axis.labels.enabled || !scale) {
18
10
  return { height: 0, width: 0 };
19
11
  }
20
12
  const getTextSize = getTextSizeFn({ style: axis.labels.style });
@@ -104,6 +96,21 @@ export const getPreparedYAxis = ({ height, boundsHeight, width, seriesData, yAxi
104
96
  const axisType = get(axisItem, 'type', DEFAULT_AXIS_TYPE);
105
97
  const shouldHideGrid = axisItem.visible === false ||
106
98
  axisSeriesData.some((s) => s.type === SERIES_TYPE.Heatmap);
99
+ let gridEnabled;
100
+ if (shouldHideGrid) {
101
+ gridEnabled = false;
102
+ }
103
+ else {
104
+ const gridEnabledProp = get(axisItem, 'grid.enabled');
105
+ if (firstPlotAxis) {
106
+ gridEnabled = gridEnabledProp !== null && gridEnabledProp !== void 0 ? gridEnabledProp : true;
107
+ }
108
+ else {
109
+ gridEnabled = shouldSyncAxisWithPrimary(axisItem, axisByPlot[plotIndex][0])
110
+ ? false
111
+ : !((_g = axisByPlot[plotIndex][0].visible) !== null && _g !== void 0 ? _g : true);
112
+ }
113
+ }
107
114
  const preparedAxis = {
108
115
  type: axisType,
109
116
  labels: {
@@ -119,7 +126,7 @@ export const getPreparedYAxis = ({ height, boundsHeight, width, seriesData, yAxi
119
126
  width: 0,
120
127
  height: 0,
121
128
  lineHeight: labelsLineHeight,
122
- maxWidth: (_h = calculateNumericProperty({ base: width, value: (_g = axisItem.labels) === null || _g === void 0 ? void 0 : _g.maxWidth })) !== null && _h !== void 0 ? _h : axisLabelsDefaults.maxWidth,
129
+ maxWidth: (_j = calculateNumericProperty({ base: width, value: (_h = axisItem.labels) === null || _h === void 0 ? void 0 : _h.maxWidth })) !== null && _j !== void 0 ? _j : axisLabelsDefaults.maxWidth,
123
130
  html: labelsHtml,
124
131
  },
125
132
  lineColor: get(axisItem, 'lineColor'),
@@ -133,20 +140,17 @@ export const getPreparedYAxis = ({ height, boundsHeight, width, seriesData, yAxi
133
140
  height: titleSize.maxHeight * estimatedTitleRows.length,
134
141
  align: get(axisItem, 'title.align', yAxisTitleDefaults.align),
135
142
  maxRowCount: titleMaxRowsCount,
136
- html: (_k = (_j = axisItem.title) === null || _j === void 0 ? void 0 : _j.html) !== null && _k !== void 0 ? _k : false,
143
+ html: (_l = (_k = axisItem.title) === null || _k === void 0 ? void 0 : _k.html) !== null && _l !== void 0 ? _l : false,
137
144
  maxWidth: titleMaxWidth !== null && titleMaxWidth !== void 0 ? titleMaxWidth : Infinity,
138
145
  rotation: titleRotation,
139
146
  },
140
- min: (_l = get(axisItem, 'min')) !== null && _l !== void 0 ? _l : getDefaultMinYAxisValue(axisSeriesData),
147
+ min: (_m = get(axisItem, 'min')) !== null && _m !== void 0 ? _m : getDefaultMinYAxisValue(axisSeriesData),
141
148
  max: get(axisItem, 'max'),
142
149
  startOnTick: get(axisItem, 'startOnTick'),
143
150
  endOnTick: get(axisItem, 'endOnTick'),
144
151
  maxPadding: get(axisItem, 'maxPadding', getMaxPaddingBySeries({ series: axisSeriesData })),
145
152
  grid: {
146
- enabled: shouldHideGrid
147
- ? false
148
- : get(axisItem, 'grid.enabled', firstPlotAxis ||
149
- (!firstPlotAxis && !((_m = axisByPlot[plotIndex][0].visible) !== null && _m !== void 0 ? _m : true))),
153
+ enabled: gridEnabled,
150
154
  },
151
155
  ticks: {
152
156
  pixelInterval: ((_o = axisItem.ticks) === null || _o === void 0 ? void 0 : _o.interval)
@@ -188,10 +192,15 @@ export const getPreparedYAxis = ({ height, boundsHeight, width, seriesData, yAxi
188
192
  order: axisItem.order,
189
193
  };
190
194
  if (labelsEnabled) {
191
- const { height: labelsHeight, width: labelsWidth } = await getAxisLabelMaxWidth({
195
+ const scale = createYScale({
196
+ axis: preparedAxis,
197
+ boundsHeight,
198
+ series: axisSeriesData,
199
+ });
200
+ const { height: labelsHeight, width: labelsWidth } = await getYAxisLabelMaxWidth({
192
201
  axis: preparedAxis,
193
202
  seriesData: axisSeriesData,
194
- height: boundsHeight,
203
+ scale,
195
204
  });
196
205
  preparedAxis.labels.height = labelsHeight;
197
206
  preparedAxis.labels.width = Math.min(preparedAxis.labels.maxWidth, labelsWidth);
@@ -1,6 +1,7 @@
1
1
  import type { PreparedAxis, PreparedSeries, PreparedSplit, RangeSliderState, ZoomState } from '../../hooks';
2
- import type { ChartAxis, ChartSeries } from '../../types';
3
2
  import type { ChartScale } from './types';
3
+ export { createXScale } from './x-scale';
4
+ export { createYScale } from './y-scale';
4
5
  type Args = {
5
6
  boundsWidth: number;
6
7
  boundsHeight: number;
@@ -16,22 +17,7 @@ type ReturnValue = {
16
17
  xScale?: ChartScale;
17
18
  yScale?: (ChartScale | undefined)[];
18
19
  };
19
- export declare function createYScale(args: {
20
- axis: PreparedAxis;
21
- boundsHeight: number;
22
- series: PreparedSeries[] | ChartSeries[];
23
- primaryTickPositions?: number[];
24
- zoomStateY?: [number, number];
25
- }): import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleTime<number, number, never> | undefined;
26
- export declare function createXScale(args: {
27
- axis: PreparedAxis | ChartAxis;
28
- boundsWidth: number;
29
- series: (PreparedSeries | ChartSeries)[];
30
- rangeSliderState?: RangeSliderState;
31
- zoomStateX?: [number, number];
32
- }): import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleTime<number, number, never> | undefined;
33
20
  /**
34
21
  * Uses to create scales for axis related series
35
22
  */
36
23
  export declare const useAxisScales: (args: Args) => ReturnValue;
37
- export {};