Published
Edited
Jun 25, 2020
Insert cell
Insert cell
Insert cell
Insert cell
buildvis = {
const width = 960
const height = 600
const svg = d3.select(DOM.svg(width, height))
let path = d3.geoPath()
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("fill", d => colorScale(rateById.get(d.id)))
.attr("d", path)
.on("mouseover", function(d){
d3.select(this) // seleciona o elemento atual
.style("cursor", "pointer") //muda o mouse para mãozinha
.attr("stroke-width", 3)
.attr("stroke","#FFF5B1");
const rect = this.getBoundingClientRect();
showTooltip(d.id, rect.x, rect.y);
})
.on("mouseout", function(d){
d3.select(this)
.style("cursor", "default")
.attr("stroke-width", 0)
.attr("stroke","none"); //volta ao valor padrão
hideTooltip();
})

svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("d", path)
// Once we append the vis elments to it, we return the DOM element for Observable to display above.
return svg.node()
}
Insert cell
tooltip = {
d3.select("#tooltip").remove()
let node = d3.select("body")
.append("div")
.attr("id", "tooltip")
.attr("class", "hidden")
.append("p")
.html("<b id='name'></b><br/>Taxa de desemprego: <span id='taxa'></span>%")
return node
}
Insert cell
function showTooltip(county_id, x, y) {
const offset = 10;
const t = d3.select("#tooltip");
t.select("#name").text(nameById.get(county_id));
t.select("#taxa").text(rateById.get(county_id));
t.classed("hidden", false);
const rect = t.node().getBoundingClientRect();
const w = rect.width;
const h = rect.height;
if (x + offset + w > width) {
x = x - w;
}
t.style("left", x + offset + "px").style("top", y - h + "px");
}
Insert cell
function hideTooltip() {
d3.select("#tooltip")
.classed("hidden", true)
}
Insert cell
colorScale = d3.scaleQuantize()
.domain([1,10])
.range(d3.schemeGreens[9])
Insert cell
rateById =
d3.tsv("https://gist.githubusercontent.com/emanueles/7fbdcd8ee412e3e63904114cf9d7ee3a/raw/af4fd93db1be3f9033caff46d4a574a54bf9363f/unemployment.tsv").then(function (data) {
let rateMap = d3.map()
data.forEach(function(d) {
rateMap.set(d.id, +d.rate)
})
return rateMap
})
Insert cell
nameById =
d3.csv("https://gist.githubusercontent.com/emanueles/19cf3828bab232612c2b1599d831f690/raw/ddbaf62af22512a39360cc6741b40f5588b44aa0/county_names.csv").then(function (data) {
let nameMap = d3.map()
data.forEach(function(d) {
nameMap.set(d.id, d.name)
})
return nameMap
})


Insert cell
us = d3.json("https://gist.githubusercontent.com/emanueles/ee2997bc6237f3dcab952ae8becaf6bf/raw/afdb12d4ef89bb239998b295c94d960e8d89c19e/us.json")
Insert cell
Insert cell
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require('d3@5')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more