【问题标题】:D3 Choropleth Map: Colors are not appearingD3 Choropleth 地图:颜色未出现
【发布时间】:2016-11-14 15:11:53
【问题描述】:

我在使用 D3 的 Choropleth Map 时遇到问题。

我想在世界地图上显示给定年份发生的灾难,比如 2015 年。

我有一个更改灾难类型的下拉菜单。我目前正在处理“地震”。

在从下拉列表中选择“地震”之前:

选择“地震”后,它正在改变颜色,但不知何故没有根据值改变。下面是输出:

以下是我生成地图的代码,它也没有显示任何错误。

       // Load Data
        d3.csv("disaster_data.csv", function(data){
            // Set color
            color.domain([
                d3.min(data, function(d) { return +d[occurrence]; }), 
                d3.max(data, function(d) { return +d[occurrence]; })
            ]);

            // Load Map JSON
            d3.json("d3-geo/mapshaper_output.json", function(error, json) {
                if (error) throw error;
                // Create Map
                var countries = svg.selectAll(".countries")
                                       .data(json.features)
                                       .enter()
                                       .append("path");
                countries.attr("d", path)
                                   .attr("stroke", "gray");

                // Return number of occurrences for given dataset countrywise
                function get_occurrences(disaster_type, year){
                    if(disaster_type == "Earthquake"){
                        var dataEarthquake = data.filter(function(a) {return a.disaster_type == disaster_type});
                        var dataEarthquake_Year = dataEarthquake.filter(function(a) {return a.year == year});
                        for(var i = 0; i < dataEarthquake_Year.length; i++){
                            var dataCountryCode = dataEarthquake_Year[i].iso;
                            var dataOccurence = +dataEarthquake_Year[i].occurrence;
                            for (var j = 0; j < json.features.length; j++) {
                                var jsonCountryCode = json.features[j].properties.iso_a3;
                                if (dataCountryCode == jsonCountryCode) {
                                    json.features[j].properties.occurrence = dataOccurence;
                                    break;
                                }
                            }                   
                        }
                        countries.attr("d", path)
                                .attr("stroke", "gray")
                                .attr("fill", function(d) {
                            var value = d.properties[occurrence];
                                                            if (value) {
                                return color(value);
                            } else {
                                return "#aaa";
                            }
                        });
                    }
                }

                // Dropdown change event
                dropdown.on("change", change);
                var selected_year = 2015;
                var occurrence_country = 0;

                function change(){
                    if(this.value == "Earthquake"){
                        occurrence_country = get_occurrences("Earthquake", selected_year);
                    }
                }
            }); // JSON ends
            d3.select(self.frameElement).style("height", height + "px");
        }); // CSV Load ends

任何人都可以查看代码并提供帮助吗?

已编辑: 添加用于颜色的代码

var colorgrad = ['#fcfbfd','#efedf5','#dadaeb','#bcbddc','#9e9ac8','#807dba','#6a51a3','#54278f','#3f007d'];
var color = d3.scale.quantize().range(colorgrad);

【问题讨论】:

  • get_occurrences 仅在下拉列表更改时调用。这或许可以解释为什么颜色只有在您使用下拉菜单时才会发生变化。
  • 是的,但对于少数国家,它应该显示除灰色以外的任何其他颜色,例如至少来自印度、中国的数据中有一些值。它们应该是另一种颜色

标签: javascript d3.js choropleth


【解决方案1】:

代替:

countries.attr("d", path)
    .attr("stroke", "gray")
    .attr("fill", function(d) {
        var value = d.properties[occurrence];
        if (value) {
            return color(value);
        } else {
            return "#aaa";
        }
    });

试试:

d3.selectAll("path)
    .attr("stroke", "gray")
    .attr("fill", function(d) {
        var value = d.properties[occurrence];
        if (value) {
            return color(value);
        } else {
            return "#aaa";
        }
    });

【讨论】:

  • 它说“未定义不是一个对象(评估 d.properties)
  • json.features[j].properties.occurrence = dataOccurence;在这一行,JSON 中有 12 个条目。所以它不是空的......
  • 这是个问题,它必须只有 1。
  • 我的意思是说它有印度、中国等 12 个国家的数据。所以它不应该对所有国家都显示为灰色......它目前处于“其他”部分而不是阅读属性中的任何“值”。理想情况下,它应该为印度、中国和其他 10 个国家/地区显示不同的颜色...
  • if 之前写一个console.log(value),然后告诉我你得到了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-08
  • 2019-12-19
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
相关资源
最近更新 更多