-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (146 loc) · 5.62 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
select,
input {
background-color: #ffe0e6;
border: 2px solid #fbabbb;
border-radius: 5px;
padding: 5px;
margin: 0 2px 0 0;
}
button {
background-color: #ffe0e6;
border: 2px solid #fbabbb;
border-radius: 5px;
padding: 7px 16px;
margin: 0 2px 0 0;
color: #7b7a7a;
cursor: pointer;
}
</style>
</head>
<body style="display: flex; align-items: center; justify-content: center;">
<div>
<div id="chart-container" style="width:80vw;">
<canvas id="myChart"></canvas>
</div>
<div style="gap: 10px; width: 80vw; display: flex; align-items: center; justify-content: center;">
<select onchange="chartType = this.value;newChart(this)">
<option value="bar">Biểu đồ cột</option>
<option value="pie">Biểu đồ hình quạt</option>
<option value="line">Biểu đồ đoạn thẳng</option>
</select>
<input type="text" onchange="chartName = this.value; newChart()" placeholder="Tên biểu đồ">
<button type="button" onclick="saveCanvas()">Lưu biểu đồ</button>
<input id="solieu" type="number" placeholder="Số liệu">
<input id="tensolieu" type="text" placeholder="Tên số liệu">
<button type="button" onclick="addData(myChart)">Thêm số liệu</button>
<button type="button" onclick="removeData(myChart)">Xóa tất cả số liệu</button>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const ctx = document.getElementById('myChart');
chartName = '', chartType = 'bar'
labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']
data = [12, 19, 3, 5, 2, 3]
myChart = new Chart(ctx, {
type: chartType,
data: {
labels: labels,
datasets: [{
label: chartName,
data: data,
borderWidth: 1,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
]
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
function newChart(object) {
if (chartType == 'pie') { document.getElementById("chart-container").style.width = '40vw' }
else { document.getElementById("chart-container").style.width = '80vw' }
myChart.destroy()
myChart = new Chart(ctx, {
type: chartType,
data: {
labels: labels,
datasets: [{
label: chartName,
data: data,
borderWidth: 1,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
]
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
function addData(chart) {
labels.push(document.getElementById("tensolieu").value);
data.push(document.getElementById("solieu").value);
newChart()
}
function removeData(chart) {
labels = []
data = []
newChart()
}
function saveCanvas() {
var image = ctx.toDataURL();
var tmpLink = document.createElement('a');
tmpLink.download = chartName + '.png';
tmpLink.href = image;
document.body.appendChild(tmpLink);
tmpLink.click();
document.body.removeChild(tmpLink);
}
</script>
</body>
</html>