@gravity-ui/charts 1.38.6 → 1.38.7

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 (69) hide show
  1. package/dist/cjs/components/AxisX/prepare-axis-data.d.ts +1 -1
  2. package/dist/cjs/components/AxisX/prepare-axis-data.js +10 -9
  3. package/dist/cjs/components/AxisY/prepare-axis-data.d.ts +1 -1
  4. package/dist/cjs/components/AxisY/prepare-axis-data.js +2 -2
  5. package/dist/cjs/components/ChartInner/index.js +8 -3
  6. package/dist/cjs/components/ChartInner/useChartInnerProps.d.ts +18 -19
  7. package/dist/cjs/components/ChartInner/useChartInnerProps.js +232 -138
  8. package/dist/cjs/components/ChartInner/useChartInnerState.js +2 -1
  9. package/dist/cjs/components/Legend/index.d.ts +1 -1
  10. package/dist/cjs/components/Legend/index.js +1 -1
  11. package/dist/cjs/hooks/useAxis/index.d.ts +40 -5
  12. package/dist/cjs/hooks/useAxis/index.js +54 -39
  13. package/dist/cjs/hooks/useAxis/types.d.ts +0 -2
  14. package/dist/cjs/hooks/useAxisScales/index.d.ts +4 -0
  15. package/dist/cjs/hooks/useAxisScales/index.js +1 -1
  16. package/dist/cjs/hooks/useChartDimensions/index.d.ts +1 -1
  17. package/dist/cjs/hooks/useChartDimensions/index.js +14 -29
  18. package/dist/cjs/hooks/useCrosshair/index.d.ts +1 -1
  19. package/dist/cjs/hooks/useNormalizedOriginalData/index.d.ts +29 -34
  20. package/dist/cjs/hooks/useNormalizedOriginalData/index.js +19 -30
  21. package/dist/cjs/hooks/useSeries/index.d.ts +10 -14
  22. package/dist/cjs/hooks/useSeries/index.js +8 -80
  23. package/dist/cjs/hooks/useShapes/index.d.ts +4 -0
  24. package/dist/cjs/hooks/useShapes/index.js +194 -189
  25. package/dist/cjs/hooks/useSplit/index.d.ts +5 -2
  26. package/dist/cjs/hooks/useSplit/index.js +27 -30
  27. package/dist/cjs/hooks/useYAxisLabelWidth/index.d.ts +39 -6
  28. package/dist/cjs/hooks/useYAxisLabelWidth/index.js +30 -43
  29. package/dist/cjs/hooks/useZoom/index.d.ts +1 -1
  30. package/dist/cjs/hooks/useZoom/index.js +2 -2
  31. package/dist/cjs/utils/chart/axis/common.d.ts +1 -1
  32. package/dist/cjs/utils/chart/axis/common.js +1 -1
  33. package/dist/esm/components/AxisX/prepare-axis-data.d.ts +1 -1
  34. package/dist/esm/components/AxisX/prepare-axis-data.js +10 -9
  35. package/dist/esm/components/AxisY/prepare-axis-data.d.ts +1 -1
  36. package/dist/esm/components/AxisY/prepare-axis-data.js +2 -2
  37. package/dist/esm/components/ChartInner/index.js +8 -3
  38. package/dist/esm/components/ChartInner/useChartInnerProps.d.ts +18 -19
  39. package/dist/esm/components/ChartInner/useChartInnerProps.js +232 -138
  40. package/dist/esm/components/ChartInner/useChartInnerState.js +2 -1
  41. package/dist/esm/components/Legend/index.d.ts +1 -1
  42. package/dist/esm/components/Legend/index.js +1 -1
  43. package/dist/esm/hooks/useAxis/index.d.ts +40 -5
  44. package/dist/esm/hooks/useAxis/index.js +54 -39
  45. package/dist/esm/hooks/useAxis/types.d.ts +0 -2
  46. package/dist/esm/hooks/useAxisScales/index.d.ts +4 -0
  47. package/dist/esm/hooks/useAxisScales/index.js +1 -1
  48. package/dist/esm/hooks/useChartDimensions/index.d.ts +1 -1
  49. package/dist/esm/hooks/useChartDimensions/index.js +14 -29
  50. package/dist/esm/hooks/useCrosshair/index.d.ts +1 -1
  51. package/dist/esm/hooks/useNormalizedOriginalData/index.d.ts +29 -34
  52. package/dist/esm/hooks/useNormalizedOriginalData/index.js +19 -30
  53. package/dist/esm/hooks/useSeries/index.d.ts +10 -14
  54. package/dist/esm/hooks/useSeries/index.js +8 -80
  55. package/dist/esm/hooks/useShapes/index.d.ts +4 -0
  56. package/dist/esm/hooks/useShapes/index.js +194 -189
  57. package/dist/esm/hooks/useSplit/index.d.ts +5 -2
  58. package/dist/esm/hooks/useSplit/index.js +27 -30
  59. package/dist/esm/hooks/useYAxisLabelWidth/index.d.ts +39 -6
  60. package/dist/esm/hooks/useYAxisLabelWidth/index.js +30 -43
  61. package/dist/esm/hooks/useZoom/index.d.ts +1 -1
  62. package/dist/esm/hooks/useZoom/index.js +2 -2
  63. package/dist/esm/utils/chart/axis/common.d.ts +1 -1
  64. package/dist/esm/utils/chart/axis/common.js +1 -1
  65. package/package.json +1 -1
  66. package/dist/cjs/components/ChartInner/useLegend.d.ts +0 -14
  67. package/dist/cjs/components/ChartInner/useLegend.js +0 -34
  68. package/dist/esm/components/ChartInner/useLegend.d.ts +0 -14
  69. package/dist/esm/components/ChartInner/useLegend.js +0 -34
