在Topojson名称中选择单词的国家的标准颜色

问题描述 投票:0回答:1

我已经在D3中制作了地图,并且之前已经有条件地在id和属性上分配了颜色。在这种情况下,我试图根据名称中是否存在特定单词来有条件地进行着色。例如:将所有名称中带有“ United”字样的国家/地区涂成蓝色,所有其他国家/地区涂成粉红色。在这种情况下,它将“美国”,“英国”和“阿拉伯联合酋长国”涂成蓝色。我试过indexOf和some和str.includes,但结果是:Uncaught TypeError:无法读取未定义的属性“ includes”。

这里是代码:https://jsfiddle.net/databayou/retk2pu6/7/

  svg.selectAll(".country")
  .data(topojson.feature(nations, nations.objects.countries).features)
  .enter().append("path")
  .attr("class", function(d) { return "country " + d.name; })
  .attr("fill",function(d){ 
          var str = d.name;
          if (str.includes("United")) { return "blue"}
                            else {return "pink";
                           }
                       })
  .attr("d", path);
d3.js include conditional-statements indexof topojson
1个回答
0
投票

var str = d.name;更改为var str = d.properties.name;

如果您console.log d,您将看到nameproperties内部。

© www.soinside.com 2019 - 2024. All rights reserved.