I'm trying to map several points on an interactive 3D globe using d3. The points show up at first, but after trying to rotate the globe, they disappear until the page is reloaded. This problem does not occur when I only use a pan/zoom feature.
Here's the code for adding the points:
d3.csv("cities.csv").then(function(data) {
svg.selectAll("circle").data(data)
.enter()
.append("circle")
.attr("class", "cities")
.attr("r", 3)
.attr("cx", d => projection([d.lon,d.lat])[0])
.attr("cy", d => projection([d.lon,d.lat])[1])
})
Here's the code for rotating:
var mapZoom = d3.zoom()
.on("zoom", zoomed);
var zoomSettings = d3.zoomIdentity
.translate(0, 0)
.scale(120);
var rotateScale = d3.scaleLinear()
.domain([-500, 0, 500])
.range([-180, 0, 180]);
d3.select("svg").call(mapZoom).call(mapZoom.transform, zoomSettings);
function zoomed() {
var e = d3.event;
var currentRotate = rotateScale(e.transform.x) % 360
projection.rotate([currentRotate, 0])
.scale(e.transform.k)
d3.selectAll("path.graticule").attr("d", path);
d3.selectAll("path.countries").attr("d", path)
.style("fill", d => newFeatureColor(d3.geoArea(d)))
.style("stroke", d => d3.rgb(newFeatureColor(d3.geoArea(d))).darker())
d3.selectAll("circle.cities").each(function (d, i) {
var projectedPoint = projection([d.x,d.y])
var x = parseInt(d.x)
var display = x + currentRotate < 90 && x + currentRotate > -90
|| (x + currentRotate < -270 && x + currentRotate > -450)
|| (x + currentRotate > 270 && x + currentRotate < 450)
? "block" : "none"
d3.select(this)
.attr("cx", projectedPoint[0])
.attr("cy", projectedPoint[1])
.style("display", display)
})
}
This is the format of the projection used with the cx and cy coordinates:
var projection = d3.geoOrthographic() .scale(200) .translate([width/2, height/2]) .center([0,0])
Any help would be appreciated!