@oliasoft-open-source/charts-library 2.16.0-beta-1 → 2.16.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oliasoft-open-source/charts-library",
3
- "version": "2.16.0-beta-1",
3
+ "version": "2.16.0-beta-2",
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
@@ -3,6 +3,9 @@
3
3
  ## 2.16.0
4
4
  - Added common chart area text plugin
5
5
 
6
+ ## 2.14.1
7
+ - Fix save initAxesRange when dataset changed by parent component([OW-11332](https://oliasoft.atlassian.net/browse/OW-11332))
8
+
6
9
  ## 2.14.0
7
10
  - Refactored line chart, for better performance, readable and reduce not needed re-renders
8
11
 
@@ -82,7 +82,14 @@ const LineChart = (props) => {
82
82
  }, [state.lineEnabled, state.pointsEnabled, axes, annotations, graph]);
83
83
 
84
84
  // Call the custom hooks.
85
- useChartState({ chartRef, options, state, dispatch, persistenceId });
85
+ useChartState({
86
+ chartRef,
87
+ options,
88
+ state,
89
+ generatedDatasets,
90
+ dispatch,
91
+ persistenceId,
92
+ });
86
93
 
87
94
  const { resetZoom, handleDownload, handleKeyDown, handleKeyUp } =
88
95
  useChartFunctions({
@@ -42,12 +42,41 @@ const countLines = (words, maxWidth, ctx) => {
42
42
  return lines;
43
43
  };
44
44
 
45
+ /**
46
+ * Renders the lines of text on the canvas context.
47
+ * It iterates over the array of lines and renders each line with the provided styling.
48
+ *
49
+ * @param {CanvasRenderingContext2D} ctx - The canvas rendering context to draw on.
50
+ * @param {string[]} lines - The array of lines to be rendered.
51
+ * @param {number} lineHeight - The height of each line.
52
+ * @param {number} x - The x-coordinate of the starting position.
53
+ * @param {number} y - The y-coordinate of the starting position.
54
+ */
45
55
  const drawText = (ctx, lines, lineHeight, x, y) => {
46
56
  lines.forEach((line, index) => {
47
57
  ctx.fillText(line, x, y - (lines.length - 1 - index) * lineHeight);
48
58
  });
49
59
  };
50
60
 
61
+ /**
62
+ * Calculates the maximum width for the text based on the initial maximum width
63
+ * and the chart area width.
64
+ * @param {number} initialMaxWidth - The initial maximum width.
65
+ * @param {number} chartAreaWidth - The width of the chart area.
66
+ * @returns {number} The updated maximum width.
67
+ */
68
+ const calculateMaxWidth = (initialMaxWidth, chartAreaWidth) => {
69
+ const factorMiddle = 0.5;
70
+ const factorSmall = 0.7;
71
+ const maxWidthFactor =
72
+ chartAreaWidth < 500
73
+ ? factorMiddle
74
+ : chartAreaWidth < 700
75
+ ? factorSmall
76
+ : 1;
77
+ return initialMaxWidth * maxWidthFactor;
78
+ };
79
+
51
80
  export const chartAreaTextPlugin = {
52
81
  id: 'chartAreaText',
53
82
 
@@ -72,18 +101,21 @@ export const chartAreaTextPlugin = {
72
101
  },
73
102
 
74
103
  beforeDraw: (chart, args, options) => {
75
- const { showLabel, text, fontSize, xOffset, yOffset, lineHeight } = options;
104
+ const {
105
+ showLabel,
106
+ text,
107
+ fontSize,
108
+ xOffset,
109
+ yOffset,
110
+ lineHeight,
111
+ maxWidth: initialMaxWidth,
112
+ } = options;
76
113
  const { ctx, chartArea } = chart;
77
114
 
78
115
  if (!showLabel || !text) return;
79
116
 
80
117
  // Determine the maxWidth based on chartArea width
81
- const maxWidth =
82
- chartArea.width < 500
83
- ? 100
84
- : chartArea.width < 700
85
- ? 200
86
- : options.maxWidth;
118
+ const maxWidth = calculateMaxWidth(initialMaxWidth, chartArea.width);
87
119
 
88
120
  // Split the text into words and calculate the number of lines
89
121
  const words = getWords(text);
@@ -75,26 +75,38 @@ const useUpdateAxesRanges = (range, dispatch) => {
75
75
  };
76
76
 
77
77
  /**
78
- * Hook for saving the initial axes ranges in the chart state.
78
+ * A custom hook to save the initial axes ranges of a chart when the generated datasets change.
79
79
  *
80
- * @param chartRef - The reference to the chart.
81
- * @param axes - The axes data.
82
- * @param dispatch - The dispatch function for state management.
80
+ * @param {Object} chartRef - A reference to the chart object.
81
+ * @param {Array} axes - An array of axes configurations.
82
+ * @param {Array} generatedDatasets - An array of generated datasets for the chart.
83
+ * @param {Function} dispatch - A dispatch function from the useContext hook for managing the state.
83
84
  */
84
- const useSaveInitialAxesRanges = (chartRef, axes, dispatch) => {
85
+ const useSaveInitialAxesRanges = (
86
+ chartRef,
87
+ axes,
88
+ generatedDatasets,
89
+ dispatch,
90
+ ) => {
91
+ const prevGeneratedDatasets = useRef();
92
+
85
93
  const saveInitialAxesRanges = useCallback(() => {
86
- if (chartRef) {
94
+ if (
95
+ chartRef &&
96
+ !isEqual(generatedDatasets, prevGeneratedDatasets.current)
97
+ ) {
87
98
  const initialAxesRanges = getAxesRangesFromChart(chartRef, axes);
88
99
  dispatch({
89
100
  type: SAVE_INITIAL_AXES_RANGES,
90
101
  payload: { initialAxesRanges },
91
102
  });
103
+ prevGeneratedDatasets.current = generatedDatasets;
92
104
  }
93
- }, [chartRef]);
105
+ }, [chartRef, generatedDatasets]);
94
106
 
95
107
  useEffect(() => {
96
108
  saveInitialAxesRanges();
97
- }, [saveInitialAxesRanges]);
109
+ }, [saveInitialAxesRanges, generatedDatasets]);
98
110
  };
99
111
 
100
112
  /**
@@ -103,6 +115,7 @@ const useSaveInitialAxesRanges = (chartRef, axes, dispatch) => {
103
115
  * @param chartRef - The reference to the chart.
104
116
  * @param options - The chart options.
105
117
  * @param state - The chart state.
118
+ * @param {Array} generatedDatasets - An array of generated datasets for the chart.
106
119
  * @param dispatch - The dispatch function for state management.
107
120
  * @param persistenceId - The chart persistenceId.
108
121
  */
@@ -110,6 +123,7 @@ export const useChartState = ({
110
123
  chartRef,
111
124
  options,
112
125
  state,
126
+ generatedDatasets,
113
127
  dispatch,
114
128
  persistenceId,
115
129
  }) => {
@@ -123,5 +137,5 @@ export const useChartState = ({
123
137
  useStoreChartStateInStorage(memoState, persistenceId);
124
138
  useToggleCustomLegendVisibility(memoState, memoOptions);
125
139
  useUpdateAxesRanges(range, dispatch);
126
- useSaveInitialAxesRanges(chartRef, axes, dispatch);
140
+ useSaveInitialAxesRanges(chartRef, axes, generatedDatasets, dispatch);
127
141
  };