@oliasoft-open-source/charts-library 2.13.4 → 2.13.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/charts-library",
3
- "version": "2.13.4",
3
+ "version": "2.13.5",
4
4
  "description": "React Chart Library (based on Chart.js and react-chart-js-2)",
5
5
  "homepage": "https://gitlab.com/oliasoft-open-source/charts-library",
6
6
  "bugs": {
package/release-notes.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Charts Library Release Notes
2
2
 
3
+ ## 2.13.5
4
+
5
+ - Fixed reset range when subComponent update data, prevent extra re-renders when range have same deep values but different reference
6
+
3
7
  ## 2.13.4
4
8
 
5
9
  - Fixed legend state
@@ -1,10 +1,4 @@
1
- import React, {
2
- useEffect,
3
- useLayoutEffect,
4
- useReducer,
5
- useRef,
6
- useState,
7
- } from 'react';
1
+ import React, { useReducer, useRef, useState } from 'react';
8
2
  import {
9
3
  CategoryScale,
10
4
  Chart as ChartJS,
@@ -29,7 +23,6 @@ import initialState from './state/initial-state';
29
23
  import {
30
24
  DISABLE_DRAG_OPTIONS,
31
25
  RESET_AXES_RANGES,
32
- SAVE_INITIAL_AXES_RANGES,
33
26
  TOGGLE_ANNOTATION,
34
27
  TOGGLE_DRAG_POINTS,
35
28
  TOGGLE_LEGEND,
@@ -80,8 +73,7 @@ import {
80
73
  import getDraggableData from '../../helpers/get-draggableData';
81
74
  import { generateAxisId, generateKey } from './line-chart-utils';
82
75
  import { autoScale } from './axis-scales/axis-scales';
83
- import useChartState from './state/use-chart-state';
84
- import { getAxesRangesFromChart } from './get-axes-ranges-from-chart';
76
+ import { useChartState } from './state/use-chart-state';
85
77
 
86
78
  ChartJS.register(
87
79
  LinearScale,
@@ -134,36 +126,8 @@ const LineChart = (props) => {
134
126
  initialState,
135
127
  );
136
128
 
137
- useLayoutEffect(() => {
138
- const { range } = props.chart.options.additionalAxesOptions;
139
- if (range?.x && range?.y) {
140
- const axes = Object.entries(range).map(([key, { min, max }]) => {
141
- return {
142
- id: key,
143
- min: min ?? 0,
144
- max: max ?? 0,
145
- };
146
- });
147
-
148
- dispatch({
149
- type: UPDATE_AXES_RANGES,
150
- payload: { axes },
151
- });
152
- }
153
- }, [props.chart.options]);
154
-
155
- useEffect(() => {
156
- if (chartRef) {
157
- //save the initial axis ranges in state (we need this for resetting ranges)
158
- dispatch({
159
- type: SAVE_INITIAL_AXES_RANGES,
160
- payload: { initialAxesRanges: getAxesRangesFromChart(chartRef, axes) },
161
- });
162
- }
163
- }, []);
164
-
165
129
  // Call the custom hook.
166
- useChartState(options, state, persistenceId);
130
+ useChartState({ chartRef, options, state, dispatch, persistenceId });
167
131
 
168
132
  const generateLineChartDatasets = (datasets) => {
169
133
  const copyDataset = [...datasets];
@@ -1,5 +1,8 @@
1
- import { useEffect, useMemo } from 'react';
1
+ import { useCallback, useEffect, useMemo, useRef } from 'react';
2
+ import { isEqual } from 'lodash';
2
3
  import { storeChartStateInStorage } from './manage-state-in-local-storage';
4
+ import { SAVE_INITIAL_AXES_RANGES, UPDATE_AXES_RANGES } from './action-types';
5
+ import { getAxesRangesFromChart } from '../get-axes-ranges-from-chart';
3
6
 
4
7
  /**
5
8
  * Hook for toggling the visibility of the custom legend.
@@ -42,25 +45,82 @@ const useStoreChartStateInStorage = (memoState, persistenceId) => {
42
45
  ]);
43
46
  };
44
47
 
48
+ /**
49
+ * Hook for updating the axes ranges in the chart state.
50
+ *
51
+ * @param range - The range to the chart.
52
+ * @param dispatch - The dispatch function for state management.
53
+ */
54
+ const useUpdateAxesRanges = (range, dispatch) => {
55
+ const prevRange = useRef();
56
+
57
+ const updateAxesRanges = useCallback(() => {
58
+ if (range && range.x && range.y && !isEqual(range, prevRange.current)) {
59
+ const axes = Object.entries(range).map(([key, { min, max }]) => ({
60
+ id: key,
61
+ min: min ?? 0,
62
+ max: max ?? 0,
63
+ }));
64
+ dispatch({
65
+ type: UPDATE_AXES_RANGES,
66
+ payload: { axes },
67
+ });
68
+ prevRange.current = range;
69
+ }
70
+ }, [range]);
71
+
72
+ useEffect(() => {
73
+ updateAxesRanges();
74
+ }, [updateAxesRanges]);
75
+ };
76
+
77
+ /**
78
+ * Hook for saving the initial axes ranges in the chart state.
79
+ *
80
+ * @param chartRef - The reference to the chart.
81
+ * @param axes - The axes data.
82
+ * @param dispatch - The dispatch function for state management.
83
+ */
84
+ const useSaveInitialAxesRanges = (chartRef, axes, dispatch) => {
85
+ const saveInitialAxesRanges = useCallback(() => {
86
+ if (chartRef) {
87
+ const initialAxesRanges = getAxesRangesFromChart(chartRef, axes);
88
+ dispatch({
89
+ type: SAVE_INITIAL_AXES_RANGES,
90
+ payload: { initialAxesRanges },
91
+ });
92
+ }
93
+ }, [chartRef]);
94
+
95
+ useEffect(() => {
96
+ saveInitialAxesRanges();
97
+ }, [saveInitialAxesRanges]);
98
+ };
99
+
45
100
  /**
46
101
  * Hook for managing the state of the chart.
47
102
  *
103
+ * @param chartRef - The reference to the chart.
48
104
  * @param options - The chart options.
49
105
  * @param state - The chart state.
50
106
  * @param persistenceId - The chart persistenceId.
51
107
  */
52
- const useChartState = (options, state, persistenceId) => {
53
- const memoized = useMemo(() => {
54
- return {
55
- state,
56
- options,
57
- };
58
- }, [state, options]);
59
-
60
- const { state: memoState, options: memoOptions } = memoized ?? {};
108
+ export const useChartState = ({
109
+ chartRef,
110
+ options,
111
+ state,
112
+ dispatch,
113
+ persistenceId,
114
+ }) => {
115
+ const memoState = useMemo(() => state, [state]);
116
+ const memoOptions = useMemo(() => options, [options]);
117
+ const {
118
+ additionalAxesOptions: { range },
119
+ axes,
120
+ } = memoOptions;
61
121
 
62
122
  useStoreChartStateInStorage(memoState, persistenceId);
63
123
  useToggleCustomLegendVisibility(memoState, memoOptions);
124
+ useUpdateAxesRanges(range, dispatch);
125
+ useSaveInitialAxesRanges(chartRef, axes, dispatch);
64
126
  };
65
-
66
- export default useChartState;