"飞行动画"在d3.js v3

时间:2017-06-30 12:16:11

标签: javascript d3.js geolocation

我目前正试图绕着地图周围的连接点并将它们设置为动画,如下所示:

  

http://www.tnoda.com/flightanimation

我希望使用CSV文件中的信息连接点: 特别是'其中'将告诉我们的地方是飞行目的地或起源的地方:

code,city,country,lat,lon,where
ZNZ,ZANZIBAR,TANZANIA,-6.13,39.31,dest
TYO,TOKYO,JAPAN,35.68,139.76,dest
AKL,AUCKLAND,NEW ZEALAND,-36.85,174.78,orgin
BKK,BANGKOK,THAILAND,13.75,100.48,orgin
DEL,DELHI,INDIA,29.01,77.38,orgin
SIN,SINGAPORE,SINGAPOR,1.36,103.75,orgin
BSB,BRASILIA,BRAZIL,-15.67,-47.43,orgin
RIO,RIO DE JANEIRO,BRAZIL,-22.90,-43.24,orgin
YTO,TORONTO,CANADA,43.64,-79.40,orgin
IPC,EASTER ISLAND,CHILE,-27.11,-109.36,orgin
SEA,SEATTLE,USA,47.61,-122.33,orgin

(我知道我故意拼错了它的原因)

现在这是我的HTML代码:



<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.13/d3.min.js"></script>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
path {
        stroke: limegreen;
        stroke-width: 0.25px;
        fill: black;
        margin: 0 auto;
}
    body {
        background-color:darkgrey;
        margin: 0 auto;
    }
.packet {
        max-height:  height: 50px;
        max-width: 50px;
        fill: limegreen;

    }
</style>
<body>
<script type="text/javascript" src="http://gc.kis.v2.scr.kaspersky-labs.com/3F7B1EB8-32BF-7449-968C-CB1318D27635/main.js" charset="UTF-8"></script><link rel="stylesheet" crossorigin="anonymous" href="http://gc.kis.v2.scr.kaspersky-labs.com/53672D8131BC-C869-9447-FB23-8BE1B7F3/abn/main.css"/><script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
var width = 960,
    height = 500;
    
var projection = d3.geo.mercator()// Creating our projection for our map


var svg = d3.select("body").append("svg")//Append svg to body
    .attr("width", width)
    .attr("height", height);

var path = d3.geo.path()//create a path for the projection
    .projection(projection);

var g = svg.append("g"); //create an empty space to append

var packet = svg.append("path")
    .attr("width","50px")
    .attr("d", "M612.074,132.141v-2.38c0-8.849-4.016-19.26-11.229-26.473l-0.818-0.818c0,0-0.818,0-0.818-0.818   c-1.636-1.636-3.198-2.38-4.833-4.016c-0.818,0-0.818-0.818-1.636-0.818c-1.636-0.818-4.016-1.636-5.652-2.38   c-0.818,0-0.818-0.818-1.636-0.818c-2.38-0.818-4.833-1.636-7.213-1.636c-0.818,0-0.818,0-1.636,0c-2.38,0-5.651-0.818-8.849-0.818   H43.427c-3.198,0-6.395,0-9.667,0.818c-0.818,0-1.636,0-2.38,0.818c-2.38,0.818-4.834,0.818-6.395,1.636   c-0.818,0-0.818,0.818-1.636,0.818c-1.636,0.818-4.016,1.636-5.652,2.38l-0.818,0.818c-1.636,0.818-3.198,2.38-4.834,3.198   c-0.818,0.818-1.636,1.636-2.38,2.38C4.016,110.428,0.818,117.715,0,125.746c0,0.818,0,0.818,0,1.636v357.384   c0,0.818,0,0.818,0,1.636c1.636,11.229,7.213,20.896,15.244,26.473c7.213,4.833,16.062,8.031,26.473,8.031H569.39c0,0,0,0,0.818,0   l0,0c2.38,0,5.651,0,8.031-0.818c0.818,0,0.818,0,1.636,0c2.38-0.818,4.834-0.818,6.395-1.636h0.818   c17.698-6.395,24.911-21.714,24.911-36.14v-2.38v-0.818v-0.818V134.521c0-0.818,0-0.818,0-1.636   C612.074,132.959,612.074,132.959,612.074,132.141z M560.69,120.913l-252.98,246.51l-57.854-56.218l0,0L51.459,120.838H560.69   V120.913z M29.819,475.099V140.991l187.095,179.882L29.819,475.099z M299.679,491.905H56.292l182.336-149.393l58.597,57.036   c2.38,2.38,4.834,3.198,7.213,4.016h0.818c0.818,0,0.818,0,1.636,0l0,0c0.818,0,1.636,0,1.636,0h0.818   c2.38-0.818,5.651-1.636,7.213-4.016l55.4-53.838l183.079,146.196H299.679z M582.329,475.843L394.417,324.07L582.329,140.99   V475.843z");
    
    var route = svg.append("path");
    
