2016-09-22 3 views
0

По совету другого пользователя я решил переделать свою карту с помощью v4. Итак, имея это в виду, я начал с простой карты и читал the arcs docs, но как применить это к карте?Как добавить дугу d3 v4 map

var arc = d3.arc(); 
arc({ 
    innerRadius: 0, 
    outerRadius: 100, 
    startAngle: 0, 
    endAngle: Math.PI/2 
}); // "M0,-100A100,100,0,0,1,100,0L0,0Z" 

plnkr: http://plnkr.co/edit/ttXZdjmZDaqtTNvhb6Qz?p=preview

ответ

2

Я оставил тебя висит на ваш комментарий вопрос в вашем предыдущем вопросе, так вот быстрый удар на то, что вы после этого. Исходный рисунок карты из примера d3here. После рисования я добавляю строку с пользовательской дугой, заимствованной из here. Затем я использую attrTween с "stroke-dasharray", чтобы оживить линию, и в конце этого перехода используйте другой переход для увеличения круга.

Running Код:

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<style> 
 
    .stroke { 
 
    fill: none; 
 
    stroke: #000; 
 
    stroke-width: 3px; 
 
    } 
 
    
 
    .fill { 
 
    fill: #fff; 
 
    } 
 
    
 
    .graticule { 
 
    fill: none; 
 
    stroke: #777; 
 
    stroke-width: 0.5px; 
 
    stroke-opacity: 0.5; 
 
    } 
 
    
 
    .land { 
 
    fill: #222; 
 
    } 
 
    
 
    .boundary { 
 
    fill: none; 
 
    stroke: #fff; 
 
    stroke-width: 0.5px; 
 
    } 
 
</style> 
 
<svg width="960" height="960"></svg> 
 
<script src="//d3js.org/d3.v4.js"></script> 
 
<script src="//d3js.org/topojson.v1.min.js"></script> 
 
<script> 
 
    var svg = d3.select("svg"), 
 
    width = +svg.attr("width"), 
 
    height = +svg.attr("height"); 
 

 
    var projection = d3.geoMercator() 
 
    .scale((width - 3)/(2 * Math.PI)) 
 
    .translate([width/2, height/2]); 
 

 
    var path = d3.geoPath() 
 
    .projection(projection); 
 

 
    var graticule = d3.geoGraticule(); 
 

 
    svg.append("defs").append("path") 
 
    .datum({ 
 
     type: "Sphere" 
 
    }) 
 
    .attr("id", "sphere") 
 
    .attr("d", path); 
 

 
    svg.append("use") 
 
    .attr("class", "stroke") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("use") 
 
    .attr("class", "fill") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("path") 
 
    .datum(graticule) 
 
    .attr("class", "graticule") 
 
    .attr("d", path); 
 

 
    d3.json("https://rawgit.com/d3/d3-geo/master/test/data/world-50m.json", function(error, world) { 
 
    if (error) throw error; 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.feature(world, world.objects.land)) 
 
     .attr("class", "land") 
 
     .attr("d", path); 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.mesh(world, world.objects.countries, function(a, b) { 
 
     return a !== b; 
 
     })) 
 
     .attr("class", "boundary") 
 
     .attr("d", path); 
 

 

 
    var coordinates = [ 
 
     projection([-74, 40]), // new york 
 
     projection([-43, -22]) // rio 22.9068° S, 43.1729° W 
 
    ]; 
 

 
    var line = svg.append("path") 
 
     .datum(coordinates) 
 
     .attr("d", function(c) { 
 
     var d = { 
 
      source: c[0], 
 
      target: c[1] 
 
     }; 
 
     var dx = d.target[0] - d.source[0], 
 
      dy = d.target[1] - d.source[1], 
 
      dr = Math.sqrt(dx * dx + dy * dy); 
 
     return "M" + d.source[0] + "," + d.source[1] + "A" + dr + "," + dr + 
 
      " 0 0,1 " + d.target[0] + "," + d.target[1]; 
 
     }) 
 
     .style("stroke", "steelblue") 
 
     .style("stroke-width", 5) 
 
     .style("fill", "none") 
 
     .transition() 
 
     .duration(5000) 
 
     .attrTween("stroke-dasharray", function() { 
 
     var len = this.getTotalLength(); 
 
     return function(t) { 
 
      return (d3.interpolateString("0," + len, len + ",0"))(t) 
 
     }; 
 
     }) 
 
     .on('end', function(d) { 
 
     var c = coordinates[1]; 
 
     svg.append('circle') 
 
      .attr('cx', c[0]) 
 
      .attr('cy', c[1]) 
 
      .attr('r', 0) 
 
      .style('fill', 'red') 
 
      .style('fill-opacity', '0.5') 
 
      .transition() 
 
      .duration(2000) 
 
      .attr('r', 50) 
 
      .on('end', function(d) { 
 
      d3.select(this) 
 
       .transition() 
 
       .duration(2000) 
 
       .attr('r', 10); 
 
      }); 
 
     }); 
 
    }); 
 
