-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-chart.js
More file actions
111 lines (101 loc) · 2.92 KB
/
generate-chart.js
File metadata and controls
111 lines (101 loc) · 2.92 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const { ChartJSNodeCanvas } = require('chartjs-node-canvas');
const Chart = require('chart.js');
const ChartDataLabels = require('chartjs-plugin-datalabels');
// Manually register the datalabels plugin if not automatically recognized
Chart.register(ChartDataLabels);
const fs = require('fs-extra');
const paths = require('./paths.js');
const util = require('./util-date');
const graphsBoletaFolderPath = paths.getGraphsBoletaFolderPath();
const width = 4000; //px
const height = 1350; //px
const backgroundColour = 'white';
const chartJSNodeCanvas = new ChartJSNodeCanvas({ width, height, backgroundColour, Chart: Chart });
async function generateChart(data) {
const chartDateArray = [];
if (data[`${util.getChartMonths()[0]}`]) {
chartDateArray.push(...util.getChartMonths().reverse());
} else {
chartDateArray.push(...util.getChartPreviousMonths().reverse());
}
const monthlyConsumption = chartDateArray.map(date => data[date]);
const monthNames = chartDateArray.map(dates => dates.slice(0, 3));
const maxDataValue = Math.max(...monthlyConsumption);
const buffer = Math.max(1, Math.ceil(maxDataValue * 0.1));
const configuration = {
type: 'bar',
data: {
labels: monthNames,
datasets: [
{
data: monthlyConsumption,
backgroundColor: [
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#2095b92a',
'#20c0983a',
],
borderColor: '#353535', // Specify border color
borderWidth: 6, // Specify border width
},
],
},
options: {
scales: {
x: {
grid: {
display: false,
},
ticks: {
font: {
size: 100, // Set the font size for x-axis labels
},
},
},
y: {
beginAtZero: true,
suggestedMax: maxDataValue + buffer,
grid: {
display: false,
},
ticks: {
precision: 0,
callback: function (value, index, values) {
if (Math.floor(value) === value) {
return value;
}
},
font: {
size: 100, // Set the font size for x-axis labels
},
},
},
},
plugins: {
legend: {
display: false,
},
datalabels: {
color: '#343436',
anchor: 'end',
align: 'top',
font: {
size: 90,
},
},
},
},
};
const image = await chartJSNodeCanvas.renderToBuffer(configuration);
await fs.writeFile(graphsBoletaFolderPath + `/graph_${data['Numero Cliente']}.png`, image);
}
module.exports = { generateChart };