Published
Edited
May 13, 2019
Insert cell
Insert cell
chart = {
let svg = d3.select(DOM.svg(width, height));

let value = null;

let g = svg.append("g");
var zoom = d3.zoom().on("zoom", zoomed);
svg
.call(zoom); // delete this line to disable free zooming
// .call(zoom.event); // not in d3 v4
const context = DOM.context2d(width, height);
const path = d3.geoPath(projection, context);

var countries = topojson.feature(world, world.objects.ne_110m_admin_0_countries).features
const outlineClick = svg.append("path")
.attr("fill", "red")
.attr("stroke","red")
.attr("stroke-width", "0.1px")
.attr("stroke-linejoin", "round")
.attr("pointer-events","none")
function render() {
context.clearRect(0, 0, width, height);
context.beginPath(), path(sphere), context.fillStyle = "#fff", context.fill();
context.beginPath(), path(land), context.fillStyle = "#000", context.fill();
context.beginPath(), path(land), context.strokeStyle = "#f00000", context.stroke();
context.beginPath(), path(sphere), context.stroke();
}
function zoomed() {
g.style("stroke-width", 1.5 / d3.event.transform.k + "px");
// g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"); // not in d3 v4
g.attr("transform", d3.event.transform); // updated for d3 v4
}
svg.selectAll(".country")
.data(countries)
.enter().insert("path", ".graticule")
.attr("class", function(d) { return "country " + "code" + d.id; })
.attr("d", path)
.attr("opacity","0.5")
.style("fill", "yellow")
.on("mouseover", d => {
console.log("moused over country");
const node = svg.node();
node.value = value = value === d.properties.ADMIN ? null : d.properties.ADMIN;
node.dispatchEvent(new CustomEvent("input"));
outlineClick.attr("d", value ? path(d) : null);
})

return d3.select(context.canvas)
.call(drag(projection).on("drag.render", render))
.call(render)
.node();
}
Insert cell
function drag(projection) {
let v0, q0, r0;
function dragstarted() {
v0 = versor.cartesian(projection.invert(d3.mouse(this)));
q0 = versor(r0 = projection.rotate());
}
function dragged() {
const v1 = versor.cartesian(projection.rotate(r0).invert(d3.mouse(this)));
const q1 = versor.multiply(q0, versor.delta(v0, v1));
projection.rotate(versor.rotation(q1));
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged);
}
Insert cell
projection = d3.geoOrthographic()
.fitExtent([[1, 1], [width - 1, height - 1]], sphere)
.translate([width / 2, height / 2])
.precision(0.1)
Insert cell
height = Math.min(640, width)
Insert cell
sphere = ({type: "Sphere"})
Insert cell
land = topojson.feature(world, world.objects.ne_110m_admin_0_countries)
Insert cell
world = d3.json("https://gist.githubusercontent.com/jk979/e36a0b630a3f9565e7c2aeeaba002984/raw/e01abe1dc330e28484dbaafd44a7f2ae62f802ba/countries_110.json")
Insert cell
versor = require("versor@0.0.3")
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