apexify.js 4.8.2 → 4.8.3

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 (128) hide show
  1. package/dist/tsconfig.cjs.tsbuildinfo +1 -1
  2. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  3. package/lib/ai/ApexAI.ts +4 -4
  4. package/lib/ai/ApexModules.ts +8 -8
  5. package/lib/ai/modals/electronHub/chatmodels.ts +1 -1
  6. package/lib/ai/modals/electronHub/imageModels.ts +2 -2
  7. package/lib/ai/modals/electronHub/speechModels.ts +1 -1
  8. package/lib/ai/utils.ts +3 -3
  9. package/lib/canvas/ApexPainter.ts +1 -1
  10. package/lib/canvas/utils/Background/bg.ts +1 -1
  11. package/lib/canvas/utils/Charts/charts.ts +1 -1
  12. package/lib/canvas/utils/Custom/customLines.ts +2 -2
  13. package/lib/canvas/utils/General/general functions.ts +1 -1
  14. package/lib/canvas/utils/Image/imageProperties.ts +1 -1
  15. package/lib/canvas/utils/Texts/textProperties.ts +1 -1
  16. package/lib/canvas/utils/utils.ts +8 -8
  17. package/lib/index.ts +5 -5
  18. package/lib/utils.ts +3 -3
  19. package/package.json +5 -5
  20. package/dist/cjs/ai/ApexAI.d.ts +0 -144
  21. package/dist/cjs/ai/ApexAI.js +0 -486
  22. package/dist/cjs/ai/ApexModules.d.ts +0 -52
  23. package/dist/cjs/ai/ApexModules.js +0 -811
  24. package/dist/cjs/ai/functions/readFiles.d.ts +0 -1
  25. package/dist/cjs/ai/functions/readFiles.js +0 -56
  26. package/dist/cjs/ai/functions/tokenizer.d.ts +0 -9
  27. package/dist/cjs/ai/functions/tokenizer.js +0 -60
  28. package/dist/cjs/ai/functions/validOptions.d.ts +0 -21
  29. package/dist/cjs/ai/functions/validOptions.js +0 -93
  30. package/dist/cjs/ai/modals/electronHub/chatmodels.d.ts +0 -6
  31. package/dist/cjs/ai/modals/electronHub/chatmodels.js +0 -44
  32. package/dist/cjs/ai/modals/electronHub/imageModels.d.ts +0 -11
  33. package/dist/cjs/ai/modals/electronHub/imageModels.js +0 -85
  34. package/dist/cjs/ai/modals/electronHub/songModels.d.ts +0 -1
  35. package/dist/cjs/ai/modals/electronHub/songModels.js +0 -1
  36. package/dist/cjs/ai/modals/electronHub/speechModels.d.ts +0 -6
  37. package/dist/cjs/ai/modals/electronHub/speechModels.js +0 -56
  38. package/dist/cjs/ai/modals/electronHub/videoModels.d.ts +0 -5
  39. package/dist/cjs/ai/modals/electronHub/videoModels.js +0 -56
  40. package/dist/cjs/ai/modals/groq/chatgroq.d.ts +0 -8
  41. package/dist/cjs/ai/modals/groq/chatgroq.js +0 -57
  42. package/dist/cjs/ai/modals/groq/imageAnalyzer.d.ts +0 -7
  43. package/dist/cjs/ai/modals/groq/imageAnalyzer.js +0 -75
  44. package/dist/cjs/ai/modals/groq/whisper.d.ts +0 -4
  45. package/dist/cjs/ai/modals/groq/whisper.js +0 -101
  46. package/dist/cjs/ai/modals/hercai/chatModels.d.ts +0 -6
  47. package/dist/cjs/ai/modals/hercai/chatModels.js +0 -19
  48. package/dist/cjs/ai/utils.d.ts +0 -4
  49. package/dist/cjs/ai/utils.js +0 -4
  50. package/dist/cjs/canvas/ApexPainter.d.ts +0 -144
  51. package/dist/cjs/canvas/ApexPainter.js +0 -900
  52. package/dist/cjs/canvas/utils/Background/bg.d.ts +0 -30
  53. package/dist/cjs/canvas/utils/Background/bg.js +0 -151
  54. package/dist/cjs/canvas/utils/Charts/charts.d.ts +0 -7
  55. package/dist/cjs/canvas/utils/Charts/charts.js +0 -455
  56. package/dist/cjs/canvas/utils/Custom/customLines.d.ts +0 -2
  57. package/dist/cjs/canvas/utils/Custom/customLines.js +0 -105
  58. package/dist/cjs/canvas/utils/General/conversion.d.ts +0 -5
  59. package/dist/cjs/canvas/utils/General/conversion.js +0 -26
  60. package/dist/cjs/canvas/utils/General/general functions.d.ts +0 -38
  61. package/dist/cjs/canvas/utils/General/general functions.js +0 -590
  62. package/dist/cjs/canvas/utils/Image/imageProperties.d.ts +0 -114
  63. package/dist/cjs/canvas/utils/Image/imageProperties.js +0 -590
  64. package/dist/cjs/canvas/utils/Texts/textProperties.d.ts +0 -16
  65. package/dist/cjs/canvas/utils/Texts/textProperties.js +0 -154
  66. package/dist/cjs/canvas/utils/types.d.ts +0 -621
  67. package/dist/cjs/canvas/utils/types.js +0 -5
  68. package/dist/cjs/canvas/utils/utils.d.ts +0 -18
  69. package/dist/cjs/canvas/utils/utils.js +0 -17
  70. package/dist/cjs/index.d.ts +0 -28
  71. package/dist/cjs/index.js +0 -67
  72. package/dist/cjs/utils.d.ts +0 -4
  73. package/dist/cjs/utils.js +0 -4
  74. package/dist/esm/ai/ApexAI.d.ts +0 -144
  75. package/dist/esm/ai/ApexAI.js +0 -486
  76. package/dist/esm/ai/ApexModules.d.ts +0 -52
  77. package/dist/esm/ai/ApexModules.js +0 -811
  78. package/dist/esm/ai/functions/readFiles.d.ts +0 -1
  79. package/dist/esm/ai/functions/readFiles.js +0 -56
  80. package/dist/esm/ai/functions/tokenizer.d.ts +0 -9
  81. package/dist/esm/ai/functions/tokenizer.js +0 -60
  82. package/dist/esm/ai/functions/validOptions.d.ts +0 -21
  83. package/dist/esm/ai/functions/validOptions.js +0 -93
  84. package/dist/esm/ai/modals/electronHub/chatmodels.d.ts +0 -6
  85. package/dist/esm/ai/modals/electronHub/chatmodels.js +0 -44
  86. package/dist/esm/ai/modals/electronHub/imageModels.d.ts +0 -11
  87. package/dist/esm/ai/modals/electronHub/imageModels.js +0 -85
  88. package/dist/esm/ai/modals/electronHub/songModels.d.ts +0 -1
  89. package/dist/esm/ai/modals/electronHub/songModels.js +0 -1
  90. package/dist/esm/ai/modals/electronHub/speechModels.d.ts +0 -6
  91. package/dist/esm/ai/modals/electronHub/speechModels.js +0 -56
  92. package/dist/esm/ai/modals/electronHub/videoModels.d.ts +0 -5
  93. package/dist/esm/ai/modals/electronHub/videoModels.js +0 -56
  94. package/dist/esm/ai/modals/groq/chatgroq.d.ts +0 -8
  95. package/dist/esm/ai/modals/groq/chatgroq.js +0 -57
  96. package/dist/esm/ai/modals/groq/imageAnalyzer.d.ts +0 -7
  97. package/dist/esm/ai/modals/groq/imageAnalyzer.js +0 -75
  98. package/dist/esm/ai/modals/groq/whisper.d.ts +0 -4
  99. package/dist/esm/ai/modals/groq/whisper.js +0 -101
  100. package/dist/esm/ai/modals/hercai/chatModels.d.ts +0 -6
  101. package/dist/esm/ai/modals/hercai/chatModels.js +0 -19
  102. package/dist/esm/ai/utils.d.ts +0 -4
  103. package/dist/esm/ai/utils.js +0 -4
  104. package/dist/esm/canvas/ApexPainter.d.ts +0 -144
  105. package/dist/esm/canvas/ApexPainter.js +0 -900
  106. package/dist/esm/canvas/utils/Background/bg.d.ts +0 -30
  107. package/dist/esm/canvas/utils/Background/bg.js +0 -151
  108. package/dist/esm/canvas/utils/Charts/charts.d.ts +0 -7
  109. package/dist/esm/canvas/utils/Charts/charts.js +0 -455
  110. package/dist/esm/canvas/utils/Custom/customLines.d.ts +0 -2
  111. package/dist/esm/canvas/utils/Custom/customLines.js +0 -105
  112. package/dist/esm/canvas/utils/General/conversion.d.ts +0 -5
  113. package/dist/esm/canvas/utils/General/conversion.js +0 -26
  114. package/dist/esm/canvas/utils/General/general functions.d.ts +0 -38
  115. package/dist/esm/canvas/utils/General/general functions.js +0 -590
  116. package/dist/esm/canvas/utils/Image/imageProperties.d.ts +0 -114
  117. package/dist/esm/canvas/utils/Image/imageProperties.js +0 -590
  118. package/dist/esm/canvas/utils/Texts/textProperties.d.ts +0 -16
  119. package/dist/esm/canvas/utils/Texts/textProperties.js +0 -154
  120. package/dist/esm/canvas/utils/types.d.ts +0 -621
  121. package/dist/esm/canvas/utils/types.js +0 -5
  122. package/dist/esm/canvas/utils/utils.d.ts +0 -18
  123. package/dist/esm/canvas/utils/utils.js +0 -17
  124. package/dist/esm/index.d.ts +0 -28
  125. package/dist/esm/index.js +0 -67
  126. package/dist/esm/utils.d.ts +0 -4
  127. package/dist/esm/utils.js +0 -4
  128. package/lib/ai/modals/electronHub/songModels.ts +0 -0
