Published unlisted
Edited
Feb 4, 2021
Insert cell
md`# Globe 2`
Insert cell
d3 = require("https://d3js.org/d3.v4.min.js")
Insert cell
html`<div id="map" style="width: 100%;"></div>`
Insert cell
url = "https://raw.githubusercontent.com/mgckind/desaccess/deslabs/easyweb/static/tiles/all_tiles.json"
Insert cell
tiles = (await fetch(url)).json()
Insert cell
{
let width = d3.select("#map").node().getBoundingClientRect().width
let height = 500
const sensitivity = 75
let context = this ? this.getContext("2d") : DOM.context2d(width,width);
let projection = d3.geoOrthographic()
.scale(250)
.center([0, 0])
.rotate([0,-30])
.translate([width / 2, height / 2])

const initialScale = projection.scale()
let path = d3.geoPath().projection(projection)
let svg = d3.select("#map")
.append("svg")
.attr("width", width)
.attr("height", height)

let globe = svg.append("circle")
.attr("fill", "aliceblue")
.attr("stroke", "#000")
.attr("stroke-width", "0.2")
.attr("cx", width/2)
.attr("cy", height/2)
.attr("r", initialScale)
const graticule = d3.geoGraticule()


svg.append("path")
.datum(graticule())
.attr("class", "graticule")
.attr("d", path)
.attr('fill', 'none')
.attr('stroke', '#cccccc')
.attr('stroke-width', '0.5px');

svg.call(d3.drag().on('drag', () => {
const rotate = projection.rotate()
const k = sensitivity / projection.scale()
projection.rotate([
rotate[0] + d3.event.dx * k,
rotate[1] - d3.event.dy * k
])
path = d3.geoPath().projection(projection)
svg.selectAll("path").attr("d", path)
}))
.call(d3.zoom().on('zoom', () => {
if(d3.event.transform.k > 0.3) {
projection.scale(initialScale * d3.event.transform.k)
path = d3.geoPath().projection(projection)
svg.selectAll("path").attr("d", path)
globe.attr("r", projection.scale())
}
else {
d3.event.transform.k = 0.3
}
}))

let map = svg.append("g")
d3.json("https://raw.githubusercontent.com/michael-keith/mps_interests/master/view/js/charts/data/world_map.json", function(err, d) {
map.append("g")
.attr("class", "countries" )
.selectAll("path")
.data(d.features)
.enter().append("path")
.attr("class", d => "country_" + d.properties.name.replace(" ","_"))
.attr("d", path)
.attr("fill", "mintcream")
.style('stroke', 'black')
.style('stroke-width', 0.3)
.style("opacity",0.8)
})
d3.json(url, function(err, d) {
map.append("g")
.attr("class", "countries" )
.selectAll("path")
.data(d.features)
.enter().append("path")
.attr("class", d => "country_" + d.properties.name.replace(" ","_"))
.attr("d", path)
.attr("fill", "mintcream")
.style('stroke', 'blue')
.style('stroke-width', 0.3)
.style("opacity",0.8)
})
}

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