-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_scatterplot.html
137 lines (117 loc) · 3.84 KB
/
01_scatterplot.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Scatterplot</title>
<link rel="stylesheet" href="css/styles.css">
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<p>O gráfico possui <span id="number">??</span> pontos.</p>
<div id="chart"></div>
<button id="clique">Novos Dados</button>
<script type="text/javascript">
// Primeiro definimos o objeto margin
let margin = {top: 40, right: 40, bottom: 40, left: 40};
// Depois definimos w e h como as dimensões internas
// da área do gráfico (área útil)
let w = 800 - margin.left - margin.right;
let h = 250 - margin.top - margin.bottom;
// Depois um elemento g no svg que vai transladar a origem
// do gráfico como sendo a origem da área útil do gráfico
let svg = d3.select('#chart')
.append('svg')
.attr('width', w + margin.left + margin.right)
.attr('height', h + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
let dataset = generateRandomDataset();
d3.select('#number').text(dataset.length);
// Criando as escalas
let xScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) {
return d[0];
})])
.range([0,w]);
let yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) {
return d[1];
})])
.range([h,0]);
let xAxis = d3.axisBottom()
.scale(xScale)
.ticks(5);
//.tickValues([0, 150, 300, 450, 600]);
let yAxis = d3.axisLeft()
.scale(yScale)
.ticks(5);
// Depois adicione os elementos círculos
svg.selectAll('circle')
.data(dataset)
.enter()
.append('circle')
.attr('cx', ([x,y]) => xScale(x))
.attr('cy', ([x,y]) => yScale(y))
.attr('r', 4);
// Depois adicione os labels
svg.append('g')
.attr('transform', 'translate(0,' + h + ')')
.attr('class', 'x axis')
.call(xAxis);
svg.append('text')
.attr('transform', 'translate(' + (w/2) + ',' + (h + margin.bottom) + ')')
.attr('class', 'label')
.text('Eixo X');
svg.append('g')
.attr('class', 'y axis')
.call(yAxis);
svg.append('text')
.attr('class', 'label')
.attr('transform', 'rotate(-90)')
.attr('y', 0 - margin.left)
.attr('x',0 - (h / 2))
.attr('dy', '10')
.text('Eixo Y');
function generateRandomDataset() {
let dataset = [];
let numDataPoints = Math.ceil(Math.random() * 100);
let xRange = Math.random() * 1000;
let yRange = Math.random() * 1000;
for (let i=0; i < numDataPoints; i++) {
let newNumber1 = Math.floor(Math.random() * xRange);
let newNumber2 = Math.floor(Math.random() * yRange);
dataset.push([newNumber1, newNumber2]);
}
return dataset;
}
d3.select('#clique').on('click', () => {
dataset = generateRandomDataset();
d3.select('#number').text(dataset.length);
xScale.domain([0, d3.max(dataset, ([x,y]) => x)]);
yScale.domain([0, d3.max(dataset, ([x,y]) => y)]);
svg.select('.x.axis').transition().duration(1000).call(xAxis);
svg.select('.y.axis').transition().duration(1000).call(yAxis);
let circle = svg.selectAll('circle').data(dataset);
circle.exit()
.transition()
.delay((d,i) => i/dataset.length * 1000)
.duration(1000)
.attr('cx', ([x,y]) => 0)
.attr('cy', ([x,y]) => 0)
.remove();
circle.enter()
.append('circle')
.transition()
.duration(1000)
.attr('cx', ([x,y]) => xScale(x))
.attr('cy', ([x,y]) => yScale(y))
.attr('r', 4);
circle.transition()
.delay((d,i) => i/dataset.length * 1000)
.duration(1000)
.attr('cx', ([x,y]) => xScale(x))
.attr('cy', ([x,y]) => yScale(y));
})
</script>
</body>
</html>