-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenGraphic.js
More file actions
60 lines (55 loc) · 1.27 KB
/
GenGraphic.js
File metadata and controls
60 lines (55 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { createCanvas } from "canvas";
import { createWriteStream, existsSync, mkdirSync } from "fs";
import Chart from "chart.js/auto";
import { Colors } from "chart.js";
export default (data, labels, graphName) => {
Chart.register(Colors);
const canvas = createCanvas(800, 600);
const ctx = canvas.getContext("2d");
new Chart(ctx, {
type: "line",
data: {
labels: labels,
datasets: data,
},
options: {
responsive: true,
scales: {
x: {},
y: {
suggestedMin: 0,
suggestedMax: 100,
max: 1000,
ticks: {
callback: function (value) {
return value + " ms";
},
},
},
},
layout: {
padding: 20,
},
plugins: {
legend: {
position: "top",
},
title: {
display: true,
text: graphName[1],
},
},
},
});
const resultDir = "./result";
if (!existsSync(resultDir)) {
mkdirSync(resultDir);
}
const outputPath = `${resultDir}/${graphName[0]}.png`;
const out = createWriteStream(outputPath);
const stream = canvas.createPNGStream();
stream.pipe(out);
out.on("finish", () => {
console.log(`Gráfico salvo em: ${outputPath}`);
});
};