@oliasoft-open-source/charts-library 3.3.6-beta-5 → 3.3.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 (99) hide show
  1. package/dist/index.js +39236 -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,246 +0,0 @@
1
- import { TextLabelPosition } from './plugin-constants';
2
- import { AlignOptions } from '../../../helpers/enums';
3
-
4
- const WORD_SEPARATOR = ' ';
5
- const SEMI_TRANSPARENT = 'rgba(0, 0, 0, 0.5)';
6
-
7
- /**
8
- * Splits the input text into words based on the predefined WORD_SEPARATOR.
9
- * If the input is an array, it first joins the array and then splits it.
10
- *
11
- * @param {string | string[]} text - The text to split into words.
12
- * @returns {string[]} An array of words.
13
- */
14
- const getWords = (text) => {
15
- return (Array.isArray(text) ? text.join(WORD_SEPARATOR) : text).split(
16
- WORD_SEPARATOR,
17
- );
18
- };
19
-
20
- /**
21
- * Renders the lines of text on the canvas context.
22
- * It iterates over the array of lines and renders each line with the provided styling.
23
- *
24
- * @param {CanvasRenderingContext2D} ctx - The canvas rendering context to draw on.
25
- * @param {string[]} lines - The array of lines to be rendered.
26
- * @param {number} lineHeight - The height of each line.
27
- * @param {number} x - The x-coordinate of the starting position.
28
- * @param {number} y - The y-coordinate of the starting position.
29
- * @param {string} position - The position for the text (one of the values from the TextLabelPosition enum).
30
- */
31
- const drawText = (ctx, lines, lineHeight, x, y, position) => {
32
- lines.forEach((line, index) => {
33
- const lineY = position.includes('top')
34
- ? y + lineHeight * (index + 1) - 5
35
- : y - (lines.length - 1 - index) * lineHeight;
36
- ctx.fillText(line, x, lineY);
37
- });
38
- };
39
-
40
- /**
41
- * Calculates the maximum width for the text based on the initial maximum width
42
- * and the chart area width.
43
- * @param {number} initialMaxWidth - The initial maximum width.
44
- * @param {number} chartAreaWidth - The width of the chart area.
45
- * @returns {number} The updated maximum width.
46
- */
47
- const calculateMaxWidth = (initialMaxWidth, chartAreaWidth) => {
48
- const factorMiddle = 0.5;
49
- const factorSmall = 0.7;
50
- const maxWidthFactor =
51
- chartAreaWidth < 500
52
- ? factorMiddle
53
- : chartAreaWidth < 700
54
- ? factorSmall
55
- : 1;
56
- return initialMaxWidth * maxWidthFactor;
57
- };
58
-
59
- /**
60
- * Determines the legend dimensions (width and height) based on its position.
61
- *
62
- * @param {object} chart - The Chart.js chart object.
63
- * @returns {object} An object containing the legend width and height.
64
- */
65
- const getLegendDimensions = (chart) => {
66
- const { legend } = chart;
67
-
68
- const legendWidth =
69
- legend && legend.display && legend.options.position === 'left'
70
- ? legend.width + legend.options.labels.padding * 2
71
- : 0;
72
-
73
- const legendHeight =
74
- legend &&
75
- legend.display &&
76
- (legend.options.position === 'top' || legend.options.position === 'bottom')
77
- ? legend.height + legend.options.labels.padding * 2
78
- : 0;
79
-
80
- return { legendWidth, legendHeight };
81
- };
82
-
83
- /**
84
- * Determines the X and Y coordinates for the provided position.
85
- *
86
- * @param {string} position - The position for the text (one of the values from the TextLabelPosition enum).
87
- * @param {object} chart - The Chart.js chart object.
88
- * @param {number} xOffset - The horizontal offset from the specified position.
89
- * @param {number} yOffset - The vertical offset from the specified position.
90
- * @returns {number[]} An array with the X and Y coordinates.
91
- */
92
- const getPositionCoordinates = (position, chart, xOffset, yOffset) => {
93
- const { chartArea, width } = chart;
94
- const temporaryChartAreaRight = width - 8;
95
-
96
- const { legendWidth, legendHeight } = getLegendDimensions(chart);
97
-
98
- switch (position) {
99
- case TextLabelPosition.TOP_LEFT:
100
- return [
101
- chartArea.left + xOffset + legendWidth,
102
- chartArea.top + yOffset + legendHeight,
103
- ];
104
- case TextLabelPosition.TOP_CENTER:
105
- return [
106
- chartArea.left + chartArea.width / 2,
107
- chartArea.top + yOffset + legendHeight,
108
- ];
109
- case TextLabelPosition.TOP_RIGHT:
110
- return [
111
- temporaryChartAreaRight - xOffset, //replace within chartArea.right when resize bug will be fixed
112
- chartArea.top + yOffset + legendHeight,
113
- ];
114
- case TextLabelPosition.MIDDLE_LEFT:
115
- return [
116
- chartArea.left + xOffset + legendWidth,
117
- chartArea.top + chartArea.height / 2,
118
- ];
119
- case TextLabelPosition.MIDDLE_CENTER:
120
- return [
121
- chartArea.left + chartArea.width / 2,
122
- chartArea.top + chartArea.height / 2,
123
- ];
124
- case TextLabelPosition.MIDDLE_RIGHT:
125
- return [
126
- temporaryChartAreaRight - xOffset - legendWidth, //replace within chartArea.right when resize bug will be fixed
127
- chartArea.top + chartArea.height / 2,
128
- ];
129
- case TextLabelPosition.BOTTOM_LEFT:
130
- return [
131
- chartArea.left + xOffset + legendWidth,
132
- chartArea.bottom - yOffset - legendHeight,
133
- ];
134
- case TextLabelPosition.BOTTOM_CENTER:
135
- return [
136
- chartArea.left + chartArea.width / 2,
137
- chartArea.bottom - yOffset - legendHeight,
138
- ];
139
- case TextLabelPosition.BOTTOM_RIGHT:
140
- default:
141
- return [
142
- temporaryChartAreaRight - xOffset - legendWidth, //replace within chartArea.right when resize bug will be fixed
143
- chartArea.bottom - yOffset - legendHeight,
144
- ];
145
- }
146
- };
147
-
148
- /**
149
- * Determines the appropriate text alignment based on the position provided.
150
- *
151
- * @param {string} position - The position for the text (one of the values from the TextLabelPosition enum).
152
- * @returns {string} The text alignment ('left', 'center', or 'right').
153
- */
154
- const getTextAlign = (position) => {
155
- if (position.includes(AlignOptions.Center)) {
156
- return AlignOptions.Center;
157
- } else if (position.includes(AlignOptions.Left)) {
158
- return AlignOptions.Left;
159
- } else {
160
- return AlignOptions.Right;
161
- }
162
- };
163
-
164
- /**
165
- * Wraps the text into lines based on the maxWidth provided.
166
- *
167
- * @param {string[]} words - The array of words to be processed.
168
- * @param {number} maxWidth - The maximum width allowed for the text.
169
- * @param {CanvasRenderingContext2D} ctx - The canvas rendering context to measure the text width.
170
- * @returns {string[]} An array of wrapped lines.
171
- */
172
- const wrapText = (words, maxWidth, ctx) => {
173
- let line = '';
174
- let lines = [];
175
- for (let i = 0; i < words.length; i++) {
176
- const testLine = `${line}${words[i]}${WORD_SEPARATOR}`;
177
- const { width: testWidth } = ctx.measureText(testLine);
178
- if (testWidth > maxWidth) {
179
- lines.push(line.trim());
180
- line = `${words[i]}${WORD_SEPARATOR}`;
181
- } else {
182
- line = testLine;
183
- }
184
- }
185
- lines.push(line.trim()); // Add the last line
186
- return lines;
187
- };
188
-
189
- /**
190
- * Renders the wrapped text on the canvas context.
191
- *
192
- * @param {CanvasRenderingContext2D} ctx - The canvas rendering context to draw on.
193
- * @param {object} options - The options object containing text, maxWidth, fontSize, lineHeight, position, and coordinates (x, y).
194
- */
195
- const renderWrappedText = (ctx, options) => {
196
- const { text, maxWidth, fontSize, lineHeight, x, y, position } = options;
197
-
198
- const words = getWords(text);
199
- const wrappedLines = wrapText(words, maxWidth, ctx);
200
-
201
- ctx.save();
202
- ctx.font = `${fontSize}px Arial`;
203
- ctx.fillStyle = SEMI_TRANSPARENT;
204
- ctx.textAlign = getTextAlign(position);
205
-
206
- drawText(ctx, wrappedLines, lineHeight, x, y, position);
207
-
208
- ctx.restore();
209
- };
210
-
211
- export const chartAreaTextPlugin = {
212
- id: 'chartAreaText',
213
-
214
- beforeDraw: (chart, args, options) => {
215
- const {
216
- showLabel,
217
- text,
218
- fontSize,
219
- xOffset,
220
- yOffset,
221
- lineHeight,
222
- maxWidth: initialMaxWidth,
223
- position,
224
- } = options;
225
- const { ctx, chartArea } = chart;
226
-
227
- if (!showLabel || !text) return;
228
-
229
- // Determine the maxWidth based on chartArea width
230
- const maxWidth = calculateMaxWidth(initialMaxWidth, chartArea.width);
231
-
232
- // Get the position coordinates
233
- const [x, y] = getPositionCoordinates(position, chart, xOffset, yOffset);
234
-
235
- // Render the wrapped text
236
- renderWrappedText(ctx, {
237
- text,
238
- maxWidth,
239
- fontSize,
240
- lineHeight,
241
- x,
242
- y,
243
- position,
244
- });
245
- },
246
- };
@@ -1,88 +0,0 @@
1
- const MINOR_TICKS_PER_MAJOR = 10;
2
-
3
- // Return largest gap between ticks
4
- // (Tick distance may not be consistent if ranges are custom)
5
- export const getLargestMajorTickWidth = (majorTickPositions) => {
6
- return majorTickPositions.reduce((acc, curr, index) => {
7
- if (index === 0) return acc;
8
- const gap = Math.abs(curr - majorTickPositions[index - 1]);
9
- return Math.max(gap, acc);
10
- }, 0);
11
- };
12
-
13
- // Check if position is in visible part of axis scale
14
- // Check position does not duplicate a major tick
15
- export const isValidPosition = (
16
- minorTickPosition,
17
- majorTickPositions,
18
- scale,
19
- ) => {
20
- const { axis, left, top, right, bottom } = scale;
21
- const isHorizontal = axis === 'x';
22
- const start = isHorizontal ? left : top;
23
- const end = isHorizontal ? right : bottom;
24
- const isAfterStart = minorTickPosition > start;
25
- const isBeforeEnd = minorTickPosition < end;
26
- const isDuplicate = majorTickPositions.indexOf(minorTickPosition) !== -1;
27
- return isAfterStart && isBeforeEnd && !isDuplicate;
28
- };
29
-
30
- // Generate minor tick positions for a given axis/scale
31
- // (Uses second tick as starting point in case first tick is an irregular custom value)
32
- export const getMinorTickPositions = (majorTickPositions, scale) => {
33
- const sortedMajorTickPositions = majorTickPositions.sort((a, b) => {
34
- return a - b;
35
- });
36
- const minorTickWidth = getLargestMajorTickWidth(majorTickPositions) / 10;
37
- const startPosition = majorTickPositions[0];
38
- const numMinorTicks = (majorTickPositions.length + 1) * MINOR_TICKS_PER_MAJOR;
39
- const positions = [...Array(numMinorTicks)]
40
- .map((_, index) => {
41
- const minorTickPosition =
42
- startPosition + (index - MINOR_TICKS_PER_MAJOR + 1) * minorTickWidth;
43
- return parseFloat(minorTickPosition.toFixed(1));
44
- })
45
- .filter((position) =>
46
- isValidPosition(position, sortedMajorTickPositions, scale),
47
- );
48
- return positions;
49
- };
50
-
51
- const drawMinorTicksForScale = (scale) => {
52
- const { chart, ctx } = scale;
53
- if (!chart.config._config.options.scales[scale.id].grid.drawOnChartArea)
54
- return;
55
- if (chart.config._config.options.indexAxis === scale.axis) return;
56
- const isHorizontal = scale.axis === 'x';
57
- const majorTickPositions = scale.ticks
58
- .map((_, index) => scale.getPixelForTick(index))
59
- .sort((a, b) => a - b);
60
- const minorTickPositions = getMinorTickPositions(majorTickPositions, scale);
61
- ctx.save();
62
- ctx.strokeStyle =
63
- chart.config._config.options.scales[scale.id].grid.color ||
64
- 'rgba(0,0,0,0.06)';
65
- ctx.lineWidth = 0.5;
66
- minorTickPositions.forEach((minorTickPosition) => {
67
- ctx.beginPath();
68
- if (isHorizontal) {
69
- const { top, bottom } = chart.chartArea;
70
- ctx.moveTo(minorTickPosition, top);
71
- ctx.lineTo(minorTickPosition, bottom);
72
- } else {
73
- const { left, right } = chart.chartArea;
74
- ctx.moveTo(left, minorTickPosition);
75
- ctx.lineTo(right, minorTickPosition);
76
- }
77
- ctx.stroke();
78
- });
79
- ctx.restore();
80
- };
81
-
82
- export const chartMinorGridlinesPlugin = {
83
- id: 'minorGridlines',
84
- beforeDatasetsDraw: (chart) => {
85
- const { scales } = chart;
86
- Object.keys(scales).forEach((id) => drawMinorTicksForScale(scales[id]));
87
- },
88
- };
@@ -1,11 +0,0 @@
1
- export const TextLabelPosition = {
2
- TOP_LEFT: 'top-left',
3
- TOP_CENTER: 'top-center',
4
- TOP_RIGHT: 'top-right',
5
- MIDDLE_LEFT: 'middle-left',
6
- MIDDLE_CENTER: 'middle-center',
7
- MIDDLE_RIGHT: 'middle-right',
8
- BOTTOM_LEFT: 'bottom-left',
9
- BOTTOM_CENTER: 'bottom-center',
10
- BOTTOM_RIGHT: 'bottom-right',
11
- };
@@ -1,12 +0,0 @@
1
- export const TOGGLE_ZOOM = 'TOGGLE_ZOOM';
2
- export const TOGGLE_PAN = 'TOGGLE_PAN';
3
- export const TOGGLE_POINTS = 'TOGGLE_POINTS';
4
- export const TOGGLE_LINE = 'TOGGLE_LINE';
5
- export const TOGGLE_LEGEND = 'TOGGLE_LEGEND';
6
- export const TOGGLE_ANNOTATION = 'TOGGLE_ANNOTATION';
7
- export const TOGGLE_TABLE = 'TOGGLE_TABLE';
8
- export const SAVE_INITIAL_AXES_RANGES = 'SAVE_INITIAL_AXES_RANGES';
9
- export const RESET_AXES_RANGES = 'RESET_AXES_RANGES';
10
- export const UPDATE_AXES_RANGES = 'UPDATE_AXES_RANGES';
11
- export const TOGGLE_DRAG_POINTS = 'TOGGLE_DRAG_POINTS';
12
- export const DISABLE_DRAG_OPTIONS = 'DISABLE_DRAG_OPTIONS';
@@ -1,89 +0,0 @@
1
- import { AxisType } from '../../../helpers/enums';
2
- import { setAnnotations } from '../../../helpers/chart-utils';
3
- import { generateAxisId } from '../utils/line-chart-utils';
4
- import { getChartStateFromStorage } from './manage-state-in-local-storage';
5
-
6
- /**
7
- Initial chart state for the line chart.
8
-
9
- @param {Object} options - The chart options.
10
- @return {Object} The initial chart state.
11
- */
12
- const initialState = ({ options, persistenceId }) => {
13
- const {
14
- additionalAxesOptions: { range: customAxesRange },
15
- annotations: { annotationsData },
16
- axes,
17
- chartOptions: { enableZoom, enablePan, showPoints, showLine },
18
- legend: { display },
19
- dragData,
20
- } = options;
21
- /**
22
- * getStateAxesByType
23
- * @param {'x'|'y'} axisType
24
- * @param {Object} customAxesRange
25
- * @return {{id: string}[] | []} returns array of objects describing all chart axis or empty array
26
- */
27
- const getStateAxesByType = (axisType, customAxesRange) => {
28
- if (!axes[axisType]) {
29
- return [];
30
- }
31
-
32
- if (axes[axisType]?.length > 1) {
33
- return axes[axisType].map((axisObj, index) => {
34
- const id = generateAxisId(axisType, index, axes[axisType].length > 1);
35
- const customMin = customAxesRange?.[id]?.min;
36
- const customMax = customAxesRange?.[id]?.max;
37
- const { unit } = axisObj;
38
- return {
39
- id,
40
- //only add custom axis ranges if defined:
41
- ...(customMin ? { min: customMin } : {}),
42
- ...(customMax ? { max: customMax } : {}),
43
- ...(unit ? { unit } : {}),
44
- };
45
- });
46
- } else {
47
- const id = generateAxisId(axisType);
48
- const customMin = customAxesRange?.[id]?.min;
49
- const customMax = customAxesRange?.[id]?.max;
50
- const unit = axes?.[id][0].unit;
51
- return [
52
- {
53
- id,
54
- //only add custom axis ranges if defined:
55
- ...(customMin ? { min: customMin } : {}),
56
- ...(customMax ? { max: customMax } : {}),
57
- ...(unit ? { unit } : {}),
58
- },
59
- ];
60
- }
61
- };
62
-
63
- const xStateAxes = getStateAxesByType(AxisType.X, customAxesRange);
64
- const yStateAxes = getStateAxesByType(AxisType.Y, customAxesRange);
65
- const stateAxes = [...xStateAxes, ...yStateAxes];
66
-
67
- const {
68
- zoomEnabled,
69
- panEnabled,
70
- pointsEnabled,
71
- lineEnabled,
72
- legendEnabled,
73
- enableDragPoints,
74
- } = getChartStateFromStorage(persistenceId) || {};
75
-
76
- return {
77
- zoomEnabled: zoomEnabled ?? enableZoom,
78
- panEnabled: panEnabled ?? enablePan,
79
- pointsEnabled: pointsEnabled ?? showPoints,
80
- lineEnabled: lineEnabled ?? showLine,
81
- legendEnabled: legendEnabled ?? display,
82
- axes: stateAxes,
83
- showAnnotationLineIndex: setAnnotations(annotationsData),
84
- showTable: false,
85
- enableDragPoints: dragData.enableDragData && enableDragPoints,
86
- };
87
- };
88
-
89
- export default initialState;
@@ -1,101 +0,0 @@
1
- import { produce } from 'immer';
2
- import {
3
- DISABLE_DRAG_OPTIONS,
4
- RESET_AXES_RANGES,
5
- SAVE_INITIAL_AXES_RANGES,
6
- TOGGLE_ANNOTATION,
7
- TOGGLE_DRAG_POINTS,
8
- TOGGLE_LEGEND,
9
- TOGGLE_LINE,
10
- TOGGLE_PAN,
11
- TOGGLE_POINTS,
12
- TOGGLE_TABLE,
13
- TOGGLE_ZOOM,
14
- UPDATE_AXES_RANGES,
15
- } from './action-types';
16
-
17
- export const reducer = (state, action) => {
18
- return produce(state, (draft) => {
19
- switch (action.type) {
20
- case TOGGLE_ZOOM: {
21
- draft.zoomEnabled = !draft.zoomEnabled;
22
- if (draft.panEnabled) {
23
- draft.panEnabled = false;
24
- }
25
- if (draft.enableDragPoints) {
26
- draft.enableDragPoints = false;
27
- }
28
- break;
29
- }
30
- case TOGGLE_PAN: {
31
- draft.panEnabled = !draft.panEnabled;
32
- if (draft.zoomEnabled) {
33
- draft.zoomEnabled = false;
34
- }
35
- break;
36
- }
37
- case TOGGLE_POINTS: {
38
- draft.pointsEnabled = !draft.pointsEnabled;
39
- break;
40
- }
41
- case TOGGLE_LINE: {
42
- draft.lineEnabled = !draft.lineEnabled;
43
- break;
44
- }
45
- case TOGGLE_LEGEND: {
46
- draft.legendEnabled = !draft.legendEnabled;
47
- break;
48
- }
49
- case TOGGLE_TABLE: {
50
- draft.showTable = !draft.showTable;
51
- break;
52
- }
53
- case SAVE_INITIAL_AXES_RANGES: {
54
- const { initialAxesRanges } = action.payload;
55
- draft.initialAxesRanges = initialAxesRanges;
56
- break;
57
- }
58
- case UPDATE_AXES_RANGES: {
59
- const { axes } = action.payload;
60
- draft.axes = axes;
61
- break;
62
- }
63
- case RESET_AXES_RANGES: {
64
- const { initialAxesRanges } = draft;
65
- draft.axes = initialAxesRanges.map((axis) => ({
66
- id: axis.id,
67
- ...(axis.unit && { unit: axis.unit }),
68
- }));
69
- break;
70
- }
71
- case TOGGLE_ANNOTATION: {
72
- const { annotationIndex } = action.payload;
73
- const updatedIndexes = draft.showAnnotationLineIndex.includes(
74
- annotationIndex,
75
- )
76
- ? draft.showAnnotationLineIndex.filter((v) => v !== annotationIndex)
77
- : [...draft.showAnnotationLineIndex, annotationIndex];
78
- draft.showAnnotationLineIndex = updatedIndexes;
79
- break;
80
- }
81
- case TOGGLE_DRAG_POINTS: {
82
- draft.enableDragPoints = !draft.enableDragPoints;
83
- if (draft.panEnabled) {
84
- draft.panEnabled = false;
85
- }
86
- if (draft.zoomEnabled) {
87
- draft.zoomEnabled = false;
88
- }
89
- break;
90
- }
91
- case DISABLE_DRAG_OPTIONS: {
92
- if (draft.enableDragPoints || draft.panEnabled || draft.zoomEnabled) {
93
- draft.enableDragPoints = false;
94
- draft.panEnabled = false;
95
- draft.zoomEnabled = false;
96
- }
97
- break;
98
- }
99
- }
100
- });
101
- };
@@ -1,86 +0,0 @@
1
- /**
2
- * Retrieves the chart state from local storage.
3
- *
4
- * @param {string|''} persistenceId - The chart persistenceId.
5
- * @returns {object|null} The chart state object or null if not found.
6
- */
7
- export const getChartStateFromStorage = (persistenceId) => {
8
- if (!persistenceId) return null;
9
-
10
- // Retrieve the chart state object from local storage
11
- const chartStateKey = `line-chart-state-${persistenceId}`;
12
- const chartStateObjectJSON = localStorage.getItem(chartStateKey);
13
-
14
- if (chartStateObjectJSON) {
15
- // If the chart state object was found, parse it
16
- const chartStateObject = JSON.parse(chartStateObjectJSON);
17
-
18
- // Return the state property of the parsed chart state object
19
- return chartStateObject.state;
20
- }
21
-
22
- // If the chart state object was not found, return null
23
- return null;
24
- };
25
-
26
- /**
27
- * Remove expired chart states from local storage.
28
- *
29
- * @param {number} maxAgeInHours - The maximum age of chart states to keep in local storage (in hours). Default is 72 hours.
30
- */
31
- const removeExpiredChartStates = (maxAgeInHours = 72) => {
32
- const currentTime = new Date().getTime();
33
- const maxAgeInMilliseconds = maxAgeInHours * 60 * 60 * 1000;
34
-
35
- // Iterate through all keys in local storage
36
- for (let i = 0; i < localStorage.length; i++) {
37
- const key = localStorage.key(i);
38
-
39
- // Check if the key is related to a line-chart-state
40
- if (key.includes('line-chart-state-')) {
41
- const chartStateObjectJSON = localStorage.getItem(key);
42
-
43
- // If a valid chart state object JSON is found
44
- if (chartStateObjectJSON) {
45
- const chartStateObject = JSON.parse(chartStateObjectJSON);
46
- const storedTime = chartStateObject.timestamp;
47
-
48
- // If a valid timestamp is found
49
- if (storedTime) {
50
- const ageInMilliseconds = currentTime - storedTime;
51
-
52
- // If the age of the chart state is older than the specified maxAgeInHours
53
- if (ageInMilliseconds > maxAgeInMilliseconds) {
54
- // Remove the expired chart state from local storage
55
- localStorage.removeItem(key);
56
- }
57
- }
58
- }
59
- }
60
- }
61
- };
62
-
63
- /**
64
- * Stores the chart state in local storage.
65
- *
66
- * @param {object} state - The chart state object to store.
67
- * @param {string|''} persistenceId - The chart persistenceId.
68
- */
69
- export const storeChartStateInStorage = (state, persistenceId) => {
70
- if (!persistenceId) return;
71
-
72
- const currentTime = new Date().getTime();
73
- const chartStateKey = `line-chart-state-${persistenceId}`;
74
-
75
- // Create an object containing the chart state and the timestamp
76
- const chartStateObject = {
77
- state,
78
- timestamp: currentTime,
79
- };
80
-
81
- // Serialize the chart state object as a JSON string and store it in local storage
82
- localStorage.setItem(chartStateKey, JSON.stringify(chartStateObject));
83
-
84
- // Remove expired chart states from local storage
85
- removeExpiredChartStates();
86
- };