【问题标题】:D3 map - filter on specific propertiesD3 地图 - 过滤特定属性
【发布时间】:2016-04-24 05:03:14
【问题描述】:

我正在使用 D3 构建一个带有国家名称文本标签的地图。我的问题是我生成了太多国家标签,并且想手动选择要显示的国家/地区。

最好的方法是什么?我可以使用

找到我的国家/地区名称
d.properties.name

也许我可以使用 d3.filter() 但我不知道该怎么写

svg.selectAll(".place-label")
.data(topojson.feature(germany, germany.objects.populated).features)
.enter().append("text")
.attr("class", "place-label")
.attr("transform", function(d) { return "translate(" +  projection(d.geometry.coordinates) + ")"; })
.attr("dy", ".35em")
.text(function(d) { return d.properties.name; });

【问题讨论】:

  • 是否要过滤标签以使特定标签不会显示在地图上?
  • 知道这会很有用,但这次根据几个国家/地区名称进行过滤对我有用

标签: d3.js


【解决方案1】:

您可以像这样使用过滤器功能:

.text(function(d) { return d.properties.name; })
.filter(function(labels) {
                    if(labels.properties.name == "some country name"){
                        //do stuff here
                    }
                    //or return the specific label and continue styling it afterwards
                    return labels.properties.name == "germany";
                })
.style("opacity",0);// make it dissappear

你也可以把它放在像点击这样的事件中。

【讨论】:

  • 我已经尝试过这个选项,它可以工作,但由于某些奇怪的原因,fitSize(使用相同的过滤数据)会使地图消失。在我的非 js 头脑中,使用过滤后的数据使用 fitSize 重新拟合地图是有道理的。为什么?
猜你喜欢
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
相关资源
最近更新 更多