-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_scatterplot.html
154 lines (135 loc) · 4.39 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!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();
// 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", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.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");
d3.select("#number").text(dataset.length);
d3.select("#clique")
.on("click", function(){
// Vai fazer alguma coisa ao ser clicado
dataset = generateRandomDataset();
xScale.domain([0, d3.max(dataset, function(d) {
return d[0]; })
]);
yScale.domain([0, d3.max(dataset, function(d) {
return d[1]; })
]);
svg.select(".x.axis")
.call(xAxis);
svg.select(".y.axis")
.call(yAxis);
d3.select("#number").text(dataset.length);
// Removendo elementos sem dados
svg.selectAll("circle")
.data(dataset)
.exit()
.remove();
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", 4);
svg.selectAll("circle")
.data(dataset)
.transition()
.duration(1000)
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
});
})
function generateRandomDataset() {
let dataset = [];
let numDataPoints = 50// 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;
}
</script>
</body>
</html>