在 d3.js 中的点之间画线
Drawing lines between points in d3.js
我正在尝试使用 d3.js 在标绘点之间画线。
示例 geojson(3 LineString 的 FeatureCollection):
https://www.dropbox.com/s/hgkdvbnrgmb6kak/test.geojson?dl=0
完整的现有代码:
https://www.dropbox.com/s/49820rs561vneti/test.html?dl=0
我遇到问题的代码块:
lines.append("g").selectAll("path")
.data(d3.entries(data.features)).enter()
.append("svg:path")
.attr("d", path)
出现了圆圈,但没有出现将它们连接在一起的线。
任何帮助将不胜感激!
错误一:
在你的 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.
我正在尝试使用 d3.js 在标绘点之间画线。
示例 geojson(3 LineString 的 FeatureCollection): https://www.dropbox.com/s/hgkdvbnrgmb6kak/test.geojson?dl=0
完整的现有代码: https://www.dropbox.com/s/49820rs561vneti/test.html?dl=0
我遇到问题的代码块:
lines.append("g").selectAll("path")
.data(d3.entries(data.features)).enter()
.append("svg:path")
.attr("d", path)
出现了圆圈,但没有出现将它们连接在一起的线。
任何帮助将不胜感激!
错误一:
在你的 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.