-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_margin_scatterplot.html
More file actions
121 lines (113 loc) · 3.81 KB
/
02_margin_scatterplot.html
File metadata and controls
121 lines (113 loc) · 3.81 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Scatterplot</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<div id="chart"></div>
<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 = 200 - 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 = [
[5, 20], [480, 90], [250, 50],
[100, 33], [330, 95], [410, 12],
[475, 44], [25, 67], [85, 21],
[220, 88], [650, 100]]; */
let dataset = [];
let numDataPoints = 50;
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]);
}
// Agora todo o código pode ignorar as margens.
// Copie aqui o código de criação das escalas do arquivo
// anterior (vai continuar o mesmo)
// Crie o elemento svg
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);
let yAxis = d3.axisLeft()
.scale(yScale)
.ticks(10);
// 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", 5);
// Depois adicione os labels
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.attr("x", function(d){
return xScale(d[0]);
})
.attr("y", function(d){
return yScale(d[1]);
})
.attr("font-family", "sansserif")
.attr("font-size", "11px")
.attr("fill", "red")
.text(function(d) {
return d[0] + ',' + d[1];
});
svg.append("g")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
svg.append("g")
//.attr("transform", "translate(0," + h + ")")
.call(yAxis);
// Título eixo horizontal
svg.append("text")
.attr("transform", "translate(" + (w/2) + "," + (h + margin.bottom) + ")")
.style("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.text("Eixo X");
// Título eixo vertical
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (h / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.text("Eixo Y");
</script>
</body>
</html>