将要素类型添加到路径标记中作为D3中的属性

时间:2018-04-16 13:48:56

标签: d3.js

在此页面GeoJson map of Colombia中,它使用D3和geojson文件来显示地图。

以下功能:

d3.json('colombia.geo.json', function(error, mapData) {
var features = mapData.features;

// Update color scale domain based on data
color.domain([0, d3.max(features, nameLength)]);

// Draw each province as a path
mapLayer.selectAll('path')
  .data(features)
  .enter().append('path')
  .attr('d', path)
  .attr('vector-effect', 'non-scaling-stroke')
  .style('fill', fillFn)
  .on('mouseover', mouseover)
  .on('mouseout', mouseout)
  .on('click', clicked);
});

我想将每个要素的类型(Polygon)添加为路径的属性。

.attr('attribute', mapData.features.????)

如何从' mapData.features'?

获取类型

1 个答案:

答案 0 :(得分:1)

您正在寻找的类型是几何对象的一部分。

func splitByLength(_ length: Int, seperator: String) -> [String] {
    var result = [String]()
    var collectedWords = [String]()
    collectedWords.reserveCapacity(length)
    var count = 0
    let words = self.components(separatedBy: " ")

    for word in words {
        count += word.count + 1 //add 1 to include space
        if (count > length) {
            // Reached the desired length

            result.append(collectedWords.map { String($0) }.joined(separator: seperator) )
            collectedWords.removeAll(keepingCapacity: true)

            count = word.count
            collectedWords.append(word)
        } else {
            collectedWords.append(word)
        }
    }

    // Append the remainder
    if !collectedWords.isEmpty {
        result.append(collectedWords.map { String($0) }.joined(separator: seperator))
    }

    return result
}

会做到这一点。