@oliasoft-open-source/charts-library 2.16.0-beta-3 → 2.16.0-beta-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.16.0-beta-3",
3
+ "version": "2.16.0-beta-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": {
@@ -2,7 +2,7 @@ import { TextLabelPosition } from './plugin-constants';
2
2
  import { AlignOptions } from '../../../helpers/enums';
3
3
 
4
4
  const WORD_SEPARATOR = ' ';
5
- const TRANSPARENT = 'rgba(0, 0, 0, 0.5)';
5
+ const SEMI_TRANSPARENT = 'rgba(0, 0, 0, 0.5)';
6
6
 
7
7
  /**
8
8
  * Splits the input text into words based on the predefined WORD_SEPARATOR.
@@ -17,36 +17,6 @@ const getWords = (text) => {
17
17
  );
18
18
  };
19
19
 
20
- /**
21
- * Counts the number of lines required to render the words within the specified maxWidth.
22
- * It takes into consideration the context (ctx) provided to calculate the text width.
23
- *
24
- * @param {string[]} words - The array of words to be processed.
25
- * @param {number} maxWidth - The maximum width allowed for the text.
26
- * @param {CanvasRenderingContext2D} ctx - The canvas rendering context to measure the text width.
27
- * @returns {number} The number of lines required.
28
- */
29
- const countLines = (words, maxWidth, ctx) => {
30
- const lineCount = words.reduce(
31
- (lines, word) => {
32
- const testLine = `${lines.currentLine}${word}${WORD_SEPARATOR}`;
33
- const { width: testWidth } = ctx.measureText(testLine);
34
-
35
- if (testWidth > maxWidth) {
36
- return {
37
- lineCount: lines.lineCount + 1,
38
- currentLine: `${word}${WORD_SEPARATOR}`,
39
- };
40
- } else {
41
- return { lineCount: lines.lineCount, currentLine: testLine };
42
- }
43
- },
44
- { lineCount: 0, currentLine: '' },
45
- );
46
-
47
- return lineCount.lineCount + 1; // Add the last line
48
- };
49
-
50
20
  /**
51
21
  * Renders the lines of text on the canvas context.
52
22
  * It iterates over the array of lines and renders each line with the provided styling.
@@ -120,7 +90,8 @@ const getLegendDimensions = (chart) => {
120
90
  * @returns {number[]} An array with the X and Y coordinates.
121
91
  */
122
92
  const getPositionCoordinates = (position, chart, xOffset, yOffset) => {
123
- const { chartArea } = chart;
93
+ const { chartArea, width } = chart;
94
+ const temporaryChartAreaRight = width - 8;
124
95
 
125
96
  const { legendWidth, legendHeight } = getLegendDimensions(chart);
126
97
 
@@ -137,7 +108,7 @@ const getPositionCoordinates = (position, chart, xOffset, yOffset) => {
137
108
  ];
138
109
  case TextLabelPosition.TOP_RIGHT:
139
110
  return [
140
- chartArea.right - xOffset,
111
+ temporaryChartAreaRight - xOffset, //replace within chartArea.right when resize bug will be fixed
141
112
  chartArea.top + yOffset + legendHeight,
142
113
  ];
143
114
  case TextLabelPosition.MIDDLE_LEFT:
@@ -152,7 +123,7 @@ const getPositionCoordinates = (position, chart, xOffset, yOffset) => {
152
123
  ];
153
124
  case TextLabelPosition.MIDDLE_RIGHT:
154
125
  return [
155
- chartArea.right - xOffset - legendWidth,
126
+ temporaryChartAreaRight - xOffset - legendWidth, //replace within chartArea.right when resize bug will be fixed
156
127
  chartArea.top + chartArea.height / 2,
157
128
  ];
158
129
  case TextLabelPosition.BOTTOM_LEFT:
@@ -168,7 +139,7 @@ const getPositionCoordinates = (position, chart, xOffset, yOffset) => {
168
139
  case TextLabelPosition.BOTTOM_RIGHT:
169
140
  default:
170
141
  return [
171
- chartArea.right - xOffset - legendWidth,
142
+ temporaryChartAreaRight - xOffset - legendWidth, //replace within chartArea.right when resize bug will be fixed
172
143
  chartArea.bottom - yOffset - legendHeight,
173
144
  ];
174
145
  }
@@ -190,32 +161,55 @@ const getTextAlign = (position) => {
190
161
  }
191
162
  };
192
163
 
193
- export const chartAreaTextPlugin = {
194
- id: 'chartAreaText',
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
+ };
195
188
 
196
- beforeLayout: (chart, args, options) => {
197
- const { showLabel, text, fontStyle, fontSize, maxWidth, lineHeight } =
198
- options;
199
- const { ctx } = chart;
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;
200
197
 
201
- if (!showLabel || !text) return;
198
+ const words = getWords(text);
199
+ const wrappedLines = wrapText(words, maxWidth, ctx);
202
200
 
203
- ctx.save();
204
- ctx.font = `${fontStyle} ${fontSize}px Arial`;
201
+ ctx.save();
202
+ ctx.font = `${fontSize}px Arial`;
203
+ ctx.fillStyle = SEMI_TRANSPARENT;
204
+ ctx.textAlign = getTextAlign(position);
205
205
 
206
- const words = getWords(text);
207
- const lines = countLines(words, maxWidth, ctx);
206
+ drawText(ctx, wrappedLines, lineHeight, x, y, position);
208
207
 
209
- // Calculate and set the padding needed at the top or bottom of the chart
210
- const paddingNeeded = lines * lineHeight + lineHeight;
211
- if (options.position.includes('top')) {
212
- chart.options.layout.padding.top = paddingNeeded;
213
- } else {
214
- chart.options.layout.padding.bottom = paddingNeeded;
215
- }
208
+ ctx.restore();
209
+ };
216
210
 
217
- ctx.restore();
218
- },
211
+ export const chartAreaTextPlugin = {
212
+ id: 'chartAreaText',
219
213
 
220
214
  beforeDraw: (chart, args, options) => {
221
215
  const {
@@ -235,33 +229,18 @@ export const chartAreaTextPlugin = {
235
229
  // Determine the maxWidth based on chartArea width
236
230
  const maxWidth = calculateMaxWidth(initialMaxWidth, chartArea.width);
237
231
 
238
- // Split the text into words and calculate the number of lines
239
- const words = getWords(text);
240
- let line = '';
241
- let lines = [];
242
- for (let i = 0; i < words.length; i++) {
243
- const testLine = `${line}${words[i]}${WORD_SEPARATOR}`;
244
- const { width: testWidth } = ctx.measureText(testLine);
245
- if (testWidth > maxWidth) {
246
- lines.push(line.trim());
247
- line = `${words[i]}${WORD_SEPARATOR}`;
248
- } else {
249
- line = testLine;
250
- }
251
- }
252
- lines.push(line.trim()); // Add the last line
253
-
254
- // Wrap the text based on the maxWidth and draw it on the canvas
255
- ctx.save();
256
- ctx.font = `${fontSize}px Arial`;
257
- ctx.fillStyle = TRANSPARENT;
258
- ctx.textAlign = AlignOptions.Right;
259
-
232
+ // Get the position coordinates
260
233
  const [x, y] = getPositionCoordinates(position, chart, xOffset, yOffset);
261
- ctx.textAlign = getTextAlign(position);
262
234
 
263
- drawText(ctx, lines, lineHeight, x, y, position);
264
-
265
- ctx.restore();
235
+ // Render the wrapped text
236
+ renderWrappedText(ctx, {
237
+ text,
238
+ maxWidth,
239
+ fontSize,
240
+ lineHeight,
241
+ x,
242
+ y,
243
+ position,
244
+ });
266
245
  },
267
246
  };