javascript - 如何通过名称(属性值)获取topojson的几何形状?

标签 javascript topojson

我是 TopoJson 新手,我有一些数据看起来像这样......

{"type":"Polygon","properties":{"name":"Arkansas"},"arcs":[[0,1,2,3,4,5]]}

我想说的是输出阿肯色州,所以我想出了以下内容(我正在使用 underscore.js)...

var collection = topojson.feature(us, us.objects.subunits).features;
  var final = [];
  _.forEach(collection, function(item){
    if(item.properties.name == "Arkansas"){
      final.push(item);
    }
  });
  svg.selectAll(".subunit")
     .data(final)
     .enter()
     .append("path")
     .attr("class", function(d) { return "subunit " + d.id; })
     .attr("d", path);

这很好用,但是没有更简单的方法吗?我可以做类似 us.objects.subunits["Arkansas"] 的事情吗?

最佳答案

(根据我的手机和内存,尝试以下操作)

通常,要走的路是这样的:

var final = topojson.feature(us, us.objects.subunits).features;
  svg.selectAll(".subunit")
      .data(final)
    .enter()
      .append("path")
      .attr("class", function(d) { return "subunit " + d.id; })
      .attr("d", function(d){ if(d.properties.name == "Arkansas"){ return d }  });

过滤直接在.attr('d', function(d){…})内进行。如果不起作用,请尝试 .attr('d', function(d, path){…}) 并返回路径。

关于javascript - 如何通过名称(属性值)获取topojson的几何形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28911180/

相关文章:

javascript - Firebase 简单查询给出错误 : "Converting circular structure to JSON at JSON.stringify"

javascript - 从父级溢出 :scroll 中弹出子级

javascript - 使用 props 设置图片 src

javascript - d3.geoPath() 与 d3.geo.path()

json - 将数据 (.csv/json) 与 topojson 相结合

javascript - 有什么方法可以优化这个可能呈现数百万个组件的 React 应用程序

javascript - 在 jquery 中单击时更改类名

angular - 属性 'features' 在类型 'Feature<Point, { [name: string]: any; }>' 上不存在

svg - 在 d3.js 地理 map 中如何在每个国家/地区创建一个圆圈?

d3.js - D3js-Topojson : how to move from pixelized to Bézier curves?