-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoinGraphModule.js
More file actions
114 lines (98 loc) · 2.47 KB
/
Copy pathcoinGraphModule.js
File metadata and controls
114 lines (98 loc) · 2.47 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
112
113
114
const { ChartJSNodeCanvas } = require('chartjs-node-canvas');
const db = require('./database');
// tamanho do gráfico
const width = 900;
const height = 400;
const chartCanvas = new ChartJSNodeCanvas({
width,
height,
backgroundColour: "#2b2d31"
});
// formata satoshis → coin (sem quebrar)
function formatCoins(sats) {
if (!sats) return "0.00000000";
const coins = sats / 100_000_000;
// remove zeros desnecessários MAS mantém precisão
return coins.toFixed(8).replace(/\.?0+$/, '');
}
async function generateUserGraph(userId) {
try {
const { labels, values } = db.getUserGraphData(userId);
if (!labels.length) {
throw new Error("No graph data");
}
const config = {
type: "line",
data: {
labels: labels,
datasets: [
{
label: "Balance (Last 30 Days)",
data: values,
fill: true,
borderWidth: 2,
tension: 0.4,
pointRadius: 3,
borderColor: "#5865F2",
backgroundColor: "rgba(88, 101, 242, 0.2)"
}
]
},
options: {
responsive: false,
plugins: {
legend: {
labels: {
color: "#ffffff"
}
},
tooltip: {
callbacks: {
label: function(context) {
return `💰 ${formatCoins(context.raw)} coins`;
}
}
}
},
scales: {
x: {
ticks: {
color: "#b5bac1"
},
grid: {
color: "rgba(255,255,255,0.05)"
}
},
y: {
ticks: {
color: "#b5bac1",
callback: function(value) {
return formatCoins(value);
}
},
grid: {
color: "rgba(255,255,255,0.05)"
}
}
}
}
};
return await chartCanvas.renderToBuffer(config);
} catch (err) {
console.error('❌ [graph] generateUserGraph error:', err);
const fallbackConfig = {
type: "line",
data: {
labels: ["No Data"],
datasets: [{
label: "No Data Available",
data: [0]
}]
}
};
return await chartCanvas.renderToBuffer(fallbackConfig);
}
}
module.exports = {
generateUserGraph
};