首页 \ 问答 \ d3.js:从文件的地图上的两点之间绘制弧线(d3.js: Drawing arcs between two points on map from file)

d3.js:从文件的地图上的两点之间绘制弧线(d3.js: Drawing arcs between two points on map from file)

我是d3.js的新手,我正在尝试一些简单的方法。 我绘制了一个读取file1和file2的世界地图。 file2按indexID,lat和lon列出机场。 file1通过它们的indexID配对机场。 我想绘制一条弧线,一条线或任何连接它们的东西。 这个想法是产生这样的东西: http : //mbostock.github.io/d3/talk/20111116/airports.html与一个不同的数据集,但这个例子太难以遵循。

下面的代码正确地绘制了地图和机场圈图,但仍有待观察如何连接它们。

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script type="text/javascript" src="d3/d3.v3.js"></script>
<script src="js/topojson.v0.min.js"></script>
<script>
    var width = 2000, height = 2000;
    var projection = d3.geo.mercator().center([0, 5]).scale(100).rotate([0, 0]);
    var svg = d3.select("body").append("svg").attr("width", width).attr("height", height);
    var path = d3.geo.path().projection(projection);
    var g = svg.append("g");

    d3.json("json/world-110m2.json", function(error, topology) {// load and display the World
        g.selectAll("path").data(topojson.object(topology, topology.objects.countries).geometries).enter().append("path").attr("d", path)
    });

    d3.csv("file1", function(flights) { //Attempt to draw arcs
        var linksByOrigin = {}, countByAirport = {}, locationByAirport = {}, positions = [];

        var arc = d3.geo.greatArc().source(function(d) {
            return locationByAirport[d.source];
        }).target(function(d) {
            return locationByAirport[d.target];
        });

        flights.forEach(function(flight) {
            var origin = flight.origin, destination = flight.destination, links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
            links.push({
                source : origin,
                target : destination
            });
            countByAirport[origin] = (countByAirport[origin] || 0) + 1;
            countByAirport[destination] = (countByAirport[destination] || 0) + 1;
        });

        d3.csv("file2", function(error, data) {// read in and plot the circles
            g.selectAll(".blue.circle").data(data).enter().append("circle").attr("class", "blue circle").attr("cx", function(d) {
                return projection([d.lon, d.lat])[0];
            }).attr("cy", function(d) {
                return projection([d.lon, d.lat])[1];
            });

            g.selectAll("path.arc").data(function(d) {
                return linksByOrigin[data.ctuid] || [];
            }).enter().append("svg:path").attr("class", "arc").attr("d", function(d) {
                return path(arc(d));
            });
        });
    });
</script>
</body>
</html>

我对此很陌生,所以代码可能很sl,,但是关于从CSV中提取连接点的任何提示都将不胜感激。 谢谢!


I am new to d3.js and am trying something simple. I have drawn a world map that reads in file1 and file2. file2 lists airports by an indexID, lat, and lon. file1 pairs the airports by their indexID. I want to draw an arc, line, or anything to connect them. The idea was to produce something like this: http://mbostock.github.io/d3/talk/20111116/airports.html with a different data set but this example was too hard to follow.

The code below correctly draws the map and plots circles for the airports, but remains to be seen how to connect them.

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script type="text/javascript" src="d3/d3.v3.js"></script>
<script src="js/topojson.v0.min.js"></script>
<script>
    var width = 2000, height = 2000;
    var projection = d3.geo.mercator().center([0, 5]).scale(100).rotate([0, 0]);
    var svg = d3.select("body").append("svg").attr("width", width).attr("height", height);
    var path = d3.geo.path().projection(projection);
    var g = svg.append("g");

    d3.json("json/world-110m2.json", function(error, topology) {// load and display the World
        g.selectAll("path").data(topojson.object(topology, topology.objects.countries).geometries).enter().append("path").attr("d", path)
    });

    d3.csv("file1", function(flights) { //Attempt to draw arcs
        var linksByOrigin = {}, countByAirport = {}, locationByAirport = {}, positions = [];

        var arc = d3.geo.greatArc().source(function(d) {
            return locationByAirport[d.source];
        }).target(function(d) {
            return locationByAirport[d.target];
        });

        flights.forEach(function(flight) {
            var origin = flight.origin, destination = flight.destination, links = linksByOrigin[origin] || (linksByOrigin[origin] = []);
            links.push({
                source : origin,
                target : destination
            });
            countByAirport[origin] = (countByAirport[origin] || 0) + 1;
            countByAirport[destination] = (countByAirport[destination] || 0) + 1;
        });

        d3.csv("file2", function(error, data) {// read in and plot the circles
            g.selectAll(".blue.circle").data(data).enter().append("circle").attr("class", "blue circle").attr("cx", function(d) {
                return projection([d.lon, d.lat])[0];
            }).attr("cy", function(d) {
                return projection([d.lon, d.lat])[1];
            });

            g.selectAll("path.arc").data(function(d) {
                return linksByOrigin[data.ctuid] || [];
            }).enter().append("svg:path").attr("class", "arc").attr("d", function(d) {
                return path(arc(d));
            });
        });
    });
