Plotting Points on a Map with d3.js

二次信任 提交于 2019-12-04 19:23:01

Did you get any output with your code, as you've got a couple of syntax errors, such as:

  1. proj vs projection (in the calculation of the circle x and y positions)
  2. svg vs india (the variable svg doesn't exist when you try to create the circles)

After correcting those syntax errors, your code still doesn't work as expected, with nothing visible in the browser, probably because of:

  1. The projection needs to be rotated to bring the map into view
  2. The projection needs a scale and translate that matches the map and svg size
  3. The circles you draw are going to be really small due to the multiplication by 0.0004

Here are some changes that may get you onto the right track:

<!DOCTYPE html>
<html lang="en">
    <head>
    <!-- India State Map  -->
    <title>India Map</title>
    <style type="text/css">
        svg {
            background: #fff;
        }

        #india {
            fill: #95a5a6;
            opacity: .8;
            stroke: white ;
            stroke-width: 1.2;
        }
    </style>
</head>

<body>
    <div id="chart"></div>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.4.13/d3.min.js"></script>
    <script type="text/javascript">
        var w = 600;
        var h = 600;
        var bounds = [[68,38], [97, 8]];  // rough extents of India
        var proj = d3.geo.mercator()
                         .scale(800)
                         .translate([w/2,h/2])
                         .rotate([(bounds[0][0] + bounds[1][0]) / -2, 
                                  (bounds[0][1] + bounds[1][1]) / -2]); // rotate the project to bring India into view.

        var path = d3.geo.path().projection(proj);

        var map = d3.select("#chart").append("svg:svg")
                    .attr("width", w)
                    .attr("height", h);

        var india = map.append("svg:g")
                       .attr("id", "india");

        d3.json("json/indianstates.json", function(json) {
            india.selectAll("path")
                 .data(json.features)
               .enter().append("path")
                 .attr("d", path);
        });

        d3.csv("csv/water.csv", function(csv) {
            india.selectAll("circle")
                 .data(csv)
               .enter()
                 .append("circle")
                 .attr("cx", function (d) {
                     return proj([d.lon, d.lat])[0];
                 })
                 .attr("cy", function (d) {
                     return proj([d.lon, d.lat])[1];
                 })
                 .attr("r", function (d) {
                     return Math.sqrt(parseInt(d.complaints));
                 })
                 .style("fill", "yellow")
                 .style("opacity", 0.75);
        });

    </script>
</body>

You can see where I've rotated the projection based upon the geographical extents of India, set the scale and translate to appropriate values based on the size of the SVG and the size of the country. I've also set the radius of the circles to more appropriate values.

Note: I used an arbitrary json/indianstates.json file that I googled for, hopefully it's the same as, or close enough to your file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!