Public
Edited
Jan 17, 2023
Insert cell
Insert cell
chart = {
const path = d3.geoPath().projection(projection);;

const svg = d3.create("svg")
.attr("viewBox", "0 0 960 600")
.style("width", "100%")
.style("height", "auto");

svg.append("g")
.selectAll("path")
.data(topojson.feature(us, us.objects.ESP_adm1).features)
.enter()
.append("path")
.attr("fill", d => {
console.log(d.properties.HASC_1 + ": "+ data1.get(d.properties.HASC_1));
return color(data1.get(d.properties.HASC_1))
})
.attr("d", path)
.on("mouseover", function(d){
return tooltip.style("visibility", "visible").text(d.properties.NAME_1 + ": " + data1.get(d.properties.HASC_1));
})
.on("mousemove", function(){
return tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");
})
.on("mouseout", function(){
return tooltip.style("visibility", "hidden");
});
svg.append("path")
.datum(topojson.mesh(us, us.objects.ESP_adm1, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("d", path);

return svg.node();
}
Insert cell
tooltip = d3.select("body").append("div")
.attr("class", "svg-tooltip")
.style("position", "absolute")
.style("visibility", "hidden")
.text("I'm a circle!");
Insert cell
projection = d3.geoMercator()
// centro del mapa en grados
.center([0, 37])
// zoomlevel
.scale(1600)
// map-rotation
.rotate([0,0]);
Insert cell
color = d3.scaleSequentialQuantile(d3.range(100).map(() => Math.random() ** 2), d3.interpolateBlues)
Insert cell
mydata=d3.dsv(";","https://raw.githubusercontent.com/Alexrendon/D3J/main/drogas.csv")
Insert cell
data1 = new Map(mydata.map(d => [d.code, parseFloat(d['Porcentaje total'])]))
Insert cell
states = new Map(us.objects.ESP_adm1.geometries.map(d => [d.properties.HASC_1, d.properties]))
Insert cell
us = d3.json("https://raw.githubusercontent.com/deldersveld/topojson/master/countries/spain/spain-comunidad-with-canary-islands.json")
Insert cell
Insert cell
Insert cell
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