@oliasoft-open-source/charts-library 2.16.0-beta-4 → 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
|
+
"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
|
|
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.
|
|
@@ -191,32 +161,55 @@ const getTextAlign = (position) => {
|
|
|
191
161
|
}
|
|
192
162
|
};
|
|
193
163
|
|
|
194
|
-
|
|
195
|
-
|
|
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
|
+
};
|
|
196
188
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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;
|
|
201
197
|
|
|
202
|
-
|
|
198
|
+
const words = getWords(text);
|
|
199
|
+
const wrappedLines = wrapText(words, maxWidth, ctx);
|
|
203
200
|
|
|
204
|
-
|
|
205
|
-
|
|
201
|
+
ctx.save();
|
|
202
|
+
ctx.font = `${fontSize}px Arial`;
|
|
203
|
+
ctx.fillStyle = SEMI_TRANSPARENT;
|
|
204
|
+
ctx.textAlign = getTextAlign(position);
|
|
206
205
|
|
|
207
|
-
|
|
208
|
-
const lines = countLines(words, maxWidth, ctx);
|
|
206
|
+
drawText(ctx, wrappedLines, lineHeight, x, y, position);
|
|
209
207
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (options.position.includes('top')) {
|
|
213
|
-
chart.options.layout.padding.top = paddingNeeded;
|
|
214
|
-
} else {
|
|
215
|
-
chart.options.layout.padding.bottom = paddingNeeded;
|
|
216
|
-
}
|
|
208
|
+
ctx.restore();
|
|
209
|
+
};
|
|
217
210
|
|
|
218
|
-
|
|
219
|
-
|
|
211
|
+
export const chartAreaTextPlugin = {
|
|
212
|
+
id: 'chartAreaText',
|
|
220
213
|
|
|
221
214
|
beforeDraw: (chart, args, options) => {
|
|
222
215
|
const {
|
|
@@ -236,33 +229,18 @@ export const chartAreaTextPlugin = {
|
|
|
236
229
|
// Determine the maxWidth based on chartArea width
|
|
237
230
|
const maxWidth = calculateMaxWidth(initialMaxWidth, chartArea.width);
|
|
238
231
|
|
|
239
|
-
//
|
|
240
|
-
const words = getWords(text);
|
|
241
|
-
let line = '';
|
|
242
|
-
let lines = [];
|
|
243
|
-
for (let i = 0; i < words.length; i++) {
|
|
244
|
-
const testLine = `${line}${words[i]}${WORD_SEPARATOR}`;
|
|
245
|
-
const { width: testWidth } = ctx.measureText(testLine);
|
|
246
|
-
if (testWidth > maxWidth) {
|
|
247
|
-
lines.push(line.trim());
|
|
248
|
-
line = `${words[i]}${WORD_SEPARATOR}`;
|
|
249
|
-
} else {
|
|
250
|
-
line = testLine;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
lines.push(line.trim()); // Add the last line
|
|
254
|
-
|
|
255
|
-
// Wrap the text based on the maxWidth and draw it on the canvas
|
|
256
|
-
ctx.save();
|
|
257
|
-
ctx.font = `${fontSize}px Arial`;
|
|
258
|
-
ctx.fillStyle = TRANSPARENT;
|
|
259
|
-
ctx.textAlign = AlignOptions.Right;
|
|
260
|
-
|
|
232
|
+
// Get the position coordinates
|
|
261
233
|
const [x, y] = getPositionCoordinates(position, chart, xOffset, yOffset);
|
|
262
|
-
ctx.textAlign = getTextAlign(position);
|
|
263
234
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
235
|
+
// Render the wrapped text
|
|
236
|
+
renderWrappedText(ctx, {
|
|
237
|
+
text,
|
|
238
|
+
maxWidth,
|
|
239
|
+
fontSize,
|
|
240
|
+
lineHeight,
|
|
241
|
+
x,
|
|
242
|
+
y,
|
|
243
|
+
position,
|
|
244
|
+
});
|
|
267
245
|
},
|
|
268
246
|
};
|