d3 choropleth队列和填充问​​题

时间:2017-02-26 16:10:34

标签: javascript json d3.js choropleth

我有两个不同的问题,但它们可能有关:

  1. 如果我在同时加载两个json文件中的一个时调用回调函数,我无法获得d3.queue工作。如果我不使用d3.queue并且只将一个d3.json嵌套在另一个中,则会显示地图,但填充问题(如下所述)仍未解决。
  2. 无论有没有d3.queue,我都不能让每个美国州都显示它自己的填色。我相信这是因为我没有创建单独的容器"对于每个州。当我将鼠标悬停在"路径类=状态"浏览器检查器中的容器,整个地图都会突出显示,而不是应该按照个别状态。因此,所有的颜色都是相互叠加的,但最暗的色调占主导地位,只能看到一个。
  3. 这是d3.queue的版本

    
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Choropleth Map</title>
    <style>
    
    .counties {
      fill: none;
    }
    
    .states {
      fill: none;
      stroke: black;
      stroke-linejoin: round;
    }
    
    </style>
    </head>
    <body>
    <script src="https://d3js.org/d3.v3.min.js"></script>
    <script src="https://d3js.org/topojson.v1.min.js"></script>
    <script src="https://d3js.org/d3-queue.v3.min.js"></script>
    
    <script>
    var margin = {top: 10, right: 200, bottom: 30, left: 50},
        width = 960 - margin.left - margin.right,
        height = 600 - margin.top - margin.bottom;    
        
    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");    
        
    var earnings = d3.map();
    
    var path = d3.geo.path();
    
    var x = d3.scale.linear()
        .domain([1, 10])
        .rangeRound([600, 860]);
    
    var color = d3.scale.threshold()
        .domain(d3.range(2, 10))
    .range(['#f7fcfd','#e5f5f9','#ccece6','#99d8c9','#66c2a4','#41ae76','#238b45','#006d2c','#00441b']);
    
    // Load data
    d3.queue()
        .defer(d3.json, "us.json")
        .defer(d3.json, "median_earnings.json", loadjson) //removing loadjson from here brings up the map of U.S. states without any fill. Keeping it shows a blank page
        .await(ready);
    
    function loadjson (error, earnings) {
        if (error) throw error;
        earnings.forEach(function(d) {
                d.id = +d.id;
                d.median = +d.median_earnings;
        });    
    }
        
    function ready(error, us) {
      if (error) throw error;
    
        svg.append("path")
            .attr("class", "states")    
            .datum(topojson.feature(us, us.objects.states), function(a, b) { return a !== b; })
            .attr("d", path);
        
            svg.selectAll("path")
            .attr("class", "states")    
            .datum(earnings)    
            .attr("fill", function(d,i) { 
                return color(d[i].median);
                });
    }
    
            
    </script>
    </body>
    </html>
    &#13;
    &#13;
    &#13;

    我使用的us.json文件是here和median_earnings here

    这是d3.queue的版本

    &#13;
    &#13;
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Choropleth Map of College Data</title>
    <style>
    
    .counties {
      fill: none;
    }
    
    .states {
      stroke: #fff;
      stroke-linejoin: round;
    }
    
    </style>
    </head>
    <body>
    <script src="https://d3js.org/d3.v3.min.js"></script>
    <script src="https://d3js.org/topojson.v1.min.js"></script>
    <script src="https://d3js.org/d3-queue.v3.min.js"></script>
    <script>
    var margin = {top: 10, right: 200, bottom: 30, left: 50},
        width = 960 - margin.left - margin.right,
        height = 600 - margin.top - margin.bottom;    
        
    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom);
    
    var earnings = d3.map();
    
    var path = d3.geo.path();
    
    var x = d3.scale.linear()
        .domain([1, 10])
        .rangeRound([600, 860]);
    
    var color = d3.scale.threshold()
        .domain([15000, 18000, 21000, 24000, 27000, 30000, 33000])    .range(['#f7fcfd','#e5f5f9','#ccece6','#99d8c9','#66c2a4','#41ae76','#238b45','#005824']);
    
    d3.json("median_earnings.json", function loadjson(error, earnings) {
        if (error) throw error;
        earnings.forEach(function(d) {
                d.id = +d.id;
                d.median = +d.median_earnings;
                
        d3.json("us.json", function ready(error, us) {
            if (error) throw error;
        
        svg.append("path")
            .attr("class", "states")    
            .datum(topojson.feature(us, us.objects.states, function(a, b) { return a !== b; }))
            .attr("d", path)
            .style("stroke", "grey");
            
        svg.selectAll("path")
            .attr("class", "states")    
            .datum(earnings)    
            .attr("fill", function(d,i) { 
                return color(d[i].median);
                });
        });    
    })
    });
    
    </script>
    </body>
    </html>
    &#13;
    &#13;
    &#13;

1 个答案:

答案 0 :(得分:0)

这是因为您文件上的ID不匹配。