gpt4 book ai didi

javascript - 错误 : attribute d: Expected number, "MNaN,NaNLNaN,NaNL…"。在 d3.js 中

转载 作者:行者123 更新时间:2023-12-05 07:44:15 25 4
gpt4 key购买 nike

我正在尝试将我的 map 置于 d3.js 中。我找到了这段代码并做了一些改动。我做不到。我收到这个错误。我该如何解决它。我是 JavaScript 的新手。

这是我的代码。

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Set a style for our worldshape-data -->
<style>
path {
stroke: red;
stroke-width: 0.5px;
fill: steelblue;
}
</style>

<body>

<!-- implementation of the hosted D3- and TopoJson js-libraries -->
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v0.min.js"></script>

<!-- map creation -->
<script>
// canvas resolution
var width = 1000,
height = 600;

// defines "svg" as data type and "make canvas" command
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);


// shorten the svg.append command
var g = svg.append("g");

// load data and display the map on the canvas with country geometries
d3.json("turkey.json", function(error, topology) {

var projection = d3.geo.mercator().scale(1).translate([0, 0]).precision(0);
var path = d3.geo.path().projection(projection);
var bounds = path.bounds(topology);


var scale = .95 / Math.max((bounds[1][0] - bounds[0][0]) / width,
(bounds[1][1] - bounds[0][1]) / height);
var transl = [(width - scale * (bounds[1][0] + bounds[0][0])) / 2,
(height - scale * (bounds[1][1] + bounds[0][1])) / 2
];
projection.scale(scale).translate(transl);

g.selectAll("path")
.data(topojson.object(topology, topology.objects.turkeytopo)
.geometries)
.enter()
.append("path")
.attr("d", path)
});



// zoom and pan functionality
var zoom = d3.behavior.zoom()
.on("zoom", function() {
g.attr("transform", "translate(" +
d3.event.translate.join(",") + ")scale(" + d3.event.scale + ")");
g.selectAll("path")
.attr("d", path.projection(projection));
});

svg.call(zoom)
</script>
</body>

</html>

这是我的 json 国家/地区数据的一部分。

{
"type": "Topology",
"objects": {
"turkeytopo": {
"type": "GeometryCollection",
"bbox": [25.66652800000014,
35.820178,
44.83384,
42.106301
],
"geometries": [{
"type": "MultiPolygon",
"arcs": [
[
[0]
],
[
[1]
],
[
[2]
],
[
[3]
],
[
[4]
],
[
[5]
],
[
[6]
],
[
[7]
],
[
[8]
],
[
[9]
],
[
[10]
],
[
[11]
]
]
}]
}
},
"arcs": [
[
[1726,
8274
],
[2, -33],
[-22,
0
],
[-7, -21],
[-43, -47],
[-16, -4],
[-12,
25
],
[-17,
13
],
[-36, -25],
[-33, -6],
[-11,
13
],
[-1,
66
],
[-17,
14
],
[-12, -52],
[-58,
81
],
[-28,
24
],
[-62,
16
],
[-14,
19
]
]
],
"transform": {
"scale": [0.001916922892289215,
0.000628675167516752
],
"translate": [25.66652800000014,
35.820178
]
}
}

最佳答案

您没有正确使用 path.bounds。来自 API 文档 path.bounds

Returns the projected planar bounding box (typically in pixels) for the specified GeoJSON object. (v4)

These components use the GeoJSON format—a standard way of representing geographic features in JavaScript. (v3)

您正在向 path.bounds 传递一个 topojson 对象,而是尝试:

var bounds = path.bounds(topojson.feature(topology, topology.objects.turkeytopo));

您也可以始终使用居中坐标,如拓扑中的 bbox 属性所示。投影看起来像:

var projection = d3.geo.mercator()
.scale(2000)
.center([34.5,38.5])
.translate([width/2,height/2]);

比例取决于 svg 大小。

关于javascript - 错误 : <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…"。在 d3.js 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43018959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com