-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusa.html
153 lines (131 loc) · 4.5 KB
/
usa.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
<!DOCTYPE html>
<html>
<head>
<title>Taxa de desemprego nos EUA em agosto de 2016</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<style>
.counties {
fill: none;
}
.states {
fill: none;
stroke: #eee;
stroke-linejoin: round;
}
#tooltip {
position: absolute;
width: 240px;
height: auto;
padding: 10px;
background-color: white;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-family: sans-serif;
font-size: 16px;
line-height: 20px;
}
</style>
</head>
<body>
<main id="container" class="container">
<div id="tooltip" class="hidden">
<p><strong><span id="nome"></span></strong></p>
<p>Taxa de desemprego: <span id="taxa"></span>%</p>
</div>
<header>
<h1>Taxa de desemprego nos EUA em agosto de 2016</h1>
</header>
<div id="map"></div>
</main>
<script>
let width = 960,
height = 600;
let rateById = d3.map();
let nameById = d3.map();
//Create a quantize color scale here
// We want to see the unemployment rate that goes from 2 to 10%
// display the colors in nine different levels of greens
let quantize = d3.scaleQuantize()
.range(d3.schemeGreens[9])
.domain([2,10])
let path = d3.geoPath();
let svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
let promises = [
d3.json("data/us.json"),
d3.tsv("data/unemployment.tsv", function(d) { rateById.set(d.id, +d.rate); }),
d3.csv("data/county_names.csv", function(d) { nameById.set(d.id, d.name); })
]
Promise.all(promises).then(ready);
function ready([us]) {
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("fill", function(d) { return quantize(rateById.get(d.id)); }) //
.attr("d", path)
//.append("title")
//.text(function(d) { return "Taxa de desemprego: " + rateById.get(d.id) + "%"; })
.on("mouseover", function(d){
d3.select(this)
.style("cursor", "pointer")
.attr("stroke-width", 3)
.attr("stroke","#FFF5B1");
var coordinates = [0, 0];
coordinates = d3.mouse(this); // obtém a posição do mouse relativa a this
var x = coordinates[0] + 10;
var y = coordinates[1] + 20; // descontando a posição do svg
showTooltip(d.id, x, y);
})
.on("mouseout", function(d){
d3.select(this)
.style("cursor", "default")
.attr("stroke-width", 0)
.attr("stroke","none");
hideTooltip();
});
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("d", path);
}
function showTooltip(county_id, x, y){
d3.select("#tooltip")
.style("left", x + "px")
.style("top", y + "px")
.select("#taxa")
.text(rateById.get(county_id));
d3.select("#tooltip")
.classed("hidden", false);
d3.select("#tooltip")
.select("#nome")
.text(nameById.get(county_id))
}
function hideTooltip(){
d3.select("#tooltip")
.classed("hidden", true);
}
</script>
<!-- Bootstrap javascript files -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>