-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchart5.js
109 lines (95 loc) · 3.63 KB
/
chart5.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
import salesCategoryByLocation from './salesCategoryByLocation.json' assert { type: 'json' };
console.log(salesCategoryByLocation);
const ctx5 = document.getElementById('chart5').getContext('2d');
const categoryFilter = document.getElementById('category-filter');
const monthFilter = document.getElementById('month-filter');
const colors = {
'Carbonated': {
backgroundColor: 'rgba(255, 0, 0, 0.5)',
borderColor: 'rgba(128, 0, 0, 1)'
},
'Food': {
backgroundColor: 'rgba(200, 0, 0, 0.5)',
borderColor: 'rgba(255, 0, 0, 1)'
},
'Non Carbonated': {
backgroundColor: 'rgba(255, 40, 0, 0.5)',
borderColor: 'rgba(255, 40, 0, 1)'
},
'Water': {
backgroundColor: 'rgba(248, 131, 121, 0.5)',
borderColor: 'rgba(248, 131, 121, 1)'
}
};
function filterData(category, month) {
let filteredData = month === 'all' ? salesCategoryByLocation.default : salesCategoryByLocation.periode.filter(entry => entry.Periode.toLowerCase() === month.toLowerCase());
if (category !== 'all') {
filteredData = filteredData.filter(entry => (entry.category || entry.Category).toLowerCase() === category.toLowerCase());
}
return filteredData;
}
function updateChart(category, month) {
const filteredData = filterData(category, month);
const labels = ['GuttenPlans', 'EB_Public_Library', 'Brunswick_Sq_Mall', 'Earle_Asphalt'];
const datasets = ['Carbonated', 'Food', 'Non Carbonated', 'Water'].map(cat => {
const data = filteredData.find(entry => (entry.category || entry.Category).toLowerCase() === cat.toLowerCase()) || {};
return {
label: cat,
data: labels.map(label => parseFloat(data[label] || 0)),
backgroundColor: colors[cat].backgroundColor,
borderColor: colors[cat].borderColor,
borderWidth: 1
};
});
chart.data.labels = labels;
chart.data.datasets = datasets;
chart.update();
}
// Initialize chart with all data
const initialData = filterData('all', 'all');
const initialLabels = ['GuttenPlans', 'EB_Public_Library', 'Brunswick_Sq_Mall', 'Earle_Asphalt'];
const initialDatasets = ['Carbonated', 'Food', 'Non Carbonated', 'Water'].map(cat => {
const data = initialData.find(entry => (entry.category || entry.Category).toLowerCase() === cat.toLowerCase()) || {};
return {
label: cat,
data: initialLabels.map(label => parseFloat(data[label] || 0)),
backgroundColor: colors[cat].backgroundColor,
borderColor: colors[cat].borderColor,
borderWidth: 1
};
});
// Create chart
const chart = new Chart(ctx5, {
type: 'bar',
data: {
labels: initialLabels,
datasets: initialDatasets
},
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);
});
document.addEventListener("DOMContentLoaded", function() {
updateChart('all', 'all');
});