@oliasoft-open-source/charts-library 2.17.6 → 2.18.0-beta-2

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 (86) hide show
  1. package/dist/index.js +40398 -0
  2. package/dist/style.css +177 -0
  3. package/package.json +26 -34
  4. package/index.js +0 -14
  5. package/release-notes.md +0 -310
  6. package/src/assets/icons/line-and-point.svg +0 -1
  7. package/src/assets/icons/line-only.svg +0 -1
  8. package/src/assets/icons/list-hide.svg +0 -1
  9. package/src/assets/icons/point-only.svg +0 -1
  10. package/src/components/bar-chart/bar-chart-prop-types.js +0 -209
  11. package/src/components/bar-chart/bar-chart.interface.ts +0 -91
  12. package/src/components/bar-chart/bar-chart.jsx +0 -247
  13. package/src/components/bar-chart/bar-chart.module.less +0 -61
  14. package/src/components/bar-chart/get-bar-chart-data-labels.js +0 -42
  15. package/src/components/bar-chart/get-bar-chart-scales.js +0 -123
  16. package/src/components/bar-chart/get-bar-chart-tooltips.js +0 -102
  17. package/src/components/controls/axes-options/axes-options.jsx +0 -270
  18. package/src/components/line-chart/constants/default-translations.js +0 -24
  19. package/src/components/line-chart/constants/line-chart-consts.js +0 -12
  20. package/src/components/line-chart/controls/axes-options/action-types.js +0 -5
  21. package/src/components/line-chart/controls/axes-options/axes-options-form-state.js +0 -97
  22. package/src/components/line-chart/controls/controls.jsx +0 -174
  23. package/src/components/line-chart/controls/controls.module.less +0 -12
  24. package/src/components/line-chart/controls/drag-options.jsx +0 -112
  25. package/src/components/line-chart/controls/legend-options.jsx +0 -36
  26. package/src/components/line-chart/controls/line-options.jsx +0 -64
  27. package/src/components/line-chart/hooks/use-chart-functions.js +0 -257
  28. package/src/components/line-chart/hooks/use-chart-options.js +0 -181
  29. package/src/components/line-chart/hooks/use-chart-plugins.js +0 -35
  30. package/src/components/line-chart/hooks/use-toggle-handler.js +0 -33
  31. package/src/components/line-chart/initialize/config.js +0 -23
  32. package/src/components/line-chart/initialize/initialize-line-chart.js +0 -25
  33. package/src/components/line-chart/line-chart-prop-types.js +0 -292
  34. package/src/components/line-chart/line-chart.interface.ts +0 -121
  35. package/src/components/line-chart/line-chart.jsx +0 -161
  36. package/src/components/line-chart/line-chart.module.less +0 -77
  37. package/src/components/line-chart/plugins/chart-area-text-plugin.js +0 -246
  38. package/src/components/line-chart/plugins/line-chart.minor-gridlines-plugin.js +0 -88
  39. package/src/components/line-chart/plugins/plugin-constants.js +0 -11
  40. package/src/components/line-chart/state/action-types.js +0 -12
  41. package/src/components/line-chart/state/initial-state.js +0 -89
  42. package/src/components/line-chart/state/line-chart-reducer.js +0 -100
  43. package/src/components/line-chart/state/manage-state-in-local-storage.js +0 -86
  44. package/src/components/line-chart/state/use-chart-state.js +0 -141
  45. package/src/components/line-chart/utils/axis-scales/axis-scales.js +0 -165
  46. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-condition.js +0 -13
  47. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-data.js +0 -20
  48. package/src/components/line-chart/utils/datalabels-alignment/get-datalabels-position.js +0 -25
  49. package/src/components/line-chart/utils/generate-line-chart-datasets.js +0 -114
  50. package/src/components/line-chart/utils/get-axes-ranges-from-chart.js +0 -13
  51. package/src/components/line-chart/utils/get-line-chart-data-labels.js +0 -21
  52. package/src/components/line-chart/utils/get-line-chart-scales.js +0 -120
  53. package/src/components/line-chart/utils/get-line-chart-tooltips.js +0 -94
  54. package/src/components/line-chart/utils/line-chart-utils.js +0 -77
  55. package/src/components/line-chart/utils/translations/get-translations.js +0 -17
  56. package/src/components/pie-chart/pie-chart-prop-types.js +0 -111
  57. package/src/components/pie-chart/pie-chart-utils.js +0 -32
  58. package/src/components/pie-chart/pie-chart.interface.ts +0 -61
  59. package/src/components/pie-chart/pie-chart.jsx +0 -450
  60. package/src/components/pie-chart/pie-chart.module.less +0 -61
  61. package/src/components/scatter-chart/scatter-chart.intefrace.ts +0 -33
  62. package/src/components/scatter-chart/scatter-chart.jsx +0 -21
  63. package/src/components/scatter-chart/scatter-chart.module.less +0 -4
  64. package/src/helpers/chart-border-plugin.js +0 -19
  65. package/src/helpers/chart-consts.js +0 -64
  66. package/src/helpers/chart-interface.ts +0 -94
  67. package/src/helpers/chart-utils.js +0 -182
  68. package/src/helpers/container.jsx +0 -60
  69. package/src/helpers/disabled-context.js +0 -8
  70. package/src/helpers/enums.js +0 -96
  71. package/src/helpers/get-chart-annotation.js +0 -106
  72. package/src/helpers/get-custom-legend-plugin-example.js +0 -80
  73. package/src/helpers/get-draggableData.js +0 -32
  74. package/src/helpers/range/estimate-data-series-have-close-values.js +0 -54
  75. package/src/helpers/range/range.js +0 -100
  76. package/src/helpers/text.js +0 -6
  77. package/src/style/external.less +0 -4
  78. package/src/style/fonts/lato/Lato-Bold.woff2 +0 -0
  79. package/src/style/fonts/lato/Lato-BoldItalic.woff2 +0 -0
  80. package/src/style/fonts/lato/Lato-Italic.woff2 +0 -0
  81. package/src/style/fonts/lato/Lato-Regular.woff2 +0 -0
  82. package/src/style/fonts.less +0 -27
  83. package/src/style/global.less +0 -43
  84. package/src/style/reset/reset.less +0 -28
  85. package/src/style/shared.less +0 -11
  86. package/src/style/variables.less +0 -91
