简体   繁体   English

d3弧形中不寻常的黑色形状

[英]unusual black shapes in d3 choropleth

https://www.dropbox.com/s/znno4krx64zs3hm/Screenshot%202014-12-04%2015.11.10.png?dl=0 https://www.dropbox.com/s/znno4krx64zs3hm/Screenshot%202014-12-04%2015.11.10.png?dl=0

I started adapting the choropleth d3 code here . 我从这里开始修改choropleth d3代码。 Here's the code puts the choropleth into the page 这是将代码放入页面的代码

//initialize the map's dimensions
var width = 960;
var height = 500;

//define the display threshold
var color = d3.scale.threshold()
    .domain([.02, .04, .06, .08, .10])
    .range(["#f2f0f7", "#dadaeb", "#bcbddc", "#9e9ac8", "#756bb1", "#54278f"]);

var path = d3.geo.path();

//add the map image to the div
var svg = d3.select("#" + rowID + " .choropleth").append("svg")
    .attr("width", width)
    .attr("height", height);

//import data from REST interface
$.ajax({
    url: "<?php echo REST_URL; ?>",
    data: {"method": "getChoroplethData", "reportID": reportID, "mode": mode, "contents": JSON.stringify(window.reportData[reportID]['contents'])},
    success: function(response){
        response = parseJSON(response);
        var us = response['us'];
        var data = response['data'];
        var reportID = response['reportID'];

        var rateById = {};
        for(var i in data){
            rateById[data[i]['id']] = +data[i]['rate'];
        }

        svg.append("g")
            .attr("class", "counties")
            .selectAll("path")
            .data(topojson.feature(us, us.objects.counties).features)
            .enter().append("path")
            .attr("d", path)
            .style("fill", function(d) { return color(rateById[d.id]); });

        svg.append("path")
            .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a.id !== b.id; }))
            .attr("class", "states")
            .attr("d", path);

        //show the report
        $("#" + rowID + " .panel-body").slideDown();
    }
});

For the most part this works. 在大多数情况下,这是可行的。 The response from the AJAX call has 3 parts. AJAX调用的响应分为三个部分。 us is the us.json data that the choropleth example above uses and data contains the data from unemployment.tsv translated into JSON data in the format [{'id':'some id','rate':'some rate},...{'id':'some id','rate':'some rate'}] . usus.json数据上述用途和等值线示例data包含从数据unemployment.tsv翻译成JSON格式数据[{'id':'some id','rate':'some rate},...{'id':'some id','rate':'some rate'}] It's being placed in the right part of the page and all the pieces are moving correctly except that when the choropleth is drawn it looks like this . 它被放置在页面的右侧,并且所有部分都在正确地移动,除了绘制了圆弧时,它看起来像这样 I can't think of any reason for these massive black splotches to be all over the map although I don't particularly understand how some of the code works, mostly the part at the end where the data is used to color the map. 尽管我不特别了解某些代码的工作原理,但我想不出这些巨大的黑色斑点遍布整个地图的任何原因,其中大部分是数据用来为地图着色的末尾部分。 Does anyone know what could cause this kind of effect on the choropleth map? 有谁知道什么可能导致这种情况对choropleth图?

What's happening is that the path elements that represent the states are filled black by default. 发生的情况是,代表状态的path元素默认情况下用黑色填充。 These are the weird shapes that you're seeing -- most of it is obscured by the county paths , but some of them are still visible. 这些是您看到的怪异形状-大部分都被县paths遮挡了,但是其中一些仍然可见。

To prevent this from happening, copy the CSS from the example you've linked to, in particular 为了避免这种情况的发生,请复制链接示例中的CSS,尤其是

.states {
  fill: none;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM