@oliasoft-open-source/charts-library 3.3.6-beta-5 → 3.3.6

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 (99) hide show
  1. package/dist/index.js +39230 -0
  2. package/dist/index.js.map +1 -0
  3. package/package.json +6 -1
  4. package/index.js +0 -14
  5. package/release-notes.md +0 -378
  6. package/src/assets/circle.svg +0 -1
  7. package/src/assets/icons/line-and-point.svg +0 -1
  8. package/src/assets/icons/line-only.svg +0 -1
  9. package/src/assets/icons/list-hide.svg +0 -1
  10. package/src/assets/icons/point-only.svg +0 -1
  11. package/src/assets/rect.svg +0 -1
  12. package/src/assets/rectRot.svg +0 -1
  13. package/src/assets/triangle.svg +0 -1
  14. package/src/components/bar-chart/bar-chart-prop-types.js +0 -209
  15. package/src/components/bar-chart/bar-chart.interface.ts +0 -98
  16. package/src/components/bar-chart/bar-chart.jsx +0 -247
  17. package/src/components/bar-chart/bar-chart.module.less +0 -61
  18. package/src/components/bar-chart/get-bar-chart-data-labels.js +0 -42
  19. package/src/components/bar-chart/get-bar-chart-scales.js +0 -138
  20. package/src/components/bar-chart/get-bar-chart-tooltips.js +0 -102
  21. package/src/components/controls/axes-options/axes-options.jsx +0 -271
  22. package/src/components/line-chart/constants/default-translations.js +0 -24
  23. package/src/components/line-chart/constants/line-chart-consts.js +0 -12
  24. package/src/components/line-chart/controls/axes-options/action-types.js +0 -5
  25. package/src/components/line-chart/controls/axes-options/axes-options-form-state.js +0 -97
  26. package/src/components/line-chart/controls/controls-portal.jsx +0 -12
  27. package/src/components/line-chart/controls/controls.jsx +0 -176
  28. package/src/components/line-chart/controls/controls.module.less +0 -12
  29. package/src/components/line-chart/controls/drag-options.jsx +0 -124
  30. package/src/components/line-chart/controls/line-options.jsx +0 -64
  31. package/src/components/line-chart/hooks/use-chart-functions.js +0 -261
  32. package/src/components/line-chart/hooks/use-chart-options.js +0 -181
  33. package/src/components/line-chart/hooks/use-chart-plugins.js +0 -35
  34. package/src/components/line-chart/hooks/use-toggle-handler.js +0 -33
  35. package/src/components/line-chart/initialize/config.js +0 -23
  36. package/src/components/line-chart/initialize/initialize-line-chart.js +0 -25
  37. package/src/components/line-chart/legend/legend-constants.js +0 -1
  38. package/src/components/line-chart/legend/legend-dropzone.jsx +0 -74
  39. package/src/components/line-chart/legend/legend-item.jsx +0 -105
  40. package/src/components/line-chart/legend/legend-panel.jsx +0 -86
  41. package/src/components/line-chart/legend/legend.jsx +0 -54
  42. package/src/components/line-chart/legend/legend.module.less +0 -192
  43. package/src/components/line-chart/line-chart-prop-types.js +0 -298
  44. package/src/components/line-chart/line-chart.interface.ts +0 -128
  45. package/src/components/line-chart/line-chart.jsx +0 -180
  46. package/src/components/line-chart/line-chart.module.less +0 -78
  47. package/src/components/line-chart/plugins/chart-area-text-plugin.js +0 -246
  48. package/src/components/line-chart/plugins/line-chart.minor-gridlines-plugin.js +0 -88
  49. package/src/components/line-chart/plugins/plugin-constants.js +0 -11
  50. package/src/components/line-chart/state/action-types.js +0 -12
  51. package/src/components/line-chart/state/initial-state.js +0 -89
  52. package/src/components/line-chart/state/line-chart-reducer.js +0 -101
  53. package/src/components/line-chart/state/manage-state-in-local-storage.js +0 -86
  54. package/src/components/line-chart/state/use-chart-state.js +0 -141
  55. package/src/components/line-chart/utils/axis-formatting/axis-formatting.js +0 -69
  56. package/src/components/line-chart/utils/axis-scales/axis-scales.js +0 -165
  57. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-condition.js +0 -13
  58. package/src/components/line-chart/utils/datalabels-alignment/get-alignment-data.js +0 -20
  59. package/src/components/line-chart/utils/datalabels-alignment/get-datalabels-position.js +0 -25
  60. package/src/components/line-chart/utils/generate-line-chart-datasets.js +0 -122
  61. package/src/components/line-chart/utils/get-axes-ranges-from-chart.js +0 -13
  62. package/src/components/line-chart/utils/get-line-chart-data-labels.js +0 -21
  63. package/src/components/line-chart/utils/get-line-chart-scales.js +0 -117
  64. package/src/components/line-chart/utils/get-line-chart-tooltips.js +0 -94
  65. package/src/components/line-chart/utils/line-chart-utils.js +0 -62
  66. package/src/components/line-chart/utils/translations/get-translations.js +0 -17
  67. package/src/components/pie-chart/pie-chart-prop-types.js +0 -111
  68. package/src/components/pie-chart/pie-chart-utils.js +0 -32
  69. package/src/components/pie-chart/pie-chart.interface.ts +0 -61
  70. package/src/components/pie-chart/pie-chart.jsx +0 -458
  71. package/src/components/pie-chart/pie-chart.module.less +0 -61
  72. package/src/components/scatter-chart/scatter-chart.intefrace.ts +0 -33
  73. package/src/components/scatter-chart/scatter-chart.jsx +0 -21
  74. package/src/components/scatter-chart/scatter-chart.module.less +0 -4
  75. package/src/helpers/chart-border-plugin.js +0 -19
  76. package/src/helpers/chart-consts.js +0 -64
  77. package/src/helpers/chart-interface.ts +0 -94
  78. package/src/helpers/chart-utils.js +0 -178
  79. package/src/helpers/container.jsx +0 -60
  80. package/src/helpers/disabled-context.js +0 -8
  81. package/src/helpers/enums.js +0 -107
  82. package/src/helpers/get-chart-annotation.js +0 -102
  83. package/src/helpers/get-custom-legend-plugin-example.js +0 -80
  84. package/src/helpers/get-draggableData.js +0 -32
  85. package/src/helpers/range/estimate-data-series-have-close-values.js +0 -54
  86. package/src/helpers/range/range.js +0 -100
  87. package/src/helpers/text.js +0 -6
  88. package/src/style/external.less +0 -4
  89. package/src/style/fonts/lato/Lato-Bold.woff2 +0 -0
  90. package/src/style/fonts/lato/Lato-BoldItalic.woff2 +0 -0
  91. package/src/style/fonts/lato/Lato-Italic.woff2 +0 -0
  92. package/src/style/fonts/lato/Lato-Regular.woff2 +0 -0
  93. package/src/style/fonts.less +0 -27
  94. package/src/style/global.less +0 -43
  95. package/src/style/reset/reset.less +0 -28
  96. package/src/style/shared.less +0 -11
  97. package/src/style/variables.less +0 -91
  98. package/vite.config.mjs +0 -49
  99. package/vitest.config.mjs +0 -8