@@ -1,174 +0,0 @@
1
- import React from 'react';
2
- import { Button, Text, Tooltip } from '@oliasoft-open-source/react-ui-library';
3
- import {
4
- RiFileDownloadLine,
5
- RiLineChartLine,
6
- RiTableLine,
7
- } from 'react-icons/ri';
8
- import PropTypes from 'prop-types';
9
- import { LineOptions } from './line-options';
10
- import { DragOptions } from './drag-options';
11
- import { LegendOptions } from './legend-options';
12
- import styles from './controls.module.less';
13
- import { useToggleHandlers } from '../hooks/use-toggle-handler';
14
- import { useChartFunctions } from '../hooks/use-chart-functions';
15
- import { AxesOptions } from '../../controls/axes-options/axes-options';
16
-
17
- const Controls = ({
18
- props: { headerComponent, subheaderComponent, table },
19
- chartRef,
20
- state,
21
- options,
22
- dispatch,
23
- generatedDatasets,
24
- translations,
25
- }) => {
26
- const {
27
- enableDragPoints,
28
- initialAxesRanges,
29
- legendEnabled,
30
- lineEnabled,
31
- panEnabled,
32
- pointsEnabled,
33
- showTable,
34
- zoomEnabled,
35
- } = state;
36
- const { dragData } = options;
37
- const {
38
- onToggleLegend,
39
- onToggleLine,
40
- onTogglePan,
41
- onTogglePoints,
42
- onToggleTable,
43
- onToggleZoom,
44
- onToggleDragPoints,
45
- onDisableDragOptions,
46
- } = useToggleHandlers(dispatch);
47
- const {
48
- handleDownload,
49
- controlsAxes,
50
- controlsAxesLabels,
51
- onResetAxes,
52
- onUpdateAxes,
53
- } = useChartFunctions({
54
- chartRef,
55
- state,
56
- options,
57
- dispatch,
58
- generatedDatasets,
59
- });
60
-
61
- return (
62
- <>
63
- <div className={styles.controls}>
64
- {!!options.title && <Text bold>{options.title}</Text>}
65
- {headerComponent}
66
- <div className={styles.buttons}>
67
- {!showTable && (
68
- <>
69
- <AxesOptions
70
- initialAxesRanges={initialAxesRanges}
71
- axes={controlsAxes}
72
- controlsAxesLabels={controlsAxesLabels}
73
- onUpdateAxes={onUpdateAxes}
74
- onResetAxes={onResetAxes}
75
- depthType={options.depthType}
76
- translations={translations}
77
- />
78
- <LineOptions
79
- lineEnabled={lineEnabled}
80
- pointsEnabled={pointsEnabled}
81
- onToggleLine={onToggleLine}
82
- onTogglePoints={onTogglePoints}
83
- translations={translations}
84
- />
85
- <LegendOptions
86
- legendEnabled={legendEnabled}
87
- onToggleLegend={onToggleLegend}
88
- translations={translations}
89
- />
90
- <Tooltip text={translations.downloadAsPNG} placement="bottom-end">
91
- <Button
92
- small
93
- basic
94
- colored="muted"
95
- round
96
- icon={<RiFileDownloadLine />}
97
- onClick={handleDownload}
98
- />
99
- </Tooltip>
100
- <DragOptions
101
- panEnabled={panEnabled}
102
- zoomEnabled={zoomEnabled}
103
- onTogglePan={onTogglePan}
104
- onToggleZoom={onToggleZoom}
105
- enableDragPoints={enableDragPoints}
106
- isDragDataAllowed={dragData.enableDragData}
107
- onToggleDragPoints={onToggleDragPoints}
108
- onDisableDragOptions={onDisableDragOptions}
109
- translations={translations}
110
- />
111
- </>
112
- )}
113
-
114
- {table ? (
115
- <Tooltip
116
- text={showTable ? translations.showChart : translations.showTable}
117
- placement="bottom-end"
118
- >
119
- <Button
120
- small
121
- basic
122
- colored="muted"
123
- round
124
- icon={showTable ? <RiLineChartLine /> : <RiTableLine />}
125
- onClick={onToggleTable}
126
- />
127
- </Tooltip>
128
- ) : null}
129
- </div>
130
- </div>
131
- {subheaderComponent}
132
- </>
133
- );
134
- };
135
-
136
- Controls.defaultProps = {
137
- props: {
138
- headerComponent: null,
139
- subheaderComponent: null,
140
- table: null,
141
- },
142
- state: {
143
- initialAxesRanges: [],
144
- },
145
- options: {},
146
- };
147
-
148
- Controls.propTypes = {
149
- props: PropTypes.shape({
150
- headerComponent: PropTypes.node,
151
- subheaderComponent: PropTypes.node,
152
- table: PropTypes.node,
153
- }),
154
- chartRef: PropTypes.object.isRequired,
155
- state: PropTypes.shape({
156
- enableDragPoints: PropTypes.bool,
157
- initialAxesRanges: PropTypes.arrayOf(PropTypes.shape({})),
158
- legendEnabled: PropTypes.bool,
159
- lineEnabled: PropTypes.bool,
160
- panEnabled: PropTypes.bool,
161
- pointsEnabled: PropTypes.bool,
162
- showTable: PropTypes.bool,
163
- zoomEnabled: PropTypes.bool,
164
- }),
165
- options: PropTypes.shape({
166
- dragData: PropTypes.shape({
167
- enableDragData: PropTypes.bool,
168
- }),
169
- }),
170
- dispatch: PropTypes.func.isRequired,
171
- generatedDatasets: PropTypes.arrayOf(PropTypes.object).isRequired,
172
- };
173
-
174
- export default Controls;
@@ -1,12 +0,0 @@
1
- .controls {
2
- display: flex;
3
- align-items: center;
4
- flex-wrap: wrap;
5
- gap: 8px;
6
- }
7
-
8
- .buttons {
9
- display: flex;
10
- margin-left: auto;
11
- gap: 4px;
12
- }
@@ -1,112 +0,0 @@
1
- import React, { useMemo } from 'react';
2
- import PropTypes from 'prop-types';
3
- import { Flex, Menu, Text } from '@oliasoft-open-source/react-ui-library';
4
- import { RiDragMove2Line, RiForbidLine, RiZoomInLine } from 'react-icons/ri';
5
- import { TbHandStop } from 'react-icons/tb';
6
-
7
- export const DragOptions = ({
8
- onTogglePan,
9
- onToggleZoom,
10
- panEnabled,
11
- zoomEnabled,
12
- enableDragPoints,
13
- isDragDataAllowed,
14
- onToggleDragPoints,
15
- onDisableDragOptions,
16
- translations: {
17
- dragToZoom,
18
- doubleClickToReset,
19
- dragToPan,
20
- orDoubleClickToCanvas,
21
- dragToMovePoints,
22
- dragDisabled,
23
- },
24
- }) => {
25
- const options = useMemo(
26
- () => [
27
- {
28
- buttonLabel: dragToZoom,
29
- label: (
30
- <Flex direction="column">
31
- <Text>Drag to zoom</Text>
32
- <Text small muted>
33
- {doubleClickToReset}
34
- </Text>
35
- </Flex>
36
- ),
37
- icon: <RiZoomInLine />,
38
- selected: zoomEnabled,
39
- onClick: onToggleZoom,
40
- },
41
- {
42
- buttonLabel: dragToPan,
43
- label: (
44
- <Flex direction="column">
45
- <Text>Drag to pan</Text>
46
- <Text small muted>
47
- {orDoubleClickToCanvas}
48
- </Text>
49
- </Flex>
50
- ),
51
- icon: <RiDragMove2Line />,
52
- selected: panEnabled,
53
- onClick: onTogglePan,
54
- },
55
- ...(isDragDataAllowed
56
- ? [
57
- {
58
- label: dragToMovePoints,
59
- icon: <TbHandStop />,
60
- selected: enableDragPoints,
61
- type: 'Option',
62
- onClick: onToggleDragPoints,
63
- },
64
- ]
65
- : []),
66
- {
67
- label: dragDisabled,
68
- icon: <RiForbidLine />,
69
- selected: !zoomEnabled && !panEnabled && !enableDragPoints,
70
- onClick: onDisableDragOptions,
71
- },
72
- ],
73
- [zoomEnabled, panEnabled, enableDragPoints, isDragDataAllowed],
74
- );
75
-
76
- const selectedOption = options.find((option) => option.selected);
77
- const optionsWithDragPoints = options.map(
78
- ({ icon, label, onClick, selected }) => ({
79
- icon,
80
- label,
81
- type: 'Option',
82
- onClick,
83
- disabled: selected,
84
- }),
85
- );
86
-
87
- return (
88
- <Menu
89
- menu={{
90
- label: selectedOption.buttonLabel || selectedOption.label,
91
- sections: optionsWithDragPoints,
92
- trigger: 'DropDownButton',
93
- small: true,
94
- }}
95
- />
96
- );
97
- };
98
-
99
- DragOptions.defaultProps = {
100
- enableDragPoints: false,
101
- };
102
-
103
- DragOptions.propTypes = {
104
- onTogglePan: PropTypes.func.isRequired,
105
- onToggleZoom: PropTypes.func.isRequired,
106
- panEnabled: PropTypes.bool.isRequired,
107
- zoomEnabled: PropTypes.bool.isRequired,
108
- enableDragPoints: PropTypes.bool,
109
- isDragDataAllowed: PropTypes.bool.isRequired,
110
- onToggleDragPoints: PropTypes.func.isRequired,
111
- onDisableDragOptions: PropTypes.func.isRequired,
112
- };
@@ -1,36 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import { Button, Icon, Tooltip } from '@oliasoft-open-source/react-ui-library';
4
- import { RiListUnordered } from 'react-icons/ri';
5
- import listHideIcon from '../../../assets/icons/list-hide.svg';
6
-
7
- export const LegendOptions = ({
8
- legendEnabled,
9
- onToggleLegend,
10
- translations: { hideLegend, showLegend },
11
- }) => {
12
- const tooltipText = legendEnabled ? hideLegend : showLegend;
13
- const icon = legendEnabled ? (
14
- <RiListUnordered />
15
- ) : (
16
- <Icon icon={listHideIcon} />
17
- );
18
-
19
- return (
20
- <Tooltip text={tooltipText} placement="bottom">
21
- <Button
22
- small
23
- basic
24
- colored="muted"
25
- round
26
- icon={icon}
27
- onClick={onToggleLegend}
28
- />
29
- </Tooltip>
30
- );
31
- };
32
-
33
- LegendOptions.propTypes = {
34
- legendEnabled: PropTypes.bool.isRequired,
35
- onToggleLegend: PropTypes.func.isRequired,
36
- };
@@ -1,64 +0,0 @@
1
- import React from 'react';
2
- import { Button, Icon, Tooltip } from '@oliasoft-open-source/react-ui-library';
3
- import PropTypes from 'prop-types';
4
- import lineOnlyIcon from '../../../assets/icons/line-only.svg';
5
- import pointOnlyIcon from '../../../assets/icons/point-only.svg';
6
- import lineAndPointIcon from '../../../assets/icons/line-and-point.svg';
7
-
8
- export const LineOptions = ({
9
- lineEnabled,
10
- onToggleLine,
11
- onTogglePoints,
12
- pointsEnabled,
13
- translations,
14
- }) => {
15
- const options = [
16
- {
17
- label: translations.pointsLines,
18
- icon: <Icon icon={lineAndPointIcon} />,
19
- selected: pointsEnabled && lineEnabled,
20
- onClick: () => {
21
- onTogglePoints();
22
- },
23
- },
24
- {
25
- label: translations.linesOnly,
26
- icon: <Icon icon={lineOnlyIcon} />,
27
- selected: !pointsEnabled && lineEnabled,
28
- onClick: () => {
29
- onTogglePoints();
30
- onToggleLine();
31
- },
32
- },
33
- {
34
- label: translations.pointsOnly,
35
- icon: <Icon icon={pointOnlyIcon} />,
36
- selected: pointsEnabled && !lineEnabled,
37
- onClick: () => {
38
- onToggleLine();
39
- },
40
- },
41
- ];
42
-
43
- const selectedOption = options.find((option) => option.selected);
44
-
45
- return (
46
- <Tooltip text={selectedOption.label} placement="bottom">
47
- <Button
48
- small
49
- basic
50
- colored="muted"
51
- round
52
- icon={selectedOption.icon}
53
- onClick={selectedOption.onClick}
54
- />
55
- </Tooltip>
56
- );
57
- };
58
-
59
- LineOptions.propTypes = {
60
- lineEnabled: PropTypes.bool.isRequired,
61
- onToggleLine: PropTypes.func.isRequired,
62
- onTogglePoints: PropTypes.func.isRequired,
63
- pointsEnabled: PropTypes.bool.isRequired,
64
- };
@@ -1,257 +0,0 @@
1
- import { useCallback } from 'react';
2
- import { triggerBase64Download } from 'react-base64-downloader';
3
- import { AxisType, Key, PanZoomMode } from '../../../helpers/enums';
4
- import { getChartFileName } from '../../../helpers/chart-utils';
5
- import { generateAxisId } from '../utils/line-chart-utils';
6
- import { RESET_AXES_RANGES, UPDATE_AXES_RANGES } from '../state/action-types';
7
-
8
- /**
9
- * Custom hook that encapsulates the chart-related functions and their dependencies
10
- * @param {Object} chartRef - Chart reference
11
- * @param {Object} state - The component state
12
- * @param {Object} options - Chart options
13
- * @param {Function} dispatch - Dispatch function from useReducer
14
- * @param {Array} generatedDatasets - Chart datasets
15
- * @returns {Object} An object containing the chart-related functions
16
- */
17
- export const useChartFunctions = ({
18
- chartRef,
19
- state,
20
- options,
21
- dispatch,
22
- generatedDatasets,
23
- }) => {
24
- const {
25
- annotations,
26
- interactions: { onLegendClick, onPointHover, onPointUnhover },
27
- additionalAxesOptions,
28
- axes,
29
- } = options;
30
-
31
- /**
32
- * Handles legend click events
33
- * @param {Event} e - Click event
34
- * @param {Object} legendItem - Legend item data
35
- */
36
- const legendClick = useCallback(
37
- (e, legendItem) => {
38
- const { datasetIndex } = legendItem;
39
- const chartInstance = chartRef.current;
40
- const { datasets } = chartInstance.data;
41
- const dataset = datasets[datasetIndex];
42
- const meta = chartInstance.getDatasetMeta(datasetIndex);
43
- meta.hidden = meta.hidden === null ? !dataset.hidden : null;
44
-
45
- if (annotations.controlAnnotation && dataset.isAnnotation) {
46
- const { annotationIndex } = dataset;
47
- dispatch({ type: 'TOGGLE_ANNOTATION', payload: { annotationIndex } });
48
- }
49
-
50
- // Show/hide entire display group
51
- if (dataset.displayGroup) {
52
- datasets.forEach((ds, ix) => {
53
- if (ds.displayGroup !== dataset.displayGroup) return;
54
- chartInstance.getDatasetMeta(ix).hidden = meta.hidden;
55
- });
56
- }
57
-
58
- if (onLegendClick) {
59
- onLegendClick(legendItem?.text, legendItem.hidden);
60
- }
61
-
62
- chartInstance.update();
63
- },
64
- [onLegendClick, annotations],
65
- );
66
-
67
- /**
68
- * Resets the chart zoom and updates the axes ranges
69
- * @returns {Function} Memoized resetZoom function
70
- */
71
- const resetZoom = useCallback(() => {
72
- const chartInstance = chartRef.current;
73
- chartInstance.resetZoom();
74
- dispatch({ type: 'RESET_AXES_RANGES' });
75
- }, [chartRef]);
76
-
77
- /**
78
- * Handles the hover event on chart points
79
- * @param {Object} hoveredPoint - Currently hovered point
80
- * @param {Function} setHoveredPoint - Setter function for hoveredPoint state
81
- * @returns {Function} Memoized onHover function
82
- */
83
- const onHover = useCallback((hoveredPoint, setHoveredPoint) => {
84
- return (evt, hoveredItems) => {
85
- if (!hoveredItems?.length && onPointUnhover && hoveredPoint) {
86
- setHoveredPoint(null);
87
- onPointUnhover(evt);
88
- }
89
-
90
- if (hoveredItems?.length && onPointHover) {
91
- const { index, datasetIndex } = hoveredItems[0];
92
- const dataset = generatedDatasets[datasetIndex];
93
- const point = dataset?.data[index];
94
-
95
- if (point && hoveredPoint !== point) {
96
- setHoveredPoint(point);
97
- onPointHover(evt, datasetIndex, index, generatedDatasets);
98
- }
99
- }
100
- };
101
- }, []);
102
-
103
- /**
104
- * Handles the download of the chart as an image
105
- * @returns {Function} Memoized handleDownload function
106
- */
107
- const handleDownload = useCallback(() => {
108
- const chart = chartRef.current;
109
-
110
- // Add temporary canvas background
111
- const { ctx } = chart;
112
- ctx.save();
113
- ctx.globalCompositeOperation = 'destination-over';
114
- ctx.fillStyle = 'white';
115
- ctx.fillRect(0, 0, chart.width, chart.height);
116
- ctx.restore();
117
-
118
- const base64Image = chart.toBase64Image();
119
- const fileName = getChartFileName(state.axes);
120
-
121
- triggerBase64Download(base64Image, fileName);
122
- }, [chartRef, state.axes]);
123
-
124
- /**
125
- * Handles the key down event for the chart
126
- * @returns {Function} Memoized handleKeyDown function
127
- */
128
- const handleKeyDown = useCallback(
129
- (evt) => {
130
- if (evt.key === Key.Shift) {
131
- const chart = chartRef.current;
132
- chart.config.options.plugins.zoom.zoom.mode = PanZoomMode.Y;
133
- chart.config.options.plugins.zoom.pan.mode = PanZoomMode.Y;
134
- chart.update();
135
- }
136
- },
137
- [chartRef],
138
- );
139
-
140
- /**
141
- * Handles the key up event for the chart
142
- * @returns {Function} Memoized handleKeyUp function
143
- */
144
- const handleKeyUp = useCallback(
145
- (evt) => {
146
- if (evt.key === Key.Shift) {
147
- const chart = chartRef.current;
148
- chart.config.options.plugins.zoom.zoom.mode = PanZoomMode.Z;
149
- chart.config.options.plugins.zoom.pan.mode = PanZoomMode.Z;
150
- chart.update();
151
- }
152
- },
153
- [chartRef],
154
- );
155
-
156
- /**
157
- * Get the controls axes based on the state and additionalAxesOptions.
158
- * @returns {Array} Array of controls axes.
159
- */
160
- const controlsAxes = state.axes.map((axis, i) => {
161
- const axisType = i ? AxisType.Y : AxisType.X; // only first element is 'x' - rest is 'y'
162
- const min = axis.min ?? additionalAxesOptions?.range?.[axisType]?.min;
163
- const max = axis.max ?? additionalAxesOptions?.range?.[axisType]?.max;
164
- return {
165
- ...axis,
166
- // only add min and max properties if they are defined:
167
- ...(min ? { min } : {}),
168
- ...(max ? { max } : {}),
169
- };
170
- });
171
-
172
- /**
173
- * Get the controls axes labels based on the propsAxes.
174
- * @param {Object} propsAxes - Props axes object.
175
- * @returns {Array} Array of controls axes labels.
176
- */
177
- const getControlsAxesLabels = useCallback(
178
- (propsAxes) => {
179
- if (!Object.keys(propsAxes)?.length) {
180
- return [];
181
- }
182
-
183
- const getAxesLabels = (axes, axisType) => {
184
- if (!axes[axisType] || !axes[axisType]?.length) {
185
- return [];
186
- } else {
187
- return axes[axisType].map((axisObj, index) => {
188
- return {
189
- id: generateAxisId(axisType, index, axes[axisType].length > 1),
190
- label: axisObj?.label || '',
191
- };
192
- });
193
- }
194
- };
195
-
196
- const axesLabels = [
197
- ...getAxesLabels(propsAxes, AxisType.X),
198
- ...getAxesLabels(propsAxes, AxisType.Y),
199
- ];
200
- return axesLabels;
201
- },
202
- [axes],
203
- );
204
-
205
- /**
206
- * Update axes ranges from the chart.
207
- * @param {Object} chart - Chart instance.
208
- */
209
- const updateAxesRangesFromChart = useCallback(() => {
210
- const { scales = {} } = chartRef?.current || {};
211
- const axes = Object.entries(scales).map(([key, { min, max }]) => {
212
- return {
213
- id: key,
214
- min: min ?? 0,
215
- max: max ?? 0,
216
- };
217
- });
218
- dispatch({
219
- type: UPDATE_AXES_RANGES,
220
- payload: { axes },
221
- });
222
- }, [axes]);
223
-
224
- /**
225
- * Handler for resetting axes ranges.
226
- * @type {Function}
227
- */
228
- const onResetAxes = useCallback(() => {
229
- // Dispatch the RESET_AXES_RANGES action
230
- dispatch({ type: RESET_AXES_RANGES });
231
- }, []);
232
-
233
- /**
234
- * Handler for updating axes ranges.
235
- * @param {Object} axesInfo - Object containing axes information.
236
- * @param {Array} axesInfo.axes - Array of axes to update.
237
- * @type {Function}
238
- */
239
- const onUpdateAxes = useCallback(({ axes }) => {
240
- // Dispatch the UPDATE_AXES_RANGES action with payload
241
- dispatch({ type: UPDATE_AXES_RANGES, payload: { axes } });
242
- }, []);
243
-
244
- return {
245
- legendClick,
246
- resetZoom,
247
- onHover,
248
- handleDownload,
249
- handleKeyDown,
250
- handleKeyUp,
251
- controlsAxes,
252
- controlsAxesLabels: getControlsAxesLabels(axes),
253
- updateAxesRangesFromChart,
254
- onResetAxes,
255
- onUpdateAxes,
256
- };
257
- };