</script>

+0

Здравствуйте! Вы знаете ответ на этот вопрос: https://stackoverflow.com/questions/43696831/why-dont-the-flags-appear? – Coder1000

+0

Линия между точками не точна. Рисование над экватором приводит к сдвигу nortdward – jolly

+0

@pksingh, вам нужно быть более конкретным или привести пример. Я обновил приведенный выше код для запуска из Нью-Йорка в Рио-ду-Жанейро («над экватором»). выглядит точно для меня. – Mark

0

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<style> 
 
    .stroke { 
 
    fill: none; 
 
    stroke: #000; 
 
    stroke-width: 3px; 
 
    } 
 
    
 
    .fill { 
 
    fill: #fff; 
 
    } 
 
    
 
    .graticule { 
 
    fill: none; 
 
    stroke: #777; 
 
    stroke-width: 0.5px; 
 
    stroke-opacity: 0.5; 
 
    } 
 
    
 
    .land { 
 
    fill: #222; 
 
    } 
 
    
 
    .boundary { 
 
    fill: none; 
 
    stroke: #fff; 
 
    stroke-width: 0.5px; 
 
    } 
 
</style> 
 
<svg width="960" height="960"></svg> 
 
<script src="//d3js.org/d3.v4.js"></script> 
 
<script src="//d3js.org/topojson.v1.min.js"></script> 
 
<script> 
 
    var svg = d3.select("svg"), 
 
    width = +svg.attr("width"), 
 
    height = +svg.attr("height"); 
 

 
    var projection = d3.geoMercator() 
 
    .scale((width - 3)/(2 * Math.PI)) 
 
    .translate([width/2, height/2]); 
 

 
    var path = d3.geoPath() 
 
    .projection(projection); 
 

 
    var graticule = d3.geoGraticule(); 
 

 
    svg.append("defs").append("path") 
 
    .datum({ 
 
     type: "Sphere" 
 
    }) 
 
    .attr("id", "sphere") 
 
    .attr("d", path); 
 

 
    svg.append("use") 
 
    .attr("class", "stroke") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("use") 
 
    .attr("class", "fill") 
 
    .attr("xlink:href", "#sphere"); 
 

 
    svg.append("path") 
 
    .datum(graticule) 
 
    .attr("class", "graticule") 
 
    .attr("d", path); 
 

 
    d3.json("https://rawgit.com/mbostock/topojson/master/examples/world-50m.json", function(error, world) { 
 
    if (error) throw error; 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.feature(world, world.objects.land)) 
 
     .attr("class", "land") 
 
     .attr("d", path); 
 

 
    svg.insert("path", ".graticule") 
 
     .datum(topojson.mesh(world, world.objects.countries, function(a, b) { 
 
     return a !== b; 
 
     })) 
 
     .attr("class", "boundary") 
 
     .attr("d", path); 
 

 

 
    var coordinates = [ 
 
     projection([-74, 40]), // new york 
 
     projection([37, 55]) // moscow 
 
    ]; 
 

 
    var line = svg.append("path") 
 
     .datum(coordinates) 
 
     .attr("d", function(c) { 
 
     var d = { 
 
      source: c[0], 
 
      target: c[1] 
 
     }; 
 
     var dx = d.target[0] - d.source[0], 
 
      dy = d.target[1] - d.source[1], 
 
      dr = Math.sqrt(dx * dx + dy * dy); 
 
     return "M" + d.source[0] + "," + d.source[1] + "A" + dr + "," + dr + 
 
      " 0 0,1 " + d.target[0] + "," + d.target[1]; 
 
     }) 
 
     .style("stroke", "steelblue") 
 
     .style("stroke-width", 5) 
 
     .style("fill", "none") 
 
     .transition() 
 
     .duration(5000) 
 
     .attrTween("stroke-dasharray", function() { 
 
     var len = this.getTotalLength(); 
 
     return function(t) { 
 
      return (d3.interpolateString("0," + len, len + ",0"))(t) 
 
     }; 
 
     }) 
 
     .on('end', function(d) { 
 
     var c = coordinates[1]; 
 
     svg.append('circle') 
 
      .attr('cx', c[0]) 
 
      .attr('cy', c[1]) 
 
      .attr('r', 0) 
 
      .style('fill', 'red') 
 
      .style('fill-opacity', '0.5') 
 
      .transition() 
 
      .duration(2000) 
 
      .attr('r', 50) 
 
      .on('end', function(d) { 
 
      d3.select(this) 
 
       .transition() 
 
       .duration(2000) 
 
       .attr('r', 10); 
 
      }); 
 
     }); 
 
    }); 
 
</script>

Смежные вопросы