-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
130 lines (127 loc) · 4.36 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
crossorigin="anonymous"
/>
<title>Transactions</title>
</head>
<body>
<div class="container mt-5">
<form id="transactionsForm" class="row g-5">
<div class="col-auto">
<label class="form-table" for="startDate">Start Date</label>
<input
class="form-control"
type="date"
name="startDate"
id="startDate"
/>
</div>
<div class="col-auto">
<label class="form-table" for="endDate">End Date</label>
<input class="form-control" type="date" name="endDate" id="endDate" />
</div>
<div class="col-auto">
<label class="form-table" for="period">Period</label>
<select class="form-select" name="period" id="period">
<option value="year">year</option>
<option value="month">month</option>
<option value="week">week</option>
<option value="day">day</option>
</select>
</div>
<div class="col-auto">
<label class="form-table" for="mode">Mode</label>
<select class="form-select" name="mode" id="mode">
<option value="count">count</option>
<option value="amount">amount</option>
</select>
</div>
<div class="col-auto">
<label class="form-table" for="currency">Currency</label>
<select class="form-select" name="currency" id="currency">
<option value="rial">rial</option>
<option value="toman">toman</option>
</select>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mt-4">Fetch!</button>
</div>
</form>
</div>
<div class="container">
<canvas class="mt-5" id="chart"> </canvas>
</div>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js"
integrity="sha512-ElRFoEQdI5Ht6kZvyzXhYG9NqjtkmlkfYk0wr6wHxU9JEHakS7UJZNeml5ALk+8IKlU6jDgMabC3vkumRokgJA=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
></script>
<script>
let myChart;
const myForm = document.getElementById("transactionsForm");
myForm.addEventListener("submit", formSubmitFetch);
function formSubmitFetch(e) {
e.preventDefault();
function getSelectedOption(id) {
let e = document.getElementById(id);
let text = e.options[e.selectedIndex].text;
return text;
}
const formData = {};
formData["startDate"] = document.querySelector(
'input[name="startDate"]'
).value;
formData["endDate"] = document.querySelector(
'input[name="endDate"]'
).value;
formData["period"] = getSelectedOption("period");
formData["currency"] = getSelectedOption("currency");
formData["mode"] = getSelectedOption("mode");
fetch("http://127.0.0.1:5000/transactions", {
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
})
.then((response) => response.json())
.then((data) => {
if (myChart) {
myChart.destroy();
}
createChart(data);
})
.catch((error) => console.log(error));
}
function createChart(response) {
const labels = response.map((data) => data["key"]);
const chartData = {
labels: labels,
datasets: [
{
label: "Value",
backgroundColor: "rgb(255, 99, 132)",
borderColor: "rgb(255, 99, 132)",
data: response.map((data) => data["value"]),
},
],
};
const config = {
type: "line",
data: chartData,
options: {},
};
myChart = new Chart(document.getElementById("chart"), config);
}
</script>
</body>
</html>