@@ -1,4 +1,3 @@
1
- import React from 'react';
2
1
  import type { ChartXAxis, ChartYAxis, LegendConfig } from '../../types';
3
2
  import type { PreparedChart } from '../useChartOptions/types';
4
3
  import type { PreparedLegend, PreparedSeries, PreparedSeriesOptions } from '../useSeries/types';
@@ -15,9 +14,45 @@ interface UseAxesProps {
15
14
  xAxis?: ChartXAxis;
16
15
  yAxis?: ChartYAxis[];
17
16
  }
18
- export declare function useAxis(props: UseAxesProps): {
19
- setAxes: React.Dispatch<React.SetStateAction<AxesState>>;
17
+ export declare function getAxes(props: UseAxesProps): Promise<{
20
18
  xAxis: import("./types").PreparedXAxis | null;
21
- yAxis: import("./types").PreparedYAxis[];
22
- };
19
+ yAxis: (Omit<import("../../types").ChartAxis, "type" | "labels" | "plotLines" | "plotBands"> & {
20
+ type: import("../../types").ChartAxisType;
21
+ labels: Omit<import("../../types").ChartAxisLabels, "enabled" | "style" | "padding" | "autoRotation"> & Required<Pick<import("../../types").ChartAxisLabels, "margin" | "html" | "enabled" | "rotation" | "padding">> & {
22
+ style: import("../../types").BaseTextStyle;
23
+ rotation: number;
24
+ height: number;
25
+ width: number;
26
+ lineHeight: number;
27
+ maxWidth: number;
28
+ };
29
+ title: {
30
+ height: number;
31
+ width: number;
32
+ text: string;
33
+ margin: number;
34
+ style: import("../../types").BaseTextStyle;
35
+ align: import("../../types").ChartAxisTitleAlignment;
36
+ maxRowCount: number;
37
+ rotation: number;
38
+ maxWidth: number;
39
+ html: boolean;
40
+ };
41
+ min?: number;
42
+ grid: {
43
+ enabled: boolean;
44
+ };
45
+ maxPadding: number;
46
+ ticks: {
47
+ pixelInterval?: number;
48
+ };
49
+ tickMarks: import("./types").PreparedAxisTickMarks;
50
+ position: "left" | "right" | "top" | "bottom";
51
+ plotIndex: number;
52
+ plotLines: import("./types").PreparedAxisPlotLine[];
53
+ plotBands: import("./types").PreparedAxisPlotBand[];
54
+ crosshair: Required<import("../../types").AxisCrosshair>;
55
+ })[];
56
+ }>;
57
+ export declare function useAxis(props: UseAxesProps): AxesState;
23
58
  export {};