</script>
</body>
</html>

I am new to this so the code may be sloppy, but any hints about connecting points pulled from a CSV would be greatly appreciated. Thank you!


原文:https://stackoverflow.com/questions/17156283
更新时间:2022-05-31 09:05

最满意答案

我希望以下答案能帮到你。

function searchPos(){
if ($(window).width() < 461) {
      $(".wrap1 .login, .wrap2 .search").prependTo($(".wrap3"));
    } else {
      $(".wrap3 .login").appendTo(".wrap1");
      $(".wrap3 .search").appendTo(".wrap2");
    }
}
searchPos();    
$(window).resize(function(){
    searchPos();    
});

见演示


Found it!

@nnnnnn was actually right:

There were multiple wraps. My JS is actually $(".login").appendTo(".wrap1 .centerwrap")

(it's still called wrap1 for comprehension)

So there wouldn't be any problems, but I forgot that there were multiple nested (bc of pos:abs navigation) centerwraps in .wrap2. That's why the .wrap1 .login worked perfectly but the .wrap2 .search didn't.

Solved it with

$(".login").appendTo(".wrap1 > .centerwrap");
$(".search").appendTo(".wrap2 > .centerwrap");

And yes, I feel dumb. Have been looking for the answer for 2 days now.

相关问答

更多
  • append或者appendTo的意思是移动DOM,而不是复制DOM   原理:代码1中,首先返回了一个选中的option对象,然后将该对象remove()操作,这时它返回了一个jQuery对象并存入$remove对象中,最后将$remove对象内嵌进#select2中。这里的$remove==$options代码2中,直接将选中的option对象,追加到了#select2中。大概你所谓的自带删除,是指#select1中的option项没有了的意思吧。这其实属于元素的移动。值得注意的一点是:使用remove ...
  • 我希望以下答案能帮到你。 function searchPos(){ if ($(window).width() < 461) { $(".wrap1 .login, .wrap2 .search").prependTo($(".wrap3")); } else { $(".wrap3 .login").appendTo(".wrap1"); $(".wrap3 .search").appendTo(".wrap2"); } } searchPos(); ...
  • 只需用.append()替换.append()并添加.next()来定位移动的行。 function transfer() { $('#gallery_' + galleryID).fadeOut(function(){ $(this).after('#tbl_proofed tr:last').next().fadeIn(); }); } 编辑:我可能误解了你的问题。 如果你想让副本走向另一个方向,那就是: .insertAfter() function transfer() { ...
  • 这里有一个很好的例子,有一些额外的触动: http : //jsfiddle.net/Twisty/f700oxds/4/ HTML

    Hello World!

  • 你错误地在按钮后附加div,而不是你的
    : http : //jsfiddle.net/ZTuhn/1/ 您也可以通过正确选择前面的问题ID然后在此之后附加来修复此错误。 但是,这需要对初始的questionBox id属性进行一些异常处理。 You are mistakenly appending the div after the button, rather than your
    : http://jsfiddle.net/ZTuhn/1/ You ...
  • 由于您只能从新下拉列表中发布所选选项,因此您可能应该使用隐藏输入来将值填充为逗号分隔列表或类似内容。 唯一的其他解决方案是使用jquery $ .post发布值。 这样,在允许jquery使用您自己的值提交表单之前,您将能够遍历选择并查找其选项具有的值。 As you are only able to post the selected option from the new drop down, maybe you should have a hidden input that you use to po ...
  • 从您的描述中看起来您正在运行多次注册事件处理程序的脚本。 最好的解决方案是不要这样做,即尝试只运行一次脚本。 如果您正在处理动态元素,请查看事件委派。 如果这是不可能的,另一种解决方案是在添加新处理程序之前删除任何处理程序,以便任何给定时间只有一个处理程序。 您还可以使用事件命名空间来确保不会删除其他一些处理程序 $('some-selector').off('click.duplicate').on('click.duplicate', function(){}) From your descripti ...
  • 实现的变体之一: 在每个项目工具栏上添加唯一ID(例如view-0,view-1,view-2) 它看起来像这样:

  • 单击项目2时,会向项目2 和项目1发送“单击”事件,因为项目2(发生“单击”时)是项目1的子项。事件冒泡并且父项也获得它。 因此,您单击项目2,然后运行事件处理程序。 第2项附加到目标。 然后事件冒泡到第1项,事件处理程序再次运行。 现在, 在项目2 之后附加项目1,因为(此时)项目2不再是项目1的子项目。 判决:不是错误。 您可以更改处理程序以防止: $('.item').on('click', function(e){ // Append clicked item on destiny div ...
  • 您可以创建一个小插件来检查ID并以正确的顺序插入新元素,这并不难。 像这样的东西会起作用 (function($) { $.fn.stickTo = function(elem) { return this.each(function() { var posts = $(elem).append(this).find('.thePost'); posts.sort(function(a,b) { ret ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。