@@ -1,30 +0,0 @@
1
- import { SKRSContext2D } from "@napi-rs/canvas";
2
- import { CanvasConfig } from "../types.js";
3
- interface ZoomConfig {
4
- scale?: number;
5
- x?: number;
6
- y?: number;
7
- }
8
- /**
9
- * Draws a gradient background on the canvas with optional zoom support.
10
- * @param ctx The canvas rendering context.
11
- * @param canvas The canvas configuration object.
12
- * @param zoom Optional zoom configuration.
13
- */
14
- export declare function drawBackgroundGradient(ctx: SKRSContext2D, canvas: CanvasConfig, zoom?: ZoomConfig, blur?: number): Promise<void>;
15
- /**
16
- * Draws a solid background color on the canvas with optional zoom effect.
17
- * @param ctx The canvas rendering context.
18
- * @param canvas The canvas configuration object.
19
- * @param zoom Optional zoom configuration.
20
- */
21
- export declare function drawBackgroundColor(ctx: SKRSContext2D, canvas: CanvasConfig, zoom?: ZoomConfig, blur?: number): Promise<void>;
22
- /**
23
- * Draws a custom background image on the canvas with optional zoom functionality.
24
- * @param ctx The canvas rendering context.
25
- * @param canvas The canvas configuration object.
26
- * @param zoom Optional zoom configuration.
27
- */
28
- export declare function customBackground(ctx: SKRSContext2D, canvas: CanvasConfig, zoom?: ZoomConfig, blur?: number): Promise<void>;
29
- export declare function backgroundRadius(ctx: SKRSContext2D, x: number, y: number, width: number, height: number, borderRadius: number | "circular", borderPosition?: string): void;
30
- export {};
@@ -1,151 +0,0 @@
1
- import { loadImage } from "@napi-rs/canvas";
2
- import path from "path";
3
- /**
4
- * Draws a gradient background on the canvas with optional zoom support.
5
- * @param ctx The canvas rendering context.
6
- * @param canvas The canvas configuration object.
7
- * @param zoom Optional zoom configuration.
8
- */
9
- export async function drawBackgroundGradient(ctx, canvas, zoom, blur) {
10
- if (canvas.gradientBg) {
11
- const { type, startX = 0, startY = 0, endX = canvas.width || 500, endY = canvas.height || 500, startRadius = 0, endRadius = 0, colors } = canvas.gradientBg;
12
- if (!colors || colors.length === 0) {
13
- throw new Error("You need to provide colors for the gradient.");
14
- }
15
- let gradient;
16
- if (type === "linear" || type === undefined) {
17
- let finalStartX = startX, finalStartY = startY, finalEndX = endX, finalEndY = endY;
18
- if (zoom) {
19
- const scale = Math.max(1, zoom.scale || 1);
20
- finalStartX = (zoom.x || 0) - (endX - startX) / (2 * scale);
21
- finalStartY = (zoom.y || 0) - (endY - startY) / (2 * scale);
22
- finalEndX = (zoom.x || 0) + (endX - startX) / (2 * scale);
23
- finalEndY = (zoom.y || 0) + (endY - startY) / (2 * scale);
24
- }
25
- gradient = ctx.createLinearGradient(finalStartX, finalStartY, finalEndX, finalEndY);
26
- }
27
- else if (type === "radial") {
28
- gradient = ctx.createRadialGradient(startX, startY, startRadius, endX, endY, endRadius);
29
- }
30
- else {
31
- throw new Error("Unsupported gradient type.");
32
- }
33
- for (const { stop, color } of colors) {
34
- gradient.addColorStop(stop, color);
35
- }
36
- if (blur) {
37
- ctx.filter = `blur(${blur}px)`;
38
- }
39
- ctx.fillStyle = gradient;
40
- ctx.fillRect(0, 0, canvas.width || 500, canvas.height || 500);
41
- ctx.filter = "none";
42
- }
43
- }
44
- /**
45
- * Draws a solid background color on the canvas with optional zoom effect.
46
- * @param ctx The canvas rendering context.
47
- * @param canvas The canvas configuration object.
48
- * @param zoom Optional zoom configuration.
49
- */
50
- export async function drawBackgroundColor(ctx, canvas, zoom, blur) {
51
- const canvasWidth = canvas.width || 500;
52
- const canvasHeight = canvas.height || 500;
53
- ctx.save();
54
- if (blur) {
55
- ctx.filter = `blur(${blur}px)`;
56
- }
57
- if (zoom) {
58
- const scale = Math.max(1, zoom.scale || 1);
59
- const zoomedWidth = canvasWidth / scale;
60
- const zoomedHeight = canvasHeight / scale;
61
- ctx.scale(scale, scale);
62
- ctx.translate((zoom.x || 0) + zoomedWidth / 2, (zoom.y || 0) + zoomedHeight / 2);
63
- }
64
- if (canvas.colorBg !== "transparent") {
65
- ctx.fillStyle = canvas.colorBg;
66
- ctx.fillRect(0, 0, canvasWidth, canvasHeight);
67
- }
68
- ctx.filter = "none";
69
- ctx.restore();
70
- }
71
- /**
72
- * Draws a custom background image on the canvas with optional zoom functionality.
73
- * @param ctx The canvas rendering context.
74
- * @param canvas The canvas configuration object.
75
- * @param zoom Optional zoom configuration.
76
- */
77
- export async function customBackground(ctx, canvas, zoom, blur) {
78
- if (canvas.customBg) {
79
- try {
80
- let imageBuffer;
81
- if (canvas.customBg.startsWith("http")) {
82
- imageBuffer = canvas.customBg;
83
- }
84
- else {
85
- imageBuffer = path.join(process.cwd(), canvas.customBg);
86
- }
87
- const image = await loadImage(imageBuffer);
88
- const imgWidth = image.width;
89
- const imgHeight = image.height;
90
- const canvasWidth = canvas.width || imgWidth;
91
- const canvasHeight = canvas.height || imgHeight;
92
- if (blur) {
93
- ctx.filter = `blur(${blur}px)`;
94
- }
95
- if (zoom) {
96
- const scale = Math.max(1, zoom.scale || 1);
97
- const cropWidth = imgWidth / scale;
98
- const cropHeight = imgHeight / scale;
99
- let sx = (zoom.x || 0) - cropWidth / 2;
100
- let sy = (zoom.y || 0) - cropHeight / 2;
101
- sx = Math.max(0, Math.min(sx, imgWidth - cropWidth));
102
- sy = Math.max(0, Math.min(sy, imgHeight - cropHeight));
103
- ctx.drawImage(image, sx, sy, cropWidth, cropHeight, 0, 0, canvasWidth, canvasHeight);
104
- }
105
- else {
106
- ctx.drawImage(image, 0, 0, canvasWidth, canvasHeight);
107
- }
108
- ctx.filter = "none";
109
- }
110
- catch (error) {
111
- console.error("Error loading custom background image:", error.message);
112
- }
113
- }
114
- }
115
- export function backgroundRadius(ctx, x, y, width, height, borderRadius, borderPosition = "all") {
116
- ctx.beginPath();
117
- if (borderRadius === "circular") {
118
- const circleRadius = Math.min(width, height) / 2;
119
- ctx.arc(x + width / 2, y + height / 2, circleRadius, 0, 2 * Math.PI);
120
- }
121
- else if (typeof borderRadius === "number" && borderRadius > 0) {
122
- const br = Math.min(borderRadius, width / 2, height / 2);
123
- const selectedPositions = new Set(borderPosition.toLowerCase().split(",").map((s) => s.trim()));
124
- const roundTopLeft = selectedPositions.has("all") || selectedPositions.has("top-left") || (selectedPositions.has("top") && selectedPositions.has("left"));
125
- const roundTopRight = selectedPositions.has("all") || selectedPositions.has("top-right") || (selectedPositions.has("top") && selectedPositions.has("right"));
126
- const roundBottomRight = selectedPositions.has("all") || selectedPositions.has("bottom-right") || (selectedPositions.has("bottom") && selectedPositions.has("right"));
127
- const roundBottomLeft = selectedPositions.has("all") || selectedPositions.has("bottom-left") || (selectedPositions.has("bottom") && selectedPositions.has("left"));
128
- const tl = roundTopLeft ? br : 0;
129
- const tr = roundTopRight ? br : 0;
130
- const brR = roundBottomRight ? br : 0;
131
- const bl = roundBottomLeft ? br : 0;
132
- ctx.moveTo(x + tl, y);
133
- ctx.lineTo(x + width - tr, y);
134
- if (tr > 0)
135
- ctx.arc(x + width - tr, y + tr, tr, -Math.PI / 2, 0, false);
136
- ctx.lineTo(x + width, y + height - brR);
137
- if (brR > 0)
138
- ctx.arc(x + width - brR, y + height - brR, brR, 0, Math.PI / 2, false);
139
- ctx.lineTo(x + bl, y + height);
140
- if (bl > 0)
141
- ctx.arc(x + bl, y + height - bl, bl, Math.PI / 2, Math.PI, false);
142
- ctx.lineTo(x, y + tl);
143
- if (tl > 0)
144
- ctx.arc(x + tl, y + tl, tl, Math.PI, -Math.PI / 2, false);
145
- }
146
- else {
147
- ctx.rect(x, y, width, height);
148
- }
149
- ctx.closePath();
150
- ctx.clip();
151
- }
@@ -1,7 +0,0 @@
1
- import { barChart_1, PieChartData, LineChartConfig, DataPoint } from "../types.js";
2
- export declare function verticalBarChart(data: barChart_1): Promise<Buffer<ArrayBufferLike> | undefined>;
3
- export declare function pieChart(pieChartData: PieChartData): Promise<Buffer>;
4
- export declare function lineChart(data: {
5
- data: DataPoint[][];
6
- lineConfig: LineChartConfig;
7
- }): Promise<Buffer>;
@@ -1,455 +0,0 @@
1
- import { createCanvas, loadImage } from '@napi-rs/canvas';
2
- import path from 'path';
3
- ////////////////////////////////////////BAR CHARTS////////////////////////////////////////
4
- export async function verticalBarChart(data) {
5
- try {
6
- const { chartData, xLabels, yLabels, data: { xAxis, yAxis, keys, xTitle, yTitle, labelStyle } } = data;
7
- if (!xLabels || !yLabels || !xAxis || !yAxis) {
8
- throw new Error('Required data is missing.');
9
- }
10
- xAxis.forEach(bar => {
11
- if (bar.position.startsXLabel < Math.min(...xLabels) || bar.position.endsXLabel > Math.max(...xLabels)) {
12
- throw new Error(`X-axis value range for bar '${bar.label}' is invalid.`);
13
- }
14
- if (bar.value < Math.min(...yAxis) || bar.value > Math.max(...yAxis)) {
15
- throw new Error(`Y-axis value for bar '${bar.label}' is out of range.`);
16
- }
17
- });
18
- const canvasWidth = chartData?.width || 800;
19
- const canvasHeight = chartData?.height || 600;
20
- let img;
21
- if ((chartData?.widthPerc || 0.8) > 1 || (chartData?.widthPerc || 0.8) < 0)
22
- throw new Error(`widthPerc: Cannor be bigger than 1 or smaller than 0`);
23
- if ((chartData?.heightPerc || 0.8) > 1 || (chartData?.heightPerc || 0.8) < 0)
24
- throw new Error(`widthPerc: Cannor be bigger than 1 or smaller than 0`);
25
- const chartWidth = canvasWidth * (chartData?.widthPerc || 0.8);
26
- const chartHeight = canvasHeight * (chartData?.heightPerc || 0.8);
27
- if (chartData?.bg?.image) {
28
- try {
29
- let img;
30
- if (chartData.bg.image.startsWith('http')) {
31
- const response = await fetch(chartData.bg.image);
32
- if (!response.ok) {
33
- throw new Error("Failed to fetch image.");
34
- }
35
- const buffer = await response.arrayBuffer();
36
- img = await loadImage(Buffer.from(buffer));
37
- }
38
- else {
39
- const imagePath = path.join(process.cwd(), chartData.bg.image);
40
- img = await loadImage(imagePath);
41
- }
42
- }
43
- catch (error) {
44
- console.error('Error loading image:', error);
45
- }
46
- }
47
- const canvas = createCanvas(800, 600);
48
- const ctx = canvas.getContext('2d');
49
- if (chartData?.bg?.image) {
50
- ctx.drawImage(img, 0, 0, 800, 600);
51
- }
52
- else {
53
- ctx.fillStyle = chartData?.bg?.bgColor || 'white';
54
- ctx.fillRect(0, 0, canvasWidth, canvasHeight);
55
- }
56
- ctx.fillStyle = `${chartData?.title?.color || 'black'}`;
57
- ctx.font = `bold ${chartData?.title?.size || 12}px Arial`;
58
- ctx.fillText(chartData?.title?.title || 'Sample Chart', (canvasWidth - ctx.measureText(chartData?.title?.title || 'Sample Chart').width) / 2, 30);
59
- if (keys && Object.keys(keys).length > 0) {
60
- const keysTopMargin = 70;
61
- const keysLeftMargin = chartWidth + 60;
62
- const keyWidth = 20;
63
- const keyHeight = 20;
64
- const keySpacing = 10;
65
- let keyIndex = 0;
66
- for (const color in keys) {
67
- const keyX = keysLeftMargin;
68
- const keyY = keysTopMargin + keyIndex * (keyHeight + keySpacing);
69
- const keyColor = color || 'blue';
70
- const keyLabel = keys[color].length > 15 ? keys[color].substring(0, 15) + '...' : keys[color];
71
- ctx.fillStyle = keyColor;
72
- ctx.fillRect(keyX, keyY, keyWidth, keyHeight);
73
- ctx.fillStyle = `${keyColor || 'black'}`;
74
- ctx.font = '12px Arial';
75
- ctx.fillText(keyLabel, keyX + keyWidth + 5, keyY + keyHeight - 3);
76
- keyIndex++;
77
- }
78
- }
79
- const chartTopMargin = 50;
80
- const chartLeftMargin = 50;
81
- ctx.strokeStyle = chartData?.grid?.color || 'gray';
82
- ctx.lineWidth = 1;
83
- ctx.beginPath();
84
- ctx.moveTo(chartLeftMargin, chartTopMargin);
85
- ctx.lineTo(chartLeftMargin, chartTopMargin + chartHeight);
86
- ctx.lineTo(chartLeftMargin + chartWidth, chartTopMargin + chartHeight);
87
- ctx.stroke();
88
- if (chartData?.grid?.enable) {
89
- drawGridLines(ctx, chartLeftMargin, chartTopMargin, chartWidth, chartHeight, xLabels, yLabels, chartData.grid);
90
- }
91
- ctx.fillStyle = `${chartData?.labels?.color || 'black'}`;
92
- ctx.font = `bold ${chartData?.labels?.fontSize || 16}px Arial`;
93
- xLabels.forEach((label, index) => {
94
- const x = chartLeftMargin + index * (chartWidth / (xLabels.length - 1));
95
- const y = chartTopMargin + chartHeight + 20;
96
- ctx.fillText(String(label), x - ctx.measureText(String(label)).width / 2, y);
97
- });
98
- ctx.textAlign = 'right';
99
- yLabels.forEach((label, index) => {
100
- const x = chartLeftMargin - 5;
101
- const y = chartTopMargin + chartHeight - index * (chartHeight / (yLabels.length - 1));
102
- ctx.fillText(String(label), x, y + 5);
103
- });
104
- ctx.fillStyle = `${chartData?.axis?.color || 'black'}`;
105
- ctx.font = `bold ${chartData?.axis?.size || 12}px Arial`;
106
- ctx.fillText(xTitle || 'X Axis', 30 + canvasWidth / 2, canvasHeight - 15);
107
- ctx.save();
108
- ctx.translate(10, canvasHeight / 2);
109
- ctx.rotate(-Math.PI / 2);
110
- ctx.textAlign = 'center';
111
- ctx.fillText(yTitle || 'Y Axis', 0, 10);
112
- ctx.restore();
113
- xAxis.forEach((bar, index) => {
114
- const startX = chartLeftMargin + ((bar.position?.startsXLabel - Math.min(...xLabels)) / (Math.max(...xLabels) - Math.min(...xLabels))) * chartWidth;
115
- const endX = chartLeftMargin + ((bar.position?.endsXLabel - Math.min(...xLabels)) / (Math.max(...xLabels) - Math.min(...xLabels))) * chartWidth;
116
- const barWidth = endX - startX;
117
- const barHeight = (bar.value / Math.max(...yAxis)) * chartHeight;
118
- const y = chartTopMargin + chartHeight - barHeight;
119
- ctx.fillStyle = bar.barColor || 'blue';
120
- ctx.fillRect(startX, y, barWidth, barHeight);
121
- ctx.strokeStyle = bar?.stroke?.color || 'black';
122
- ctx.lineWidth = bar?.stroke?.width || 1;
123
- ctx.strokeRect(startX, y, barWidth, barHeight);
124
- ctx.fillStyle = `${labelStyle?.color || 'black'}`;
125
- ctx.textAlign = 'center';
126
- ctx.font = `bold ${labelStyle?.size || 16}px Arial`;
127
- const labelX = startX + barWidth / 2;
128
- const labelY = y - 5;
129
- ctx.fillText(bar.label, labelX, labelY);
130
- });
131
- const buffer = canvas.toBuffer('image/png');
132
- return buffer;
133
- }
134
- catch (error) {
135
- console.error('An error occurred while drawing the bar chart:', error);
136
- }
137
- }
138
- function drawGridLines(ctx, leftMargin, topMargin, width, height, xLabels, yLabels, grid) {
139
- ctx.strokeStyle = grid.color || 'gray';
140
- ctx.lineWidth = grid.width || 2;
141
- xLabels.forEach((label, index) => {
142
- const x = leftMargin + index * (width / (xLabels.length - 1));
143
- ctx.beginPath();
144
- ctx.moveTo(x, topMargin);
145
- ctx.lineTo(x, topMargin + height);
146
- ctx.stroke();
147
- });
148
- yLabels.forEach((label, index) => {
149
- const y = topMargin + index * (height / (yLabels.length - 1));
150
- ctx.beginPath();
151
- ctx.moveTo(leftMargin, y);
152
- ctx.lineTo(leftMargin + width, y);
153
- ctx.stroke();
154
- });
155
- }
156
- ////////////////////////////////////////PIE CHARTS////////////////////////////////////////
157
- function drawPieChart(ctx, data, canvasConfig, pieDataConfig) {
158
- const width = canvasConfig?.width ?? 1200;
159
- const height = canvasConfig?.height ?? 400;
160
- const centerX = width / 2 + (pieDataConfig?.x || 0);
161
- const centerY = height / 2 + (pieDataConfig?.y || 0);
162
- const radius = pieDataConfig?.radius || Math.min(width, height) * 0.35;
163
- let startAngle = 0;
164
- const totalValue = data.reduce((acc, { value }) => acc + value, 0);
165
- for (const { label, color, value } of data) {
166
- const sliceAngle = (value / totalValue) * (Math.PI * 2);
167
- const midAngle = startAngle + sliceAngle / 2;
168
- const labelDistance = pieDataConfig?.boxes?.labelDistance || 50;
169
- const labelX = centerX + Math.cos(midAngle) * (radius + labelDistance);
170
- const labelY = centerY + Math.sin(midAngle) * (radius + labelDistance);
171
- ctx.strokeStyle = pieDataConfig?.boxes?.strokeColor || 'black';
172
- ctx.beginPath();
173
- ctx.moveTo(centerX, centerY);
174
- ctx.lineTo(labelX, labelY);
175
- ctx.stroke();
176
- const boxWidth = pieDataConfig?.boxes?.width || 100;
177
- const boxHeight = pieDataConfig?.boxes?.height || 40;
178
- const boxX = labelX - boxWidth / 2;
179
- const boxY = labelY - boxHeight / 2;
180
- ctx.fillStyle = pieDataConfig?.boxes?.boxColor || 'black';
181
- ctx.beginPath();
182
- ctx.rect(boxX, boxY, boxWidth, boxHeight);
183
- ctx.fill();
184
- ctx.stroke();
185
- // Draw the label text
186
- ctx.fillStyle = pieDataConfig?.boxes?.labelColor || 'black';
187
- ctx.font = `bold ${pieDataConfig?.boxes?.fontSize || 14}px Arial`;
188
- ctx.textAlign = 'center';
189
- ctx.textBaseline = 'middle';
190
- ctx.fillText(label, labelX, labelY);
191
- startAngle += sliceAngle;
192
- }
193
- startAngle = 0;
194
- for (const { color, value } of data) {
195
- const sliceAngle = (value / totalValue) * (Math.PI * 2);
196
- ctx.fillStyle = color || 'black';
197
- ctx.beginPath();
198
- ctx.moveTo(centerX, centerY);
199
- ctx.arc(centerX, centerY, radius, startAngle, startAngle + sliceAngle);
200
- ctx.closePath();
201
- ctx.fill();
202
- startAngle += sliceAngle;
203
- }
204
- const strokeConfig = pieDataConfig.stroke || { color: 'transparent', size: 0 };
205
- const { color: strokeColor, size: strokeWidth } = strokeConfig;
206
- ctx.strokeStyle = strokeColor || 'transparent';
207
- ctx.lineWidth = strokeWidth || 0;
208
- ctx.beginPath();
209
- ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
210
- ctx.stroke();
211
- if (pieDataConfig.title) {
212
- const { text, color, fontSize, x = 0, y = 0 } = pieDataConfig.title;
213
- ctx.fillStyle = color || 'black';
214
- ctx.font = `${fontSize || 20}px Arial`;
215
- ctx.textAlign = 'center';
216
- ctx.fillText(text || '', centerX + x, centerY + y);
217
- }
218
- }
219
- function drawKeys(ctx, data, pieConfig) {
220
- const { bgcolor, width, height, radius, x, y, content } = pieConfig.keyBox || {};
221
- const keyX = x || 0;
222
- const keyY = y || 0;
223
- ctx.fillStyle = bgcolor || 'white';
224
- ctx.beginPath();
225
- ctx.moveTo(keyX + (radius || 0), keyY);
226
- ctx.lineTo(keyX + (width || 0) - (radius || 0), keyY);
227
- ctx.arcTo(keyX + (width || 0), keyY, keyX + (width || 0), keyY + (radius || 0), radius || 0);
228
- ctx.lineTo(keyX + (width || 0), keyY + (height || 0) - (radius || 0));
229
- ctx.arcTo(keyX + (width || 0), keyY + (height || 0), keyX + (width || 0) - (radius || 0), keyY + (height || 0), radius || 0);
230
- ctx.lineTo(keyX + (radius || 0), keyY + (height || 0));
231
- ctx.arcTo(keyX, keyY + (height || 0), keyX, keyY + (height || 0) - (radius || 0), radius || 0);
232
- ctx.lineTo(keyX, keyY + (radius || 0));
233
- ctx.arcTo(keyX, keyY, keyX + (radius || 0), keyY, radius || 0);
234
- ctx.closePath();
235
- ctx.fill();
236
- ctx.fillStyle = 'black';
237
- ctx.font = `bold ${content?.keyTitle?.fontSize || 14}px Arial`;
238
- const textWidth = ctx.measureText('Keys').width;
239
- const textX = ((content?.keyTitle?.x || 0) + 20 + keyX + ((width || 0) - textWidth) / 2) || 0;
240
- const textY = (keyY + 30 + (content?.keyTitle?.y || 0)) || 0;
241
- ctx.fillText('Keys', textX, textY);
242
- let fontSize = content?.keys?.fontSize || Math.min(16, Math.floor((width || 0) / 20));
243
- ctx.font = `${fontSize}px Arial`;
244
- data.forEach(({ label, color, key }, index) => {
245
- ctx.fillStyle = color || 'black';
246
- ctx.fillRect(keyX + 20, keyY + 60 + index * (fontSize + 10), 20, 20);
247
- ctx.fillStyle = 'black';
248
- ctx.fillText(`${key || ''}: ${label || ''}`, (keyX + 95 + (content?.keys?.x || 0)), (content?.keys?.y || 0) + keyY + 70 + index * (fontSize + 10));
249
- });
250
- }
251
- export async function pieChart(pieChartData) {
252
- const { data = [], pieConfig = {} } = pieChartData;
253
- const { canvas = {}, keyBox = {}, pieData = {} } = pieConfig;
254
- const { width: canvasWidth = 1200, height: canvasHeight = 400, bgcolor: canvasBgcolor = 'gray' } = canvas || {};
255
- const { width: keyBoxWidth = 200, height: keyBoxHeight = 300, radius: keyBoxRadius = 20, bgcolor: keyBoxBgcolor = '#ffffff', x: keyBoxX = 0, y: keyBoxY = 0, content: keyBoxContent } = keyBox || {};
256
- const chartCanvas = createCanvas(canvasWidth, canvasHeight);
257
- const ctx = chartCanvas.getContext('2d');
258
- ctx.fillStyle = canvasBgcolor;
259
- ctx.fillRect(0, 0, canvasWidth, canvasHeight);
260
- const pieConfigData = pieConfig.pieData ?? {};
261
- drawPieChart(ctx, data, canvas, pieConfigData);
262
- drawKeys(ctx, data, { keyBox: { bgcolor: keyBoxBgcolor, width: keyBoxWidth, height: keyBoxHeight, radius: keyBoxRadius, x: keyBoxX, y: keyBoxY, content: keyBoxContent }, canvas });
263
- return chartCanvas.toBuffer('image/png');
264
- }
265
- ////////////////////////////////////////LINE CHARTS////////////////////////////////////////
266
- export async function lineChart(data) {
267
- if (!data || !data.data || !Array.isArray(data.data) || data.data.length === 0 || !data.lineConfig) {
268
- throw new Error('Invalid data object or missing data properties');
269
- }
270
- const lineConfig = data.lineConfig;
271
- if (!lineConfig.yLabels || !Array.isArray(lineConfig.yLabels) || lineConfig.yLabels.length === 0) {
272
- throw new Error('Missing or invalid yLabels property in line configuration');
273
- }
274
- if (!lineConfig.fillArea || !Array.isArray(lineConfig.fillArea) || lineConfig.fillArea.length !== data.data.length) {
275
- throw new Error('Missing or invalid fillArea property in line configuration');
276
- }
277
- if (!lineConfig.lineColor || !Array.isArray(lineConfig.lineColor) || lineConfig.lineColor.length !== data.data.length) {
278
- throw new Error('Missing or invalid lineColor property in line configuration');
279
- }
280
- if (!lineConfig.plot || typeof lineConfig.plot !== 'object') {
281
- throw new Error('Missing or invalid plot property in line configuration');
282
- }
283
- if (!lineConfig.lineTension || !Array.isArray(lineConfig.lineTension) || lineConfig.lineTension.length !== data.data.length) {
284
- throw new Error('Missing or invalid lineTension property in line configuration');
285
- }
286
- if (!lineConfig.grid || typeof lineConfig.grid !== 'object') {
287
- throw new Error('Missing or invalid grid property in line configuration');
288
- }
289
- const canvasWidth = Math.max(data.lineConfig?.canvas?.width || 800, (data.data[0]?.length - 1) * 80 + 100);
290
- const canvasHeight = Math.max(data.lineConfig?.canvas?.height || 600, (lineConfig.yLabels.length - 1) * 60 + 100);
291
- let img;
292
- if (data.lineConfig?.canvas?.image) {
293
- try {
294
- if (data.lineConfig.canvas.image.startsWith('http')) {
295
- const response = await fetch(data.lineConfig.canvas.image);
296
- if (!response.ok) {
297
- throw new Error("Failed to fetch image.");
298
- }
299
- const buffer = await response.arrayBuffer();
300
- img = await loadImage(Buffer.from(buffer));
301
- }
302
- else {
303
- const imagePath = path.join(process.cwd(), data.lineConfig.canvas.image);
304
- img = await loadImage(imagePath);
305
- }
306
- }
307
- catch (error) {
308
- console.error('Error loading image:', error);
309
- }
310
- }
311
- const canvas = createCanvas(canvasWidth, canvasHeight);
312
- const ctx = canvas.getContext('2d');
313
- const xAxisLabels = data.data[0].map(point => point.label);
314
- const yAxisLabel = data.lineConfig?.yaxisLabel?.label || 'Y Axis';
315
- ctx.font = `${data.lineConfig?.yaxisLabel?.fontSize || 16} Arial`;
316
- if (data.lineConfig?.canvas?.image) {
317
- ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
318
- }
319
- else {
320
- ctx.fillStyle = data.lineConfig?.canvas?.bgColor || '#f0f0f0';
321
- ctx.fillRect(0, 0, canvasWidth, canvasHeight);
322
- }
323
- if (data.lineConfig?.grid) {
324
- const grid = data.lineConfig.grid;
325
- ctx.strokeStyle = grid.color || '#ccc';
326
- ctx.lineWidth = grid.width || 1;
327
- if (grid.type === 'vertical' || grid.type === 'both') {
328
- for (let i = 1; i < xAxisLabels.length; i++) {
329
- const x = i * (canvasWidth - 100) / (xAxisLabels.length - 1) + 50;
330
- ctx.beginPath();
331
- ctx.moveTo(x, 50);
332
- ctx.lineTo(x, canvasHeight - 50);
333
- ctx.stroke();
334
- }
335
- }
336
- if (grid.type === 'horizontal' || grid.type === 'both') {
337
- for (let i = 1; i < lineConfig.yLabels.length; i++) {
338
- const y = i * (canvasHeight - 100) / (lineConfig.yLabels.length - 1) + 50;
339
- ctx.beginPath();
340
- ctx.moveTo(50, y);
341
- ctx.lineTo(canvasWidth - 50, y);
342
- ctx.stroke();
343
- }
344
- }
345
- }
346
- ctx.save();
347
- ctx.translate(20, canvasHeight / 2);
348
- ctx.rotate(-Math.PI / 2);
349
- ctx.fillStyle = data.lineConfig?.yaxisLabel?.color || 'black';
350
- ctx.fillText(yAxisLabel, data.lineConfig?.yaxisLabel?.y || 0, data.lineConfig?.yaxisLabel?.x || 0);
351
- ctx.restore();
352
- let maxY = 0;
353
- data.data.forEach(line => {
354
- const maxLineY = Math.max(...line.map(point => point.y));
355
- if (maxLineY > maxY)
356
- maxY = maxLineY;
357
- });
358
- const segmentWidth = (canvasWidth - 100) / (xAxisLabels.length - 1);
359
- ctx.beginPath();
360
- ctx.moveTo(50, 50);
361
- ctx.lineTo(50, canvasHeight - 50);
362
- ctx.lineTo(canvasWidth - 50, canvasHeight - 50);
363
- ctx.stroke();
364
- data.data.forEach((line, index) => {
365
- ctx.beginPath();
366
- ctx.strokeStyle = lineConfig?.lineColor[index] || 'blue';
367
- ctx.lineWidth = 2;
368
- const tension = (lineConfig && lineConfig.lineTension && lineConfig.lineTension[index]) || 0.1;
369
- line.forEach((point, index) => {
370
- const x = index * segmentWidth + 50;
371
- const y = canvasHeight - (point.y / maxY) * (canvasHeight - 100) - 50;
372
- if (index === 0) {
373
- ctx.moveTo(x, y);
374
- }
375
- else {
376
- const prevX = (index - 1) * segmentWidth + 50;
377
- const prevY = canvasHeight - (line[index - 1].y / maxY) * (canvasHeight - 100) - 50;
378
- const cpX1 = prevX + (x - prevX) * tension;
379
- const cpY1 = prevY;
380
- const cpX2 = x - (x - prevX) * tension;
381
- const cpY2 = y;
382
- ctx.bezierCurveTo(cpX1, cpY1, cpX2, cpY2, x, y);
383
- }
384
- });
385
- ctx.stroke();
386
- if (lineConfig.fillArea[index]) {
387
- const lastPoint = line[line.length - 1];
388
- const lastX = (line.length - 1) * segmentWidth + 50;
389
- const lastY = canvasHeight - (lastPoint.y / maxY) * (canvasHeight - 100) - 50;
390
- ctx.lineTo(lastX, canvasHeight - 50);
391
- ctx.lineTo(50, canvasHeight - 50);
392
- ctx.closePath();
393
- ctx.fillStyle = lineConfig.fillArea[index].color || 'rgba(0, 0, 255, 0.1)';
394
- ctx.fill();
395
- }
396
- if (data.lineConfig?.plot && data.lineConfig?.plot?.enable) {
397
- const plotConfig = data.lineConfig?.plot;
398
- ctx.fillStyle = plotConfig.color ? plotConfig.color[index] || 'red' : 'red';
399
- line.forEach((point, index) => {
400
- const x = index * segmentWidth + 50;
401
- const y = canvasHeight - (point.y / maxY) * (canvasHeight - 100) - 50;
402
- ctx.beginPath();
403
- ctx.arc(x, y, plotConfig.size || 4, 0, 2 * Math.PI);
404
- ctx.fill();
405
- });
406
- }
407
- });
408
- ctx.fillStyle = data.lineConfig?.canvas?.fontColor || 'black';
409
- ctx.font = `${data.lineConfig?.canvas?.fontSize || 16}px Arial`;
410
- ctx.textAlign = 'center';
411
- ctx.textBaseline = 'top';
412
- xAxisLabels.forEach((label, index) => {
413
- const x = index * segmentWidth + 50;
414
- const y = canvasHeight - 30;
415
- ctx.fillText(label, x, y);
416
- });
417
- ctx.textAlign = 'right';
418
- ctx.textBaseline = 'middle';
419
- lineConfig.yLabels.forEach((label, index) => {
420
- const y = canvasHeight - index * (canvasHeight - 100) / (lineConfig.yLabels.length - 1) - 50;
421
- ctx.fillText(label, 40, y);
422
- });
423
- if (lineConfig.keys && typeof lineConfig.keys === 'object') {
424
- const keyRadius = lineConfig.keysConfig?.radius || 10;
425
- const keyPadding = lineConfig.keysConfig?.keyPadding || 30;
426
- const textPadding = lineConfig.keysConfig?.textPadding || 60;
427
- const lineLength = 2 * keyRadius + 9 + (lineConfig.keysConfig?.textPadding || 0);
428
- const lineWidth = lineConfig.keysConfig?.lineWidth || 3;
429
- let totalKeyWidth = 0;
430
- for (const color in lineConfig.keys) {
431
- const keyText = lineConfig.keys[color];
432
- totalKeyWidth += ctx.measureText(keyText).width + keyPadding + (2 * keyRadius) + textPadding;
433
- }
434
- let startX = (canvasWidth - totalKeyWidth) / 2;
435
- for (const color in lineConfig.keys) {
436
- const keyText = lineConfig.keys[color];
437
- ctx.beginPath();
438
- ctx.arc(startX + keyRadius, 20 + keyRadius, keyRadius, 0, 2 * Math.PI);
439
- ctx.fillStyle = color;
440
- ctx.fill();
441
- ctx.closePath();
442
- ctx.beginPath();
443
- ctx.moveTo(startX + keyRadius - lineLength / 2, 20 + keyRadius);
444
- ctx.lineTo(startX + keyRadius + lineLength / 2, 20 + keyRadius);
445
- ctx.strokeStyle = color;
446
- ctx.lineWidth = lineWidth;
447
- ctx.stroke();
448
- ctx.closePath();
449
- ctx.fillStyle = lineConfig.keysConfig?.fontColor || 'black';
450
- ctx.fillText(keyText, startX + (2 * keyRadius) + keyPadding + textPadding, 20 + keyRadius);
451
- startX += ctx.measureText(keyText).width + keyPadding + (2 * keyRadius) + textPadding + keyPadding;
452
- }
453
- }
454
- return canvas.toBuffer('image/png');
455
- }
@@ -1,2 +0,0 @@
1
- import { CustomOptions } from "../types.js";
2
- export declare function customLines(ctx: any, options: CustomOptions[]): void;