@@ -3,6 +3,50 @@ import isEqual from 'lodash/isEqual';
3
3
  import { getWidthOccupiedByYAxis } from '../useChartDimensions/utils';
4
4
  import { getPreparedXAxis } from './x-axis';
5
5
  import { getPreparedYAxis } from './y-axis';
6
+ export async function getAxes(props) {
7
+ var _a, _b;
8
+ const { boundsHeight, height, preparedChart, legendConfig, preparedLegend, preparedSeries, width, xAxis, yAxis, } = props;
9
+ const seriesData = preparedSeries.filter((s) => s.visible);
10
+ const estimatedPreparedYAxis = await getPreparedYAxis({
11
+ height,
12
+ boundsHeight: height,
13
+ width,
14
+ seriesData,
15
+ yAxis,
16
+ });
17
+ const axesWidth = getWidthOccupiedByYAxis({ preparedAxis: estimatedPreparedYAxis });
18
+ const estimatedBoundsWidth = width - (axesWidth + preparedChart.margin.left + preparedChart.margin.right);
19
+ const preparedXAxis = await getPreparedXAxis({
20
+ xAxis,
21
+ width,
22
+ boundsWidth: estimatedBoundsWidth,
23
+ seriesData,
24
+ });
25
+ let estimatedBoundsHeight = boundsHeight !== null && boundsHeight !== void 0 ? boundsHeight : height;
26
+ if (preparedXAxis && typeof boundsHeight !== 'number') {
27
+ estimatedBoundsHeight =
28
+ height -
29
+ (preparedXAxis.title.height +
30
+ preparedXAxis.title.margin +
31
+ preparedXAxis.labels.margin +
32
+ preparedXAxis.labels.height +
33
+ (preparedXAxis.rangeSlider.enabled
34
+ ? preparedXAxis.rangeSlider.height + preparedXAxis.rangeSlider.margin
35
+ : 0) +
36
+ ((_a = legendConfig === null || legendConfig === void 0 ? void 0 : legendConfig.height) !== null && _a !== void 0 ? _a : 0) +
37
+ ((_b = preparedLegend === null || preparedLegend === void 0 ? void 0 : preparedLegend.margin) !== null && _b !== void 0 ? _b : 0) +
38
+ preparedChart.margin.top +
39
+ preparedChart.margin.bottom);
40
+ }
41
+ const preparedYAxis = await getPreparedYAxis({
42
+ height,
43
+ boundsHeight: estimatedBoundsHeight,
44
+ width,
45
+ seriesData,
46
+ yAxis,
47
+ });
48
+ return { xAxis: preparedXAxis, yAxis: preparedYAxis };
49
+ }
6
50
  export function useAxis(props) {
7
51
  const { boundsHeight, height, preparedChart, legendConfig, preparedLegend, preparedSeries, preparedSeriesOptions, width, xAxis, yAxis, } = props;
8
52
  const [axesState, setAxes] = React.useState({ xAxis: null, yAxis: [] });
@@ -16,48 +60,19 @@ export function useAxis(props) {
16
60
  return;
17
61
  }
18
62
  (async function () {
19
- var _a, _b;
20
63
  const currentRun = axesStateRunRef.current;
21
- const seriesData = preparedSeries.filter((s) => s.visible);
22
- const estimatedPreparedYAxis = await getPreparedYAxis({
64
+ const newStateValue = await getAxes({
65
+ boundsHeight,
23
66
  height,
24
- boundsHeight: height,
67
+ preparedChart,
68
+ legendConfig,
69
+ preparedLegend,
70
+ preparedSeries,
71
+ preparedSeriesOptions,
25
72
  width,
26
- seriesData,
27
- yAxis,
28
- });
29
- const axesWidth = getWidthOccupiedByYAxis({ preparedAxis: estimatedPreparedYAxis });
30
- const estimatedBoundsWidth = width - (axesWidth + preparedChart.margin.left + preparedChart.margin.right);
31
- const preparedXAxis = await getPreparedXAxis({
32
73
  xAxis,
33
- width,
34
- boundsWidth: estimatedBoundsWidth,
35
- seriesData,
36
- });
37
- let estimatedBoundsHeight = boundsHeight !== null && boundsHeight !== void 0 ? boundsHeight : height;
38
- if (preparedXAxis && typeof boundsHeight !== 'number') {
39
- estimatedBoundsHeight =
40
- height -
41
- (preparedXAxis.title.height +
42
- preparedXAxis.title.margin +
43
- preparedXAxis.labels.margin +
44
- preparedXAxis.labels.height +
45
- (preparedXAxis.rangeSlider.enabled
46
- ? preparedXAxis.rangeSlider.height + preparedXAxis.rangeSlider.margin
47
- : 0) +
48
- ((_a = legendConfig === null || legendConfig === void 0 ? void 0 : legendConfig.height) !== null && _a !== void 0 ? _a : 0) +
49
- ((_b = preparedLegend === null || preparedLegend === void 0 ? void 0 : preparedLegend.margin) !== null && _b !== void 0 ? _b : 0) +
50
- preparedChart.margin.top +
51
- preparedChart.margin.bottom);
52
- }
53
- const preparedYAxis = await getPreparedYAxis({
54
- height,
55
- boundsHeight: estimatedBoundsHeight,
56
- width,
57
- seriesData,
58
74
  yAxis,
59
75
  });
60
- const newStateValue = { xAxis: preparedXAxis, yAxis: preparedYAxis };
61
76
  if (axesStateRunRef.current === currentRun) {
62
77
  if (!isEqual(prevAxesStateValue.current, newStateValue)) {
63
78
  setAxes(newStateValue);
@@ -69,7 +84,7 @@ export function useAxis(props) {
69
84
  }, [
70
85
  boundsHeight,
71
86
  height,
72
- preparedChart.margin,
87
+ preparedChart,
73
88
  legendConfig,
74
89
  preparedLegend,
75
90
  preparedSeries,
@@ -81,10 +96,10 @@ export function useAxis(props) {
81
96
  const isAxesReady = axesStateReady.current;
82
97
  const result = React.useMemo(() => {
83
98
  if (isAxesReady) {
84
- return Object.assign(Object.assign({}, axesState), { setAxes });
99
+ return axesState;
85
100
  }
86
101
  prevAxesStateValue.current = { xAxis: null, yAxis: [] };
87
- return Object.assign(Object.assign({}, prevAxesStateValue.current), { setAxes });
102
+ return prevAxesStateValue.current;
88
103
  }, [isAxesReady, axesState]);
89
104
  return result;
90
105
  }
@@ -1,4 +1,3 @@
1
- import type React from 'react';
2
1
  import type { DashStyle } from '../../constants';
3
2
  import type { AxisCrosshair, AxisPlotBand, BaseTextStyle, ChartAxis, ChartAxisLabels, ChartAxisRangeSlider, ChartAxisTitleAlignment, ChartAxisType, DeepRequired, MeaningfulAny, PlotLayerPlacement } from '../../types';
4
3
  type PreparedAxisLabels = Omit<ChartAxisLabels, 'enabled' | 'padding' | 'style' | 'autoRotation'> & Required<Pick<ChartAxisLabels, 'enabled' | 'padding' | 'margin' | 'rotation' | 'html'>> & {
@@ -80,5 +79,4 @@ export type AxesState = {
80
79
  xAxis: PreparedXAxis | null;
81
80
  yAxis: PreparedYAxis[];
82
81
  };
83
- export type SetAxes = React.Dispatch<React.SetStateAction<AxesState>>;
84
82
  export {};
@@ -17,6 +17,10 @@ type ReturnValue = {
17
17
  xScale?: ChartScale;
18
18
  yScale?: (ChartScale | undefined)[];
19
19
  };
20
+ export declare const createScales: (args: Args) => {
21
+ xScale: import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleTime<number, number, never> | undefined;
22
+ yScale: (ChartScale | undefined)[];
23
+ };
20
24
  /**
21
25
  * Uses to create scales for axis related series
22
26
  */
@@ -7,7 +7,7 @@ import { createXScale } from './x-scale';
7
7
  import { createYScale } from './y-scale';
8
8
  export { createXScale } from './x-scale';
9
9
  export { createYScale } from './y-scale';
10
- const createScales = (args) => {
10
+ export const createScales = (args) => {
11
11
  const { boundsWidth, boundsHeight, isRangeSlider, rangeSliderState, series, split, xAxis, yAxis, zoomState, } = args;
12
12
  // For range slider: always use all series regardless of visibility so the slider domain
13
13
  // stays stable when the user hides/shows series via the legend.
@@ -11,7 +11,7 @@ type Args = {
11
11
  width: number;
12
12
  legendConfig: LegendConfig | undefined;
13
13
  };
14
- export declare const useChartDimensions: (args: Args) => {
14
+ export declare function getChartDimensions(args: Args): {
15
15
  boundsWidth: number;
16
16
  boundsHeight: number;
17
17
  };
@@ -1,4 +1,3 @@
1
- import React from 'react';
2
1
  import { isAxisRelatedSeries } from '../../utils';
3
2
  import { getBoundsWidth } from './utils';
4
3
  export { getBoundsWidth } from './utils';
@@ -46,34 +45,20 @@ const getLeftOffset = ({ preparedLegend, legendConfig, }) => {
46
45
  }
47
46
  return 0;
48
47
  };
49
- export const useChartDimensions = (args) => {
48
+ export function getChartDimensions(args) {
50
49
  const { height, margin, preparedLegend, preparedSeries, preparedXAxis, preparedYAxis, width, legendConfig, } = args;
51
- return React.useMemo(() => {
52
- if (!preparedLegend || !legendConfig) {
53
- return { boundsWidth: 0, boundsHeight: 0 };
54
- }
55
- const hasAxisRelatedSeries = preparedSeries.some(isAxisRelatedSeries);
56
- const boundsWidth = getBoundsWidth({ chartWidth: width, chartMargin: margin, preparedYAxis });
57
- const bottomOffset = getBottomOffset({
58
- hasAxisRelatedSeries,
59
- preparedLegend,
60
- preparedXAxis,
61
- legendConfig,
62
- });
63
- const topOffset = getTopOffset({ preparedLegend, legendConfig });
64
- const rightOffset = getRightOffset({ preparedLegend, legendConfig });
65
- const leftOffset = getLeftOffset({ preparedLegend, legendConfig });
66
- const boundsHeight = height - margin.top - margin.bottom - bottomOffset - topOffset;
67
- const adjustedBoundsWidth = boundsWidth - rightOffset - leftOffset;
68
- return { boundsWidth: adjustedBoundsWidth, boundsHeight };
69
- }, [
70
- height,
71
- margin,
50
+ const hasAxisRelatedSeries = preparedSeries.some(isAxisRelatedSeries);
51
+ const boundsWidth = getBoundsWidth({ chartWidth: width, chartMargin: margin, preparedYAxis });
52
+ const bottomOffset = getBottomOffset({
53
+ hasAxisRelatedSeries,
72
54
  preparedLegend,
73
- legendConfig,
74
- preparedSeries,
75
55
  preparedXAxis,
76
- preparedYAxis,
77
- width,
78
- ]);
79
- };
56
+ legendConfig,
57
+ });
58
+ const topOffset = getTopOffset({ preparedLegend, legendConfig });
59
+ const rightOffset = getRightOffset({ preparedLegend, legendConfig });
60
+ const leftOffset = getLeftOffset({ preparedLegend, legendConfig });
61
+ const boundsHeight = height - margin.top - margin.bottom - bottomOffset - topOffset;
62
+ const adjustedBoundsWidth = boundsWidth - rightOffset - leftOffset;
63
+ return { boundsWidth: adjustedBoundsWidth, boundsHeight };
64
+ }
@@ -7,7 +7,7 @@ type Props = {
7
7
  height: number;
8
8
  xScale?: ChartScale;
9
9
  yScale?: (ChartScale | undefined)[];
10
- split: PreparedSplit;
10
+ split: PreparedSplit | undefined;
11
11
  plotElement: SVGGElement | null;
12
12
  dispatcher: Dispatch<object>;
13
13
  boundsOffsetLeft: number;
@@ -1,38 +1,33 @@
1
1
  import type { ChartData } from '../../types';
2
- interface UseOriginalDataProps {
3
- seriesData: ChartData['series']['data'];
2
+ export declare function getNormalizedXAxis(props: {
4
3
  xAxis: ChartData['xAxis'];
5
- yAxis: ChartData['yAxis'];
6
- }
7
- export declare function useNormalizedOriginalData(props: UseOriginalDataProps): {
8
- normalizedSeriesData: import("../../types").ChartSeries[];
9
- normalizedXAxis: {
10
- categories: string[] | undefined;
11
- rangeSlider?: import("../../types").ChartAxisRangeSlider;
12
- crosshair?: import("../../types").AxisCrosshair;
13
- timestamps?: number[];
14
- type?: import("../../types").ChartAxisType;
15
- labels?: import("../../types").ChartAxisLabels;
16
- lineColor?: string;
17
- title?: import("../../types").ChartAxisTitle;
18
- min?: number;
19
- max?: number;
20
- grid?: {
21
- enabled?: boolean;
22
- };
23
- ticks?: {
24
- pixelInterval?: number;
25
- interval?: number | string;
26
- };
27
- maxPadding?: number;
28
- plotLines?: import("../../types").AxisPlotLine[];
29
- plotBands?: import("../../types").AxisPlotBand[];
30
- tickMarks?: import("../../types").ChartAxisTickMarks;
31
- visible?: boolean;
32
- order?: "sortAsc" | "sortDesc" | "reverse";
33
- startOnTick?: boolean;
34
- endOnTick?: boolean;
4
+ }): {
5
+ categories: string[] | undefined;
6
+ rangeSlider?: import("../../types").ChartAxisRangeSlider;
7
+ crosshair?: import("../../types").AxisCrosshair;
8
+ timestamps?: number[];
9
+ type?: import("../../types").ChartAxisType;
10
+ labels?: import("../../types").ChartAxisLabels;
11
+ lineColor?: string;
12
+ title?: import("../../types").ChartAxisTitle;
13
+ min?: number;
14
+ max?: number;
15
+ grid?: {
16
+ enabled?: boolean;
17
+ };
18
+ ticks?: {
19
+ pixelInterval?: number;
20
+ interval?: number | string;
35
21
  };
36
- normalizedYAxis: import("../../types").ChartYAxis[] | undefined;
22
+ maxPadding?: number;
23
+ plotLines?: import("../../types").AxisPlotLine[];
24
+ plotBands?: import("../../types").AxisPlotBand[];
25
+ tickMarks?: import("../../types").ChartAxisTickMarks;
26
+ visible?: boolean;
27
+ order?: "sortAsc" | "sortDesc" | "reverse";
28
+ startOnTick?: boolean;
29
+ endOnTick?: boolean;
37
30
  };
38
- export {};
31
+ export declare function getNormalizedYAxis(props: {
32
+ yAxis: ChartData['yAxis'];
33
+ }): import("../../types").ChartYAxis[] | undefined;
@@ -1,32 +1,21 @@
1
- import React from 'react';
2
- import { getAxisCategories, getSortedSeriesData } from '../../utils';
3
- export function useNormalizedOriginalData(props) {
4
- const normalizedSeriesData = React.useMemo(() => {
5
- return getSortedSeriesData({
6
- seriesData: props.seriesData,
7
- xAxis: props.xAxis,
8
- yAxis: props.yAxis,
1
+ import { getAxisCategories } from '../../utils';
2
+ export function getNormalizedXAxis(props) {
3
+ var _a;
4
+ let categories = (_a = props.xAxis) === null || _a === void 0 ? void 0 : _a.categories;
5
+ if (props.xAxis && props.xAxis.categories) {
6
+ categories = getAxisCategories(props.xAxis);
7
+ }
8
+ return Object.assign(Object.assign({}, props.xAxis), { categories });
9
+ }
10
+ export function getNormalizedYAxis(props) {
11
+ if (Array.isArray(props.yAxis) && props.yAxis.some((axis) => axis.categories)) {
12
+ return props.yAxis.map((axis) => {
13
+ let categories = axis.categories;
14
+ if (axis.categories) {
15
+ categories = getAxisCategories(axis);
16
+ }
17
+ return Object.assign(Object.assign({}, axis), { categories });
9
18
  });
10
- }, [props.seriesData, props.xAxis, props.yAxis]);
11
- const normalizedXAxis = React.useMemo(() => {
12
- var _a;
13
- let categories = (_a = props.xAxis) === null || _a === void 0 ? void 0 : _a.categories;
14
- if (props.xAxis && props.xAxis.categories) {
15
- categories = getAxisCategories(props.xAxis);
16
- }
17
- return Object.assign(Object.assign({}, props.xAxis), { categories });
18
- }, [props.xAxis]);
19
- const normalizedYAxis = React.useMemo(() => {
20
- if (Array.isArray(props.yAxis) && props.yAxis.some((axis) => axis.categories)) {
21
- return props.yAxis.map((axis) => {
22
- let categories = axis.categories;
23
- if (axis.categories) {
24
- categories = getAxisCategories(axis);
25
- }
26
- return Object.assign(Object.assign({}, axis), { categories });
27
- });
28
- }
29
- return props.yAxis;
30
- }, [props.yAxis]);
31
- return { normalizedSeriesData, normalizedXAxis, normalizedYAxis };
19
+ }
20
+ return props.yAxis;
32
21
  }
@@ -1,16 +1,12 @@
1
1
  import type { ChartData } from '../../types';
2
- import type { OnLegendItemClick, PreparedLegend, PreparedSeries } from './types';
3
- type Args = {
4
- colors: string[];
5
- legend: ChartData['legend'];
6
- originalSeriesData: ChartData['series']['data'];
7
- seriesData: ChartData['series']['data'];
8
- seriesOptions: ChartData['series']['options'];
9
- preparedLegend?: PreparedLegend;
10
- };
11
- export declare const useSeries: (args: Args) => {
12
- preparedLegend: PreparedLegend | null;
2
+ import type { PreparedLegend, PreparedSeries } from './types';
3
+ export declare const getVisibleSeries: ({ preparedSeries, activeLegendItems, }: {
13
4
  preparedSeries: PreparedSeries[];
14
- handleLegendItemClick: OnLegendItemClick;
15
- };
16
- export {};
5
+ activeLegendItems: string[];
6
+ }) => PreparedSeries[];
7
+ export declare const getPreparedSeries: ({ seriesData, seriesOptions, colors, preparedLegend, }: {
8
+ seriesData: ChartData["series"]["data"];
9
+ seriesOptions: ChartData["series"]["options"];
10
+ colors: string[];
11
+ preparedLegend?: PreparedLegend | null;
12
+ }) => Promise<PreparedSeries[]>;
@@ -1,21 +1,15 @@
1
- import React from 'react';
2
1
  import { group, scaleOrdinal } from 'd3';
3
2
  import { getSeriesNames } from '../../utils';
4
- import { usePrevious } from '../usePrevious';
5
- import { getPreparedLegend } from './prepare-legend';
6
3
  import { prepareSeries } from './prepareSeries';
7
- import { getActiveLegendItems, getAllLegendItems } from './utils';
8
- const useVisibleSeries = ({ preparedSeries, activeLegendItems, }) => {
9
- return React.useMemo(() => {
10
- return preparedSeries.map((singleSeries) => {
11
- if (singleSeries.legend.enabled) {
12
- return Object.assign(Object.assign({}, singleSeries), { visible: activeLegendItems.includes(singleSeries.legend.groupId) });
13
- }
14
- return singleSeries;
15
- });
16
- }, [preparedSeries, activeLegendItems]);
4
+ export const getVisibleSeries = ({ preparedSeries, activeLegendItems, }) => {
5
+ return preparedSeries.map((singleSeries) => {
6
+ if (singleSeries.legend.enabled) {
7
+ return Object.assign(Object.assign({}, singleSeries), { visible: activeLegendItems.includes(singleSeries.legend.groupId) });
8
+ }
9
+ return singleSeries;
10
+ });
17
11
  };
18
- const getPreparedSeries = async ({ seriesData, seriesOptions, colors, preparedLegend, }) => {
12
+ export const getPreparedSeries = async ({ seriesData, seriesOptions, colors, preparedLegend, }) => {
19
13
  const seriesNames = getSeriesNames(seriesData);
20
14
  const colorScale = scaleOrdinal(seriesNames, colors);
21
15
  const groupedSeries = group(seriesData, (item) => item.type);
@@ -37,69 +31,3 @@ const getPreparedSeries = async ({ seriesData, seriesOptions, colors, preparedLe
37
31
  }
38
32
  return acc;
39
33
  };
40
- export const useSeries = (args) => {
41
- const { legend, originalSeriesData, seriesData, seriesOptions, colors, preparedLegend: preparedLegendProps = null, } = args;
42
- const [preparedLegend, setPreparedLegend] = React.useState(preparedLegendProps);
43
- const legendRunRef = React.useRef(0);
44
- React.useEffect(() => {
45
- if (!preparedLegendProps) {
46
- const currentRun = ++legendRunRef.current;
47
- getPreparedLegend({ legend, series: seriesData }).then((value) => {
48
- if (legendRunRef.current === currentRun) {
49
- setPreparedLegend(value);
50
- }
51
- });
52
- }
53
- }, [legend, preparedLegendProps, seriesData]);
54
- const [preparedSeries, setPreparedSeries] = React.useState([]);
55
- const [activeLegendItems, setActiveLegendItems] = React.useState(getActiveLegendItems(preparedSeries));
56
- const seriesRunRef = React.useRef(0);
57
- React.useEffect(() => {
58
- const currentRun = ++seriesRunRef.current;
59
- (async () => {
60
- const items = await getPreparedSeries({
61
- seriesData,
62
- seriesOptions,
63
- preparedLegend,
64
- colors,
65
- });
66
- if (seriesRunRef.current === currentRun) {
67
- setPreparedSeries(items);
68
- setActiveLegendItems(getActiveLegendItems(items));
69
- }
70
- })();
71
- }, [seriesData, seriesOptions, preparedLegend, colors]);
72
- const prevOriginalSeriesData = usePrevious(originalSeriesData);
73
- const chartSeries = useVisibleSeries({ preparedSeries, activeLegendItems });
74
- const handleLegendItemClick = React.useCallback(({ id, metaKey }) => {
75
- const allItems = getAllLegendItems(preparedSeries);
76
- const onlyItemSelected = activeLegendItems.length === 1 && activeLegendItems.includes(id);
77
- let nextActiveLegendItems;
78
- if (metaKey && activeLegendItems.includes(id)) {
79
- nextActiveLegendItems = activeLegendItems.filter((item) => item !== id);
80
- }
81
- else if (metaKey && !activeLegendItems.includes(id)) {
82
- nextActiveLegendItems = activeLegendItems.concat(id);
83
- }
84
- else if (onlyItemSelected && allItems.length === 1) {
85
- nextActiveLegendItems = [];
86
- }
87
- else if (onlyItemSelected) {
88
- nextActiveLegendItems = allItems;
89
- }
90
- else {
91
- nextActiveLegendItems = [id];
92
- }
93
- setActiveLegendItems(nextActiveLegendItems);
94
- }, [preparedSeries, activeLegendItems]);
95
- React.useEffect(() => {
96
- if (prevOriginalSeriesData !== originalSeriesData) {
97
- setActiveLegendItems(getActiveLegendItems(preparedSeries));
98
- }
99
- }, [originalSeriesData, prevOriginalSeriesData, preparedSeries]);
100
- return {
101
- preparedLegend,
102
- preparedSeries: chartSeries,
103
- handleLegendItemClick,
104
- };
105
- };
@@ -40,6 +40,10 @@ type Args = {
40
40
  yScale?: (ChartScale | undefined)[];
41
41
  zoomState?: Partial<ZoomState>;
42
42
  };
43
+ export declare function getShapes(args: Args): Promise<{
44
+ shapes: React.ReactElement<any, string | React.JSXElementConstructor<any>>[];
45
+ shapesData: ShapeData[];
46
+ }>;
43
47
  export declare const useShapes: (args: Args) => {
44
48
  shapes: React.ReactElement<any, string | React.JSXElementConstructor<any>>[];
45
49
  shapesData: ShapeData[];