【问题标题】:How to draw the interactions between source and destination countries using ip address?如何使用IP地址绘制源国和目的地国之间的交互?
【发布时间】:2017-09-17 15:47:44
【问题描述】:

我正在尝试创建一个可点击的世界地图,每个国家都有一个标记。当点击源国家的标记时,应以聚合方式显示与其他国家的交互。我能够绘制世界地图,但我不确定如何使用 CSV 文件中给出的 IP 地址来绘制两个国家之间的交互。我试图寻找解决方案,但徒劳无功。我是 d3.js 的新手。如果有人可以对此有所了解,那就太好了。

一个建议会很有帮助。关于如何使用任何相关库(如果需要)或任何其他建议继续前进的建议将与任何直接答案一样有用。谢谢你。 这是plnkr

这是脚本

var app = angular.module("chartApp", []);
app.controller("SalesController", function($scope, $http) {
$http.get("countries.csv").then(function(response) {
    $scope.salesData = response;
    });
});

app.directive("linearChart", function($window) {
return {
    restrict: "EA",
    // template: "<svg width='1024' height='728'></svg>",
    link: function(scope, elem, attrs) {
        // canvas resolution
        var margin = { top: 50, left: 50, right: 50, bottom: 50 },
            height = 900 - margin.top - margin.bottom,
            width = 1400 - margin.left - margin.right;

        // defines "svg" as data type and "make canvas" command
        var svg = d3.select("#map")
            .append("svg")
            .attr("height", height + margin.top + margin.bottom)
            .attr("width", width + margin.left + margin.right)
            .append("g")
            .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

        // var width = 1500,
        //     height = 900;

        d3.queue()
            .defer(d3.json, "https://unpkg.com/world-atlas@1.1.4/world/110m.json")
            .defer(d3.csv, "countries.csv")
            .await(ready);

        // projection-settings for mercator    
        var projection = d3.geoMercator()
            // .translate([width / 2, height / 2])
            // where to center the map in degrees
            .center([-30, 60])
            // // zoomlevel
            .scale(200);
        // // map-rotation
        // .rotate([0, 0]);

        /* Create a Path using projection */

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


        function ready(error, data, country_name) {
            console.log(data);


            var countries = topojson.feature(data, data.objects.countries).features;
            console.log(countries);
            console.log(country_name);

            /* Add a path for each country
                 Shapes -> Path
            */

            svg.selectAll(".country")
                .data(countries)
                .enter().append("path")
                .attr("class", "country")
                .attr("d", path)
                .on('mouseover', function(d) {
                    d3.select(this).classed("selected", true);
                })
                .on('mouseout', function(d) {
                    d3.select(this).classed("selected", false);
                });

            svg.selectAll(".country-circles")
                .data(country_name)
                .enter().append("circle")
                .attr("class", "circle")
                .attr("r", 2)
                .attr("cx", function(d) {
                    var coords = projection([d.longitude, d.latitude]);
                    return coords[0];
                })
                .attr("cy", function(d) {
                    var coords = projection([d.longitude, d.latitude]);
                    return coords[1];
                });

            svg.selectAll(".countries")
                .data(country_name)
                .enter().append("marker")
                .attr("class", "countries")
                .attr("x", function(d) {
                    var coords = projection([d.longitude, d.latitude]);
                    return coords[0];
                })
                .attr("y", function(d) {
                    var coords = projection([d.longitude, d.latitude]);
                    return coords[1];
                })

                // .text(function(d) {
                //     return d.country_name;
                // })
                .attr("dx", 5)
                .attr("dy", 2);
           }
         }
      };
  });

【问题讨论】:

  • 来自 $http 请求的数据是 response.data。如果您使用console.log 查看response 对象,这将是显而易见的。阅读How to debug small programs
  • 嘿,谢谢。但是在地图显示后使用了$http请求数据,但地图一开始没有在这里显示
  • 你从哪里得到这个代码?每一条线似乎都断了。该指令没有被实例化,因为它的名称没有被正确规范化。 postLink 代码查找带有id="map" 的元素,但没有这样的元素。名单还在继续。
  • 你有国家到IP地址的映射吗?
  • 否,源国IP地址到目的国IP地址的映射。

标签: javascript angularjs d3.js


【解决方案1】:

为您指明正确的方向:

您有一个包含 IP 地址的 CSV 文件,但在我看来,您需要国家/地区的经度和纬度。

您可以尝试批量转换这些here,或者您可以使用this 之类的API 来获得具有相应IP 地址的正确经纬度。

为了展示这些点之间的相互作用,我认为这个例子here 最有助于让你上路。

【讨论】:

  • 我也有 lat 和 long 值。你可以在我提供的 plnkr 中看到
  • 啊哈!他们就在那里,隐藏在众目睽睽之下!好吧,我希望你能根据给定的例子解决你的问题
猜你喜欢
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2014-03-19
  • 2012-07-09
  • 2014-03-23
  • 1970-01-01
  • 2020-05-20
  • 2010-10-09
相关资源
最近更新 更多