【发布时间】:2020-07-22 13:10:29
【问题描述】:
我正在尝试使用 d3.js 和 vue js 向我的 web 应用程序显示平面图。数据来自 .json 文件,采用 geojson 格式。我从thisgeojson 网站生成了一些测试数据。我在想我可以使用 d3.js 路径函数将每个不同的对象绘制到 svg 元素。这是做这件事的正确方法吗?我查看了this 教程,以使用 geojson 数据制作美国地图。我认为如果您输入不同的数据,这将是正确的方法。然而,我的程序向 webapp 吐出随机矩形。我认为这可能是因为我正在使用的投影,但我不确定。我以前没有像这样使用过 d3.js,所以它对我来说是全新的。我在下面包含了我的代码。非常感谢任何帮助,我愿意使用不同的 java 脚本库,但首选 d3js。
createFloormap(){
var svg = d3.select("svg")
var width = +svg.attr("width")
var height = +svg.attr("height")
var path = d3.geoPath().projection(d3.geoAlbersUsa().scale(500))
//var path = d3.geoPath()
//var path = d3.geoPath().projection(d3.geoMercator().scale(100))
var x = d3.scaleLinear()
.domain([1, 10])
.rangeRound([600, 860]);
var color = d3.scaleThreshold()
.domain(d3.range(2, 10))
.range(d3.schemeBlues[9]);
var promises = [
d3.json("../static/data.json")
//d3.json("https://raw.githubusercontent.com/adamjanes/udemy-d3/master/08/8.04/data/us-map.json")
]
Promise.all(promises).then(function(data){
ready(data[0]);
})
function ready(us) {
console.log(us)
svg.append("g")
.selectAll("path")
.data(us.features)
//.data(topojson.feature(us, us.objects.states).features)
.enter()
.append("path")
.attr("fill", "grey")
.attr("d", path)
.attr("stroke", "#fff")
.attr("stroke-width", .2)
}
}
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-144.140625,
45.583289756006316
],
[
-65.390625,
45.583289756006316
],
[
-65.390625,
69.28725695167886
],
[
-144.140625,
69.28725695167886
],
[
-144.140625,
45.583289756006316
]
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
9.4921875,
39.36827914916014
],
[
159.609375,
39.36827914916014
],
[
159.609375,
70.02058730174062
],
[
9.4921875,
70.02058730174062
],
[
9.4921875,
39.36827914916014
]
]
]
}
}
]
}
【问题讨论】:
-
你能分享你正在使用的特定文件吗?我不确定它是否是注释掉的文件 - 将有助于提供明确的答案。
-
@AndrewReid 是的,我刚刚做了
标签: javascript d3.js