-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchart3.js
90 lines (79 loc) · 3.2 KB
/
chart3.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
import typeByLocation from './typeByLocation.json' assert { type: 'json' };
console.log(typeByLocation);
const ctx3 = document.getElementById('chart3').getContext('2d');
const categoryFilter = document.getElementById('category-filter');
const monthFilter = document.getElementById('month-filter');
function filterData(category, month) {
let filteredData = [];
if (month === 'all' && category === 'all') {
filteredData = typeByLocation.default;
} else if (month === 'all') {
filteredData = typeByLocation.category.flat().filter(entry => entry.category.toLowerCase() === category.toLowerCase());
} else if (category === 'all') {
filteredData = typeByLocation.periode.flat().filter(entry => entry.periode.toLowerCase() === month.toLowerCase());
} else {
filteredData = typeByLocation.periode_category.filter(entry =>
entry.periode.toLowerCase() === month.toLowerCase() &&
entry.category.toLowerCase() === category.toLowerCase()
);
}
return filteredData;
}
function updateChart(category, month) {
const filteredData = filterData(category, month);
const locations = filteredData.map(entry => entry.location || entry.Location);
const totalCash = filteredData.map(entry => parseFloat(entry.total_cash) || 0);
const totalCredit = filteredData.map(entry => parseFloat(entry.total_credit) || 0);
chart.data.labels = locations;
chart.data.datasets[0].data = totalCash;
chart.data.datasets[1].data = totalCredit;
chart.update();
}
// Create initial chart
const initialData = typeByLocation.default;
const initialLocations = initialData.map(entry => entry.location);
const initialTotalCash = initialData.map(entry => parseFloat(entry.total_cash) || 0);
const initialTotalCredit = initialData.map(entry => parseFloat(entry.total_credit) || 0);
const chart = new Chart(ctx3, {
type: 'bar',
data: {
labels: initialLocations,
datasets: [{
label: 'Transaction Total (Cash)',
data: initialTotalCash,
backgroundColor: 'rgba(255, 0, 0, 0.5)',
borderColor: 'rgba(255, 0, 0, 1)',
borderWidth: 1
}, {
label: 'Transaction Total (Credit)',
data: initialTotalCredit,
backgroundColor: 'rgba(248, 131, 121, 0.5)',
borderColor: 'rgba(248, 131, 121, 1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true,
ticks: {
stepSize: 1000,
callback: function (value) {
return value;
}
}
}
}
}
});
// Add event listeners to dropdowns
categoryFilter.addEventListener('change', () => {
const selectedCategory = categoryFilter.value;
const selectedMonth = monthFilter.value;
updateChart(selectedCategory, selectedMonth);
});
monthFilter.addEventListener('change', () => {
const selectedCategory = categoryFilter.value;
const selectedMonth = monthFilter.value;
updateChart(selectedCategory, selectedMonth);
});