d3.js

时间:2015-10-12 08:49:29

标签: javascript d3.js polymaps

我正在尝试使用d3.js在绘制的点之间绘制线条。

示例geojson(FeatureCollection of 3 LineString): https://www.dropbox.com/s/hgkdvbnrgmb6kak/test.geojson?dl=0

完整现有代码: https://www.dropbox.com/s/49820rs561vneti/test.html?dl=0

Code Chunk我遇到问题:

lines.append("g").selectAll("path")
  .data(d3.entries(data.features)).enter()
  .append("svg:path")
  .attr("d", path)

圆圈正在出现但不是将它们连接在一起的线条。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

错误1:

在你的topojson里面。

{
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        103.79971,
                        1.3013115
                    ],
                    [
                        103.8071998,
                        1.2932586
                    ]
                ]
            },
            "type": "Feature",//this is wrong
            "properties": {} 
        }

应该是:

{
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        103.79971,
                        1.3013115
                    ],
                    [
                        103.8071998,
                        1.2932586
                    ]
                ]
            },
            "properties": {}
        }

错误2:

  lines.append("g").selectAll("path")
    .data(d3.entries(data.features)).enter()
      .append("svg:path")
      .attr("d", path)
      .style("fill", "none")
      .style("stroke-width", "2px")

你不能像这样创建一条线,为了创建线你必须使用一个图层并像这样添加它的特征(注意这些特征来自你的test.geojson):

d3.json("test.geojson", function(data) {
  layer1.features(data.features);//adding the features to the layer
  map.add(layer1); 

完整的工作代码here

相关问题