// load and display the World
d3.json("world-110m2.json", function(error, topology) {//Load in the world map ( LOW RES)
    g.selectAll("path")
      .data(topojson.object(topology, topology.objects.countries)
          .geometries)
    .enter()
      .append("path")//append path
      .attr("d", path)//path is d3.geo.path which is the projection
    
    //Loading the countries inside the world load display function to speed up the loading on local server and faster or client
    //Loading the countries here also prevents the dots to be under the map instead on top of it!
    d3.csv("countries.csv", function(error, data) {
        g.selectAll("circle")
            .data(data)
            .enter()
            .append("circle")
            .attr("cx", function(d) {
                return projection([d.lon, d.lat])[0];
                })
            .attr("cy", function(d) {
                return projection([d.lon, d.lat])[1];})
            .attr("r", 5)
            .style("fill", "red");
        //Writing out the Cities name
        g.selectAll("text")
            .attr("class","names")
            .data(data)
            .enter()
            .append("text") // append text
            .attr("x", function(d) {
                return projection([d.lon, d.lat])[0];
                })
            .attr("y", function(d) {
                return projection([d.lon, d.lat])[1];
                })
            .attr("dy", -7) // set y position of bottom of text
            .style("fill", "limegreen") // fill the text with the colour black
            .attr("text-anchor", "middle") // set anchor y justification
            .text(function(d) {return d.city;}); // define the text to display
        
        //Test
        route.selectAll("path")
               .datum({type: "LineString", coordinates: 
                      [
                          function(d) {
                              if (d.where === origin){
                                return projection(d.lat,d.lon)
                              }},
                        function(d) {
                            if (d.where === dest){
                                return projection(d.lat,d.lon)
                              }}
                      ]
                      })
               .attr("class", "route")
               .attr("d", path);
            });
    
    //Animating path
    
    // Map Zooimng
var zoom = d3.behavior.zoom()
    .on("zoom",function() {
        g.attr("transform","translate("+
            d3.event.translate.join(",")+")scale("+d3.event.scale+")");
        g.selectAll("path")
            .attr("d", path.projection(projection));
});
svg.call(zoom)
});
    
function transition(packet, route) {
  var l = route.node().getTotalLength();
  packet.transition()
       .duration(5000)
       .attrTween("transform", delta(route.node()));
}
  
function delta(path) {
  var l = path.getTotalLength();
  return function(i) {
    return function(t) {
      var p = path.getPointAtLength(t * l);
      return "translate(" + p.x + "," + p.y + ")";
    }
  }
}
transition(packet, route);
</script>
</body>
</html>
&#13;
&#13;
&#13;

此外,如果有人能告诉我为什么我的SVG元素没有调整大小会很好但我会说我能够弄明白。

如果有人可以向我解释为什么代码不起作用 - 在我的逻辑中指出我的错误并纠正我,这将非常有帮助。 我这样做是因为数据可视化现在已经征服了我的兴趣。

干杯!

同时希望我的评论对那些可能遇到同样问题的人有所帮助,并试图围绕代码进行思考!

0 个答案:

没有答案
相关问题