简体   繁体   中英

Drag Orthographic with circles d3.js

I'm trying to create a globe (Orthographic projection) with a drag, which also has circles on it.

I've been able to create the globe with a drag, and add circles. The problem is that when I drag the circles don't move with the globe.

Here is my bl.ock where you can find my code: http://bl.ocks.org/JulienAssouline/3caf0d6e01aa8220a8d4027cb9158d7e

I've looked at other bl.ock examples such as this one: https://bl.ocks.org/larsvers/f8efeabf480244d59001310f70815b4e

and this one: https://bl.ocks.org/curran/115407b42ef85b0758595d05c825b346

but I haven't been able to get it to work for me. Their methods seem to be quite different than mine, and I don't completely understand their code.

Would anyone know what method, or what I need to add to my code?

Thanks.

Here is my javascript code:

   (function(){
 var h = 600;
   var w = 900;
   var i = 0;
   var map = void 0;
  var world = void 0;
  var US = void 0;


    var margin = {
          top: 10,
          bottom: 40,
          left: 0,
          right: 30
        };


      var circleScale = d3.scaleSqrt()
        .domain([0, 4445])
        .range([0.5, 10])


    var width = w - margin.left - margin.right;
    var height = h - margin.top - margin.bottom;

  var dragging = function(d){
    var c = projection.rotate();
    projection.rotate([c[0] + d3.event.dx/6, c[1] - d3.event.dy/6])
    map.selectAll('path').attr('d', path);
   }

   var drag = d3.drag()
    .on("drag", dragging)


   var projection = d3.geoOrthographic().clipAngle(90); 
   var path = d3.geoPath().projection(projection);

   var svg = d3.select("body")
        .append("svg")
        .attr("id", "chart")
        .attr("width", w)
        .attr("height", h)

   d3.json("world.json", function(json){ 
     d3.csv("arms_transfer_2012_2016_top - arms_transfer_2012_2016_top.csv", function(error, data){


    var countries = topojson.feature(json, json.objects.countries).features
    var US = countries[168]


    map = svg.append('g').attr('class', 'boundary');
    world = map.selectAll('path').data(countries);
    US = map.selectAll('.US').data([US]);
    Circles = map.selectAll(".circles").data(data)

    console.log(countries[168])


    world.enter()
    .append("path")
    .attr("class", "boundary")
    .attr("d", path)


    US.enter()
      .append("path")
      .attr("class", "US")
      .attr("d", path)
      .style("fill", "lightyellow")
      .style("stroke", "orange")




          Circles.enter()
              .append("circle")
              .attr("class", "importer")
              .attr("r", function(d){
                return circleScale(d.Millions)
              })
              .attr("cx", function(d){
                var coords = projection([d.Longitude_imp, d.Latitude_imp])
                return coords[0];
              })
              .attr("cy", function(d){
                var coords = projection([d.Longitude_imp, d.Latitude_imp])
                return coords[1];
              })
              .style("fill", "#cd0d0e")

    svg.append("rect")
      .attr("class", "overlay")
      .attr("width", w)
      .attr("height", h)
      .call(drag)

      })

   })

  })();

You have to update the position of the circles in your dragging function:

var dragging = function(d) {
    var c = projection.rotate();
    projection.rotate([c[0] + d3.event.dx / 6, c[1] - d3.event.dy / 6])
    map.selectAll('path').attr('d', path);
    map.selectAll(".circles").attr("cx", function(d) {
            var coords = projection([d.Longitude_imp, d.Latitude_imp])
            return coords[0];
        })
        .attr("cy", function(d) {
            var coords = projection([d.Longitude_imp, d.Latitude_imp])
            return coords[1];
        })
}

Also, select them using the correct class.

Regarding performance, if you don't want to calculate coords twice, you can use an each :

var dragging = function(d) {
    var c = projection.rotate();
    projection.rotate([c[0] + d3.event.dx / 6, c[1] - d3.event.dy / 6])
    map.selectAll('path').attr('d', path);
    map.selectAll(".circles").each(function(d) {
        var coords = projection([d.Longitude_imp, d.Latitude_imp])
        d3.select(this).attr("cx", function(d) {
                return coords[0];
            })
            .attr("cy", function(d) {
                return coords[1];
            })
    })
}

Here is your bl.ocks with that change: http://bl.ocks.org/anonymous/dc2d4fc810550586d40d4b1ce9088422/40c6e199a5be4e152c0bd94a13ea94eba41f004b

PS: You have a problem with the circles at the far side of the globe... however, this is another issue, already addressed here at SO For instance, this answer of mine: https://stackoverflow.com/a/46441983/5768908

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM