-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.html
More file actions
201 lines (187 loc) · 6.23 KB
/
index.html
File metadata and controls
201 lines (187 loc) · 6.23 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sales Dashboard</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
min-height: 100vh;
}
header {
background-color: #333;
color: white;
padding: 1rem;
}
main {
display: flex;
flex-wrap: wrap;
padding: 1rem;
flex-grow: 1;
}
section {
flex: 1;
min-width: 300px;
margin: 0.5rem;
padding: 1rem;
border: 1px solid #ddd;
border-radius: 4px;
}
h1, h2 {
margin-top: 0;
}
#quarterlyChart, #breakdownChart {
width: 100%;
height: 300px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
button {
margin-top: 1rem;
padding: 0.5rem 1rem;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<header>
<h1>Sales Dashboard</h1>
</header>
<main>
<section id="dashboard">
<h2>Quarterly Revenue</h2>
<canvas id="quarterlyChart"></canvas>
<h2>Quarterly Breakdown</h2>
<canvas id="breakdownChart"></canvas>
</section>
<section id="salesReport">
<h2>Sales Report</h2>
<div id="reportContent"></div>
<button id="generateReport">Generate Report</button>
<button id="downloadReport">Download Report</button>
</section>
</main>
<script>
const API_BASE_URL = 'http://localhost:5008/api';
let quarterlyChart, breakdownChart;
async function fetchQuarterlyRevenue() {
const response = await fetch(`${API_BASE_URL}/quarterly_revenue`);
return await response.json();
}
async function fetchQuarterlyBreakdown(quarter) {
const response = await fetch(`${API_BASE_URL}/quarterly_breakdown/${quarter}`);
return await response.json();
}
async function fetchSalesReport() {
const response = await fetch(`${API_BASE_URL}/sales_report`);
return await response.text();
}
async function updateSalesReport(data) {
const response = await fetch(`${API_BASE_URL}/sales_report`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
return await response.json();
}
function initQuarterlyChart(data) {
const ctx = document.getElementById('quarterlyChart').getContext('2d');
quarterlyChart = new Chart(ctx, {
type: 'bar',
data: {
labels: data.map(d => d.quarter),
datasets: [{
label: 'Quarterly Revenue',
data: data.map(d => d.revenue),
backgroundColor: 'rgba(75, 192, 192, 0.6)',
}]
},
options: {
responsive: true,
onClick: (event, elements) => {
if (elements.length > 0) {
const quarter = data[elements[0].index].quarter;
updateBreakdownChart(quarter);
}
}
}
});
}
function initBreakdownChart() {
const ctx = document.getElementById('breakdownChart').getContext('2d');
breakdownChart = new Chart(ctx, {
type: 'pie',
data: {
labels: [],
datasets: [{
data: [],
backgroundColor: [
'rgba(255, 99, 132, 0.6)',
'rgba(54, 162, 235, 0.6)',
'rgba(255, 206, 86, 0.6)',
'rgba(75, 192, 192, 0.6)',
],
}]
},
options: {
responsive: true,
}
});
}
async function updateBreakdownChart(quarter) {
const data = await fetchQuarterlyBreakdown(quarter);
breakdownChart.data.labels = data.map(d => d.product);
breakdownChart.data.datasets[0].data = data.map(d => d.revenue);
breakdownChart.update();
}
async function loadSalesReport() {
const reportHtml = await fetchSalesReport();
document.getElementById('reportContent').innerHTML = reportHtml;
}
document.getElementById('generateReport').addEventListener('click', loadSalesReport);
document.getElementById('downloadReport').addEventListener('click', async () => {
const reportHtml = await fetchSalesReport();
const blob = new Blob([reportHtml], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'sales_report.html';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
async function init() {
const quarterlyData = await fetchQuarterlyRevenue();
initQuarterlyChart(quarterlyData);
initBreakdownChart();
loadSalesReport();
}
init();
</script>
</body>
</html>