用d3.js从geojson对象的属性中创建一个表
Creating a table with d3.js from the properties in a geojson object
我正在尝试创建一个包含内联geojson对象中列出的每个功能的属性的表。
下面的代码几乎得到了我想要的,但它只显示了第一个条目的属性。我应该如何修改它,以显示所有条目的属性?
var thedatatable = d3.select("#propertiestable")
.append("table")
.attr("class", "datatable");
var header = thedatatable.selectAll("th")
.data(d3.keys(datapointsjson.features[0].properties))
.enter()
.append("th")
.text(function (d) {
return d
});
var tr = thedatatable.selectAll("tr")
.data(d3.values(datapointsjson.features[0].properties))
.enter()
.append("tr");
var td = thedatatable.selectAll("td")
.data(d3.values(datapointsjson.features[0].properties))
.enter()
.append("td")
.text(function (d) {
return d
});
我有理由确信这是"td"位,我弄错了,但我所尝试的只是显示第一个条目。
这是我正在工作的geojson,它需要内联在同一个文件。
var datapointsjson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
136.33, -31.5
]
},
"properties": {
"regno": "R123456",
"taxon": "genus1 species1",
"sitecode": "",
"nearestnamedplace": "FREELING ISLAND",
"preciselocation": "south coast",
"collection": "Herpetology"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
137.07, -36.23
]
},
"properties": {
"regno": "R654185",
"taxon": "genus2 species2",
"sitecode": "",
"nearestnamedplace": "Neptune Island",
"preciselocation": "Middle of island",
"collection": "Herpetology"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
142.0358, -38.7719
]
},
"properties": {
"regno": "R6528445",
"taxon": "genus3 species3",
"sitecode": "TT02",
"nearestnamedplace": "Woolongong",
"preciselocation": "5 km N",
"collection": "Herpetology"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
137.6914, -32.2789
]
},
"properties": {
"regno": "R654987",
"taxon": "genus4 species4",
"sitecode": "IL0601",
"nearestnamedplace": "Ballarat",
"preciselocation": "5.3 KM E",
"collection": "Herpetology"
}
}]
};
我做了一个jsfiddle来显示结果
任何建议都将不胜感激。谢谢你。
我已经把你的手放在这里了,其中很多都是基于肖恩·艾伦的精彩答案。
现在,您需要提供一个数组,以便d3可以遍历它们(在本例中为4次)
var tr = thedatatable.selectAll("tr")
.data(d3.values(datapointsjson.features))
.enter()
.append("tr");
然后你需要从json中提取你需要的信息:
var td = tr.selectAll("td")
.data(function(row) {
return headers.map(function(d) { console.log(row.properties)
return {column: d, value: row.properties[d]};
});
})
.enter()
.append("td")
.text(function (d) {
return d.value;
});
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 从javascript创建一个列表
- Kendo:我该如何在树视图中创建一个递归的hieiarchy
- 从html创建一个指令,该指令按类名应用函数
- 如何为jQuery屏蔽输入插件创建一个允许字母数字、空格和重音字符的掩码
- 我想在AngularJS应用程序中创建一个输入数字框,用户不应该在该框上键入十进制数字.(一个整数输入框)
- 是否可以使用JavaScript/AAJAX在客户端创建一个文件
- 如何创建一个“;表单弹出框“;在chrome中右键单击时位于突出显示的单词上方
- 使用AngularJS Directive WHITOUT$scope创建一个动态html元素
- 创建一个循环来简化HTML和CSS代码
- 在javascript中的xml中创建一个链接
- 尝试创建一个具有z-index的堆叠菜单'使用HTML5/CSS3/JS
- 创建一个方法,通过一个窗口进行迭代并获取Titanium中的所有控件
- 用javascript创建一个看起来正常分布的模式
- 如何创建一个方法来验证数组的范围
- 在量角器中创建一个.txt错误输出文件是手动的,而不是控制台错误
- 动态创建一个javascript/jquery多级数组
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- 为网站表单创建一个专业的日历
- 如何创建一个具有固定左右列和水平滚动的表