@@ -1,124 +0,0 @@
1
- import React, { useMemo } from 'react';
2
- import PropTypes from 'prop-types';
3
- import {
4
- Button,
5
- Flex,
6
- Menu,
7
- Text,
8
- } from '@oliasoft-open-source/react-ui-library';
9
- import { RiDragMove2Line, RiForbidLine, RiZoomInLine } from 'react-icons/ri';
10
- import { TbHandStop } from 'react-icons/tb';
11
-
12
- export const DragOptions = ({
13
- onTogglePan,
14
- onToggleZoom,
15
- panEnabled,
16
- zoomEnabled,
17
- enableDragPoints,
18
- isDragDataAllowed,
19
- onToggleDragPoints,
20
- onDisableDragOptions,
21
- translations: {
22
- dragToZoom,
23
- doubleClickToReset,
24
- dragToPan,
25
- orDoubleClickToCanvas,
26
- dragToMovePoints,
27
- dragDisabled,
28
- },
29
- }) => {
30
- const options = useMemo(
31
- () => [
32
- {
33
- buttonLabel: dragToZoom,
34
- label: (
35
- <Flex direction="column">
36
- <Text>Drag to zoom</Text>
37
- <Text small muted>
38
- {doubleClickToReset}
39
- </Text>
40
- </Flex>
41
- ),
42
- icon: <RiZoomInLine />,
43
- selected: zoomEnabled,
44
- onClick: onToggleZoom,
45
- },
46
- {
47
- buttonLabel: dragToPan,
48
- label: (
49
- <Flex direction="column">
50
- <Text>Drag to pan</Text>
51
- <Text small muted>
52
- {orDoubleClickToCanvas}
53
- </Text>
54
- </Flex>
55
- ),
56
- icon: <RiDragMove2Line />,
57
- selected: panEnabled,
58
- onClick: onTogglePan,
59
- },
60
- ...(isDragDataAllowed
61
- ? [
62
- {
63
- label: dragToMovePoints,
64
- icon: <TbHandStop />,
65
- selected: enableDragPoints,
66
- type: 'Option',
67
- onClick: onToggleDragPoints,
68
- },
69
- ]
70
- : []),
71
- {
72
- label: dragDisabled,
73
- icon: <RiForbidLine />,
74
- selected: !zoomEnabled && !panEnabled && !enableDragPoints,
75
- onClick: onDisableDragOptions,
76
- },
77
- ],
78
- [zoomEnabled, panEnabled, enableDragPoints, isDragDataAllowed],
79
- );
80
-
81
- const selectedOption = options.find((option) => option.selected);
82
- const optionsWithDragPoints = options.map(
83
- ({ icon, label, onClick, selected }) => ({
84
- icon,
85
- label,
86
- type: 'Option',
87
- onClick,
88
- disabled: selected,
89
- }),
90
- );
91
-
92
- return (
93
- <Menu
94
- menu={{
95
- sections: optionsWithDragPoints,
96
- trigger: 'Component',
97
- component: (
98
- <Button
99
- colored="muted"
100
- basic
101
- small
102
- label={selectedOption.buttonLabel || selectedOption.label}
103
- icon={selectedOption.icon}
104
- />
105
- ),
106
- }}
107
- />
108
- );
109
- };
110
-
111
- DragOptions.defaultProps = {
112
- enableDragPoints: false,
113
- };
114
-
115
- DragOptions.propTypes = {
116
- onTogglePan: PropTypes.func.isRequired,
117
- onToggleZoom: PropTypes.func.isRequired,
118
- panEnabled: PropTypes.bool.isRequired,
119
- zoomEnabled: PropTypes.bool.isRequired,
120
- enableDragPoints: PropTypes.bool,
121
- isDragDataAllowed: PropTypes.bool.isRequired,
122
- onToggleDragPoints: PropTypes.func.isRequired,
123
- onDisableDragOptions: PropTypes.func.isRequired,
124
- };
@@ -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,261 +0,0 @@
1
- import { useCallback } from 'react';
2
- import * as htmlToImage from 'html-to-image';
3
- import { triggerBase64Download } from 'react-base64-downloader';
4
- import { AxisType, Key, PanZoomMode } from '../../../helpers/enums';
5
- import { getChartFileName } from '../../../helpers/chart-utils';
6
- import { generateAxisId } from '../utils/line-chart-utils';
7
- import { RESET_AXES_RANGES, UPDATE_AXES_RANGES } from '../state/action-types';
8
-
9
- /**
10
- * Custom hook that encapsulates the chart-related functions and their dependencies
11
- * @param {Object} chartRef - Chart reference
12
- * @param {Object} state - The component state
13
- * @param {Object} options - Chart options
14
- * @param {Function} dispatch - Dispatch function from useReducer
15
- * @param {Array} generatedDatasets - Chart datasets
16
- * @returns {Object} An object containing the chart-related functions
17
- */
18
- export const useChartFunctions = ({
19
- chartRef,
20
- state,
21
- options,
22
- dispatch,
23
- generatedDatasets,
24
- }) => {
25
- const {
26
- annotations,
27
- interactions: { onLegendClick, onPointHover, onPointUnhover },
28
- additionalAxesOptions,
29
- axes,
30
- } = options;
31
-
32
- /**
33
- * Handles legend click events
34
- * @param {Event} e - Click event
35
- * @param {Object} legendItem - Legend item data
36
- */
37
- const legendClick = useCallback(
38
- (e, legendItem) => {
39
- const { datasetIndex } = legendItem;
40
- const chartInstance = chartRef.current;
41
- const { datasets } = chartInstance.data;
42
- const dataset = datasets[datasetIndex];
43
- const meta = chartInstance.getDatasetMeta(datasetIndex);
44
- meta.hidden = meta.hidden === null ? !dataset.hidden : null;
45
-
46
- if (annotations.controlAnnotation && dataset.isAnnotation) {
47
- const { annotationIndex } = dataset;
48
- dispatch({ type: 'TOGGLE_ANNOTATION', payload: { annotationIndex } });
49
- }
50
-
51
- // Show/hide entire display group
52
- if (dataset.displayGroup) {
53
- datasets.forEach((ds, ix) => {
54
- if (ds.displayGroup !== dataset.displayGroup) return;
55
- chartInstance.getDatasetMeta(ix).hidden = meta.hidden;
56
- });
57
- }
58
-
59
- if (onLegendClick) {
60
- onLegendClick(legendItem?.text, legendItem.hidden);
61
- }
62
-
63
- chartInstance.update();
64
- },
65
- [onLegendClick, annotations],
66
- );
67
-
68
- /**
69
- * Resets the chart zoom and updates the axes ranges
70
- * @returns {Function} Memoized resetZoom function
71
- */
72
- const resetZoom = useCallback(() => {
73
- const chartInstance = chartRef.current;
74
- chartInstance.resetZoom();
75
- dispatch({ type: 'RESET_AXES_RANGES' });
76
- }, [chartRef]);
77
-
78
- /**
79
- * Handles the hover event on chart points
80
- * @param {Object} hoveredPoint - Currently hovered point
81
- * @param {Function} setHoveredPoint - Setter function for hoveredPoint state
82
- * @returns {Function} Memoized onHover function
83
- */
84
- const onHover = useCallback((hoveredPoint, setHoveredPoint) => {
85
- return (evt, hoveredItems) => {
86
- if (!hoveredItems?.length && onPointUnhover && hoveredPoint) {
87
- setHoveredPoint(null);
88
- onPointUnhover(evt);
89
- }
90
-
91
- if (hoveredItems?.length && onPointHover) {
92
- const { index, datasetIndex } = hoveredItems[0];
93
- const dataset = generatedDatasets[datasetIndex];
94
- const point = dataset?.data[index];
95
-
96
- if (point && hoveredPoint !== point) {
97
- setHoveredPoint(point);
98
- onPointHover(evt, datasetIndex, index, generatedDatasets);
99
- }
100
- }
101
- };
102
- }, []);
103
-
104
- /**
105
- * Handles the download of the chart as an image
106
- * @returns {Function} Memoized handleDownload function
107
- */
108
- const handleDownload = useCallback(() => {
109
- const chart = chartRef.current;
110
- // Get the parent element that includes the legend
111
- const canvasElement = chart?.canvas?.parentElement;
112
- if (!canvasElement) return;
113
-
114
- // Add temporary canvas background
115
- const { ctx } = chart;
116
- ctx.save();
117
- ctx.globalCompositeOperation = 'destination-over';
118
- const isDark = document.body.dataset.theme === 'dark';
119
- ctx.fillStyle = isDark ? 'black' : 'white';
120
- ctx.fillRect(0, 0, chart.width, chart.height);
121
- ctx.restore();
122
- const fileName = getChartFileName(state.axes);
123
- htmlToImage.toPng(canvasElement).then((dataUrl) => {
124
- triggerBase64Download(dataUrl, fileName);
125
- });
126
- }, [chartRef, state.axes]);
127
-
128
- /**
129
- * Handles the key down event for the chart
130
- * @returns {Function} Memoized handleKeyDown function
131
- */
132
- const handleKeyDown = useCallback(
133
- (evt) => {
134
- if (evt.key === Key.Shift) {
135
- const chart = chartRef.current;
136
- chart.config.options.plugins.zoom.zoom.mode = PanZoomMode.Y;
137
- chart.config.options.plugins.zoom.pan.mode = PanZoomMode.Y;
138
- chart.update();
139
- }
140
- },
141
- [chartRef],
142
- );
143
-
144
- /**
145
- * Handles the key up event for the chart
146
- * @returns {Function} Memoized handleKeyUp function
147
- */
148
- const handleKeyUp = useCallback(
149
- (evt) => {
150
- if (evt.key === Key.Shift) {
151
- const chart = chartRef.current;
152
- chart.config.options.plugins.zoom.zoom.mode = PanZoomMode.Z;
153
- chart.config.options.plugins.zoom.pan.mode = PanZoomMode.Z;
154
- chart.update();
155
- }
156
- },
157
- [chartRef],
158
- );
159
-
160
- /**
161
- * Get the controls axes based on the state and additionalAxesOptions.
162
- * @returns {Array} Array of controls axes.
163
- */
164
- const controlsAxes = state.axes.map((axis, i) => {
165
- const axisType = i ? AxisType.Y : AxisType.X; // only first element is 'x' - rest is 'y'
166
- const min = axis.min ?? additionalAxesOptions?.range?.[axisType]?.min;
167
- const max = axis.max ?? additionalAxesOptions?.range?.[axisType]?.max;
168
- return {
169
- ...axis,
170
- // only add min and max properties if they are defined:
171
- ...(min ? { min } : {}),
172
- ...(max ? { max } : {}),
173
- };
174
- });
175
-
176
- /**
177
- * Get the controls axes labels based on the propsAxes.
178
- * @param {Object} propsAxes - Props axes object.
179
- * @returns {Array} Array of controls axes labels.
180
- */
181
- const getControlsAxesLabels = useCallback(
182
- (propsAxes) => {
183
- if (!Object.keys(propsAxes)?.length) {
184
- return [];
185
- }
186
-
187
- const getAxesLabels = (axes, axisType) => {
188
- if (!axes[axisType] || !axes[axisType]?.length) {
189
- return [];
190
- } else {
191
- return axes[axisType].map((axisObj, index) => {
192
- return {
193
- id: generateAxisId(axisType, index, axes[axisType].length > 1),
194
- label: axisObj?.label || '',
195
- };
196
- });
197
- }
198
- };
199
-
200
- const axesLabels = [
201
- ...getAxesLabels(propsAxes, AxisType.X),
202
- ...getAxesLabels(propsAxes, AxisType.Y),
203
- ];
204
- return axesLabels;
205
- },
206
- [axes],
207
- );
208
-
209
- /**
210
- * Update axes ranges from the chart.
211
- * @param {Object} chart - Chart instance.
212
- */
213
- const updateAxesRangesFromChart = useCallback(() => {
214
- const { scales = {} } = chartRef?.current || {};
215
- const axes = Object.entries(scales).map(([key, { min, max }]) => {
216
- return {
217
- id: key,
218
- min: min ?? 0,
219
- max: max ?? 0,
220
- };
221
- });
222
- dispatch({
223
- type: UPDATE_AXES_RANGES,
224
- payload: { axes },
225
- });
226
- }, [axes]);
227
-
228
- /**
229
- * Handler for resetting axes ranges.
230
- * @type {Function}
231
- */
232
- const onResetAxes = useCallback(() => {
233
- // Dispatch the RESET_AXES_RANGES action
234
- dispatch({ type: RESET_AXES_RANGES });
235
- }, []);
236
-
237
- /**
238
- * Handler for updating axes ranges.
239
- * @param {Object} axesInfo - Object containing axes information.
240
- * @param {Array} axesInfo.axes - Array of axes to update.
241
- * @type {Function}
242
- */
243
- const onUpdateAxes = useCallback(({ axes }) => {
244
- // Dispatch the UPDATE_AXES_RANGES action with payload
245
- dispatch({ type: UPDATE_AXES_RANGES, payload: { axes } });
246
- }, []);
247
-
248
- return {
249
- legendClick,
250
- resetZoom,
251
- onHover,
252
- handleDownload,
253
- handleKeyDown,
254
- handleKeyUp,
255
- controlsAxes,
256
- controlsAxesLabels: getControlsAxesLabels(axes),
257
- updateAxesRangesFromChart,
258
- onResetAxes,
259
- onUpdateAxes,
260
- };
261
- };
@@ -1,181 +0,0 @@
1
- import { useMemo, useState } from 'react';
2
- import {
3
- ANIMATION_DURATION,
4
- BORDER_COLOR,
5
- CUSTOM_LEGEND_PLUGIN_NAME,
6
- } from '../../../helpers/chart-consts';
7
- import {
8
- ChartHoverMode,
9
- Events,
10
- PanZoomMode,
11
- PointStyle,
12
- } from '../../../helpers/enums';
13
- import { autoScale } from '../utils/axis-scales/axis-scales';
14
- import getLineChartDataLabels from '../utils/get-line-chart-data-labels';
15
- import getAnnotation from '../../../helpers/get-chart-annotation';
16
- import { ZOOM_BOX_BACKGROUND_COLOR } from '../constants/line-chart-consts';
17
- import getLineChartToolTips from '../utils/get-line-chart-tooltips';
18
- import { getLegend } from '../../../helpers/chart-utils';
19
- import getDraggableData from '../../../helpers/get-draggableData';
20
- import { useChartFunctions } from './use-chart-functions';
21
-
22
- /**
23
- * Custom hook to generate chart options.
24
- * @function
25
- * @param {Object} chartRef - The chart reference.
26
- * @param {Object} state - The chart state.
27
- * @param {Object} options - The chart options.
28
- * @param {Function} dispatch - The dispatch function for state management.
29
- * @param {Array} generatedDatasets - The generated datasets.
30
- * @returns {Object} The chart options object.
31
- */
32
- export const useChartOptions = ({
33
- chartRef,
34
- state,
35
- options,
36
- dispatch,
37
- generatedDatasets,
38
- }) => {
39
- const {
40
- interactions: { onAnimationComplete },
41
- annotations: {
42
- labelAnnotation: {
43
- showLabel,
44
- text,
45
- position,
46
- fontSize,
47
- xOffset,
48
- yOffset,
49
- maxWidth,
50
- lineHeight,
51
- },
52
- },
53
- chartStyling: { layoutPadding },
54
- } = options;
55
-
56
- const {
57
- enableDragPoints,
58
- zoomEnabled,
59
- panEnabled,
60
- lineEnabled,
61
- showAnnotationLineIndex,
62
- legendEnabled,
63
- } = state;
64
-
65
- const [hoveredPoint, setHoveredPoint] = useState(null);
66
- const { legendClick, updateAxesRangesFromChart, onHover } = useChartFunctions(
67
- {
68
- chartRef,
69
- state,
70
- options,
71
- dispatch,
72
- generatedDatasets,
73
- },
74
- );
75
-
76
- const datalabels = getLineChartDataLabels(options);
77
- const tooltip = getLineChartToolTips(options);
78
- const annotation = useMemo(
79
- () => getAnnotation(options, state),
80
- [showAnnotationLineIndex, legendClick],
81
- );
82
- const legend = useMemo(
83
- () => getLegend(options, legendClick, state),
84
- [legendEnabled],
85
- );
86
-
87
- const scales = useMemo(
88
- () => autoScale(options, state, generatedDatasets),
89
- [options, state, generatedDatasets],
90
- );
91
-
92
- const dragData = useMemo(
93
- () => enableDragPoints && getDraggableData(options),
94
- [enableDragPoints, options],
95
- );
96
-
97
- const panOptions = useMemo(
98
- () => ({
99
- enabled: panEnabled,
100
- mode: PanZoomMode.XY,
101
- onPanComplete({ chart }) {
102
- updateAxesRangesFromChart(chart);
103
- },
104
- }),
105
- [panEnabled],
106
- );
107
-
108
- const zoomOptions = useMemo(
109
- () => ({
110
- mode: PanZoomMode.XY,
111
- drag: {
112
- enabled: zoomEnabled,
113
- threshold: 3,
114
- backgroundColor: ZOOM_BOX_BACKGROUND_COLOR,
115
- borderColor: BORDER_COLOR,
116
- borderWidth: 1,
117
- },
118
- onZoomComplete({ chart }) {
119
- updateAxesRangesFromChart(chart);
120
- },
121
- }),
122
- [zoomEnabled],
123
- );
124
-
125
- const plugins = {
126
- datalabels,
127
- annotation,
128
- zoom: { pan: panOptions, zoom: zoomOptions },
129
- tooltip,
130
- legend: { ...legend, display: false }, // hide default legend for line chart only
131
- [CUSTOM_LEGEND_PLUGIN_NAME]: options.legend.customLegend
132
- .customLegendPlugin && {
133
- containerID: options.legend.customLegend.customLegendContainerID,
134
- },
135
- chartAreaBorder: {
136
- borderColor: BORDER_COLOR,
137
- },
138
- chartAreaText: {
139
- showLabel,
140
- text,
141
- position,
142
- fontSize,
143
- xOffset,
144
- yOffset,
145
- maxWidth,
146
- lineHeight,
147
- },
148
- ...dragData,
149
- };
150
-
151
- return useMemo(
152
- () => ({
153
- layout: {
154
- padding: layoutPadding,
155
- },
156
- onHover: onHover(hoveredPoint, setHoveredPoint),
157
- maintainAspectRatio: options.chartStyling.maintainAspectRatio,
158
- aspectRatio: options.chartStyling.squareAspectRatio ? 1 : null,
159
- animation: options.chartStyling.performanceMode
160
- ? false
161
- : {
162
- duration: ANIMATION_DURATION.FAST,
163
- onComplete: onAnimationComplete,
164
- },
165
- hover: {
166
- mode: ChartHoverMode.Nearest,
167
- intersect: true,
168
- },
169
- elements: {
170
- line: {
171
- pointStyle: PointStyle.Circle,
172
- showLine: lineEnabled,
173
- },
174
- },
175
- scales,
176
- plugins,
177
- events: Events,
178
- }),
179
- [state, options],
180
- );
181
- };
@@ -1,35 +0,0 @@
1
- import { useCallback, useMemo } from 'react';
2
- import { debounce } from 'lodash';
3
- import { getPlugins } from '../../../helpers/chart-utils';
4
- import { DOUBLE_CLICK } from '../constants/line-chart-consts';
5
-
6
- export const useChartPlugins = ({ options, resetZoom }) => {
7
- const { graph, legend } = options;
8
-
9
- const debouncedResetZoom = useCallback(
10
- debounce(() => {
11
- resetZoom();
12
- }, 100), // 300ms debounce delay
13
- [resetZoom],
14
- );
15
-
16
- const handleDoubleClick = useCallback(
17
- (chart, args) => {
18
- const { event } = args;
19
- if (event.type === DOUBLE_CLICK) {
20
- debouncedResetZoom();
21
- }
22
- },
23
- [debouncedResetZoom],
24
- );
25
-
26
- return useMemo(() => {
27
- return [
28
- ...getPlugins(graph, legend),
29
- {
30
- id: 'customEventCatcher',
31
- beforeEvent: handleDoubleClick,
32
- },
33
- ];
34
- }, [handleDoubleClick]);
35
- };
@@ -1,33 +0,0 @@
1
- import { useCallback } from 'react';
2
- import {
3
- DISABLE_DRAG_OPTIONS,
4
- TOGGLE_DRAG_POINTS,
5
- TOGGLE_LEGEND,
6
- TOGGLE_LINE,
7
- TOGGLE_PAN,
8
- TOGGLE_POINTS,
9
- TOGGLE_TABLE,
10
- TOGGLE_ZOOM,
11
- } from '../state/action-types';
12
-
13
- export const useToggleHandlers = (dispatch) => {
14
- const createToggleHandler = useCallback(
15
- (type) => () => {
16
- dispatch({ type });
17
- },
18
- [dispatch],
19
- );
20
-
21
- const toggleHandlers = {
22
- onToggleLegend: createToggleHandler(TOGGLE_LEGEND),
23
- onToggleLine: createToggleHandler(TOGGLE_LINE),
24
- onTogglePan: createToggleHandler(TOGGLE_PAN),
25
- onTogglePoints: createToggleHandler(TOGGLE_POINTS),
26
- onToggleTable: createToggleHandler(TOGGLE_TABLE),
27
- onToggleZoom: createToggleHandler(TOGGLE_ZOOM),
28
- onToggleDragPoints: createToggleHandler(TOGGLE_DRAG_POINTS),
29
- onDisableDragOptions: createToggleHandler(DISABLE_DRAG_OPTIONS),
30
- };
31
-
32
- return toggleHandlers;
33
- };