-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartFunctions.js
208 lines (190 loc) · 4.94 KB
/
chartFunctions.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Default colors for the pie chart
const defaultColors = [
"#a66d93", "#82a798", "#7db3b1", "#966f75", "#9b65b1",
"#677d74", "#a072a7", "#736f6a", "#718e8f", "#849c71",
"#6cb18b", "#8e838e", "#6697a9", "#6f75ac", "#a599a5",
"#749899", "#90af8f", "#75a896", "#9d9b80", "#797797",
"#9766ab", "#b3a590", "#a17689", "#749f70", "#a4a28b",
"#808aae", "#75779a", "#8c999b", "#8b6b8d", "#85a866",
"#6b96aa", "#73a078", "#8a8496", "#78a267", "#a17a90",
"#9fa0ac", "#846993", "#7b8fa3", "#8a9bb2", "#83a987",
];
function getPieChartConfig(categories, amounts, colors = defaultColors) {
return {
type: "pie",
data: {
labels: categories,
datasets: [
{
data: amounts,
backgroundColor: colors,
},
],
},
options: {
responsive: true,
legend: { position: "top" },
title: { display: false },
animation: { animateScale: true, animateRotate: true },
},
};
}
function createPieChart(categories, amounts, htmlId) {
const canvasContext = document.getElementById(htmlId).getContext("2d");
const chartConfig = getPieChartConfig(categories, amounts);
new Chart(canvasContext, chartConfig);
}
function createCohortChart(cohorts) {
const ctx = document.getElementById("cohortChart").getContext("2d");
const chartData = {
labels: Object.keys(cohorts),
datasets: [
{
label: "Gasto Total por Cohorte",
data: Object.values(cohorts).map((cohort) => cohort.total),
backgroundColor: "rgba(54, 162, 235, 0.2)",
borderColor: "rgba(54, 162, 235, 1)",
borderWidth: 1,
},
],
};
new Chart(ctx, {
type: "bar",
data: chartData,
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
});
}
function createComparisonChart(yearlyData) {
const ctx = document.getElementById("comparisonChart").getContext("2d");
const chartData = {
labels: [
"Enero",
"Febrero",
"Marzo",
"Abril",
"Mayo",
"Junio",
"Julio",
"Agosto",
"Septiembre",
"Octubre",
"Noviembre",
"Diciembre",
],
datasets: Object.keys(yearlyData).map((year) => ({
label: `Año ${year}`,
data: yearlyData[year],
fill: false,
borderColor: randomColor(),
tension: 0.1,
})),
};
new Chart(ctx, {
type: "line",
data: chartData,
options: {
responsive: true,
title: {
display: true,
text: "Comparación de Gastos Año a Año",
},
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
},
});
}
function randomColor() {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function getBarChartConfig(
categories,
expenses,
colors = defaultColors,
borderColor = "rgba(0, 123, 255, 1)"
) {
return {
type: "bar",
data: {
labels: categories,
datasets: [
{
label: "Gasto Total por Categoría",
data: expenses,
backgroundColor: colors,
borderColor: borderColor,
borderWidth: 1,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
};
}
function createBarChart(categories, expenses, canvasId) {
const canvasContext = document.getElementById(canvasId).getContext("2d");
const chartConfig = getBarChartConfig(categories, expenses);
new Chart(canvasContext, chartConfig);
}
function getLineChartConfig(labels, datasets) {
return {
type: 'line',
data: {
labels: labels,
datasets: datasets,
},
options: {
responsive: true,
legend: { position: 'top' },
title: { display: false },
animation: { animateScale: true, animateRotate: true },
},
};
}
let currentChart = null;
function createLineChart(data, htmlId) {
const datasets = [];
const years = new Set(Object.values(data).map(item => item.year));
years.forEach((year, index) => {
const yearData = Object.values(data).filter(item => item.year === year);
const amounts = new Array(12).fill(0); // Preparar array para 12 meses
yearData.forEach(item => {
amounts[item.month - 1] = item.amount; // -1 porque los meses en JavaScript comienzan en 0
});
datasets.push({
label: `Año ${year}`,
data: amounts,
borderColor: defaultColors[index % defaultColors.length],
fill: false,
});
});
if (currentChart) {
currentChart.destroy();
}
// Configurar y crear el gráfico
const chartConfig = getLineChartConfig(["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"], datasets);
const canvasContext = document.getElementById(htmlId).getContext("2d");
currentChart = new Chart(canvasContext, chartConfig);
}