-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample5.html
83 lines (74 loc) · 3.11 KB
/
sample5.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Graphs ChartJS - Sample 5</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css" integrity="sha256-aa0xaJgmK/X74WM224KMQeNQC2xYKwlAt08oZqjeF0E=" crossorigin="anonymous" />
</head>
<body>
<fieldset>
<legend>Add value to chart</legend>
<label for="ano">Ano</label>
<input type="text" name="ano" id="ano">
<label for="homens">Homens</label>
<input type="text" name="homens" id="homens">
<label for="mulheres">Mulheres</label>
<input type="text" name="mulheres" id="mulheres">
<button type="button" id="btn-draw-chart">Add</button>
</fieldset>
<div style="height: 30vh; width: 30vw;">
<canvas id="myChart"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" integrity="sha256-Uv9BNBucvCPipKQ2NS9wYpJmi8DTOEfTA/nH2aoJALw=" crossorigin="anonymous"></script>
<script>
var ctx = document.getElementById('myChart');
var myChart = null;
$.getJSON('server/ibge_homem_mulheres.json', (r) => data = r)
.fail(() => {
alert('ajax request fail')
})
.then(() => {
myChart = new Chart(ctx, {
type: 'bar', /* bar line horizontalBar */
data: {
labels: data.anos,
datasets: [
{
label: 'Homens',
data: data.homens,
backgroundColor: 'blue'
},{
label: 'Mulheres',
data: data.mulheres,
backgroundColor: 'pink'
}
]
},
options: {
maintainAspectRatio: false,
onClick: (e, data) => {
if (data.length == 2) {
myChart.data.labels.splice(data[0]._index, 1);
myChart.data.datasets[0].data.splice(data[0]._index, 1);
myChart.data.datasets[1].data.push(data[0]._index, 1);
setTimeout(() => myChart.update(), 400);
}
}
}
});
});
$('#btn-draw-chart').click(() => {
const ano = $('#ano').val();
const homens = $('#homens').val();
const mulheres = $('#mulheres').val();
myChart.data.labels.push(ano);
myChart.data.datasets[0].data.push(homens);
myChart.data.datasets[1].data.push(mulheres);
myChart.update();
});
</script>
</body>
</html>