score:2
Accepted answer
Using this answer as inspiration, you can create a second projection, equivalent to the first one, but with a larger scale
value. That will project a point directly above the actual point on the globe, as if it was hanging above it. This allows you to draw a line from the ground up, and look at it from all angles. It even works with your hide marker logic.
const width = 220;
const height = 220;
const config = {
speed: 0.025,
verticalTilt: 10,
horizontalTilt: -10
}
let locations = [];
const svg = d3.select('svg')
.attr('width', width).attr('height', height);
const markerGroup = svg.append('g');
const projection = d3.geoOrthographic()
.scale(99.5)
.translate([100, 100]);
const markerProjection = d3.geoOrthographic()
.scale(108)
.translate(projection.translate());
const path = d3.geoPath().projection(projection);
const center = [width / 2, height / 2];
drawGlobe();
drawGraticule();
enableRotation();
const locationData = [
{"latitude": -33.8688, "longitude": 151.2093}
];
function drawGlobe() {
d3.queue()
.defer(d3.json, 'https://raw.githubusercontent.com/cszang/dendrobox/master/data/world-110m2.json')
.await((error, worldData) => {
svg.selectAll(".segment")
.data(topojson.feature(worldData, worldData.objects.countries).features)
.enter().append("path")
.attr("class", "segment")
.attr("d", path)
.style("stroke", "silver")
.style("stroke-width", "1px")
.style("fill", (d, i) => 'silver')
.style("opacity", ".5");
locations = locationData;
drawMarkers();
});
}
function drawGraticule() {
const graticule = d3.geoGraticule()
.step([10, 10]);
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path)
.style("fill", "#fff")
.style("stroke", "#ececec");
}
function enableRotation() {
d3.timer(function(elapsed) {
projection.rotate([config.speed * elapsed - 120, config.verticalTilt, config.horizontalTilt]);
markerProjection.rotate(projection.rotate());
svg.selectAll("path").attr("d", path);
drawMarkers();
});
}
function drawMarkers() {
const markers = markerGroup.selectAll('.marker')
.data(locations);
const newMarkers = markers
.enter()
.append('g')
.attr('class', 'marker')
newMarkers.append("line");
newMarkers.append("circle")
.attr("r", 3);
newMarkers.merge(markers)
.selectAll("line")
.attr("x1", d => projection([d.longitude, d.latitude])[0])
.attr("y1", d => projection([d.longitude, d.latitude])[1])
.attr("x2", d => markerProjection([d.longitude, d.latitude])[0])
.attr("y2", d => markerProjection([d.longitude, d.latitude])[1])
.attr('stroke', d => {
const coordinate = [d.longitude, d.latitude];
gdistance = d3.geoDistance(coordinate, markerProjection.invert(center));
return gdistance > (Math.PI / 2) ? 'none' : 'black';
})
newMarkers
.merge(markers)
.selectAll("circle")
.attr('cx', d => markerProjection([d.longitude, d.latitude])[0])
.attr('cy', d => markerProjection([d.longitude, d.latitude])[1])
.attr('fill', d => {
const coordinate = [d.longitude, d.latitude];
gdistance = d3.geoDistance(coordinate, markerProjection.invert(center));
return gdistance > (Math.PI / 2) ? 'none' : 'tomato';
})
markerGroup.each(function() {
this.parentNode.appendChild(this);
});
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<svg></svg>
Source: stackoverflow.com
Related Query
- How to add height to a d3.js globe marker, mimicking a push-pin?
- How to maintain the SVG marker width and height ?
- How to add multiple arcs to globe
- How do you add rotation logic for mobile devices upon dragging on a 3d globe with D3.js and html canvas
- How to add different shape marker in d3?
- D3.js: How to get the computed width and height for an arbitrary element?
- How to add a tooltip to an svg graphic?
- How to add an image to an svg container using D3.js
- How to add border/outline/stroke to SVG elements in CSS?
- How to properly add and use D3 Events?
- How to use an arrow marker on an SVG <line> element?
- d3.js how to dynamically add nodes to a tree
- How to add a click event on nvd3.js graph
- How to add using d3.js selection.text() method
- How to add in zero values into a time series in d3.js / JavaScript
- How to add more metrics on the country_map in Apache-superset?
- How to add drop shadow to d3.js pie or donut chart
- How to prevent a SVG marker (arrow head) to inherit path's stroke width?
- How to add an interactive visualization to R markdown
- How to add compound node in a D3 force layout?
- d3.js: How to add labels to scatter points on graph
- How to add Label to each state in d3.js (albersUsa)?
- How to add a background color to d3 text elements?
- How to add horizontal line over y-axis in Plottable.js
- How to add a nice legend to a d3 pie chart
- How to add external javascript file in Ipython notebook
- How to add d3 (javascript) to a vaadin application?
- How do you add d3-selection-multi to d3?
- How to add a title for a NVD3.js graph
- d3.js How to add lines to a bar chart
More Query from same tag
- D3 add zoom to circle pack
- source code in tickSize d3.js v4
- CSS/alignment issues when populating page with d3.js charts
- Append a table onto a svg by id using d3
- What is the correct way to import and use d3 and its submodules in ES6?
- D3 line graph issue
- Need help making a line graph from an array and I would like custom tick labels
- How to add space between axis and line in D3 line chart?
- d3v4 bubble chart update
- d3.selectAll to change styles by class in v4
- Angularjs unknown provider nvd3
- Not able to show min and max value on x-axis in d3 js
- In an example for a worldmap for d3v4 world_countries.json can't be found
- d3js: Dynamically updating node objects
- Map not rendering, but data is correct
- Can you use transition().delay() and .classed() together?
- Add data in pie chart using D3
- D3 change order of selection/appended svgs
- Clustergrammer getting negative values for rects
- d3.js Prevent `rotate` property from parent group
- Export manually edited htmlwidget to SVG or similar
- failed to parse x to Date object
- Get value property of each element in d3 selection
- Making a graph inside a leaflet popup using geoJson data
- update clipPath alongside a dynamic yaxis in d3js
- d3.js smooth unrolling path transition
- D3 transitions on more than 1 svg element
- How to set the label for each vertical axis in a parallel coordinates visualization?
- Prevent clipping using domain in d3.js
- D3.js Can't update existing element in group