我有以下代碼,我正在嘗試製作除南極洲之外的世界地圖。無法顯示帶有JSON文件的地圖d3.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
background-color: white;
}
svg {
background-color: white;
}
</style>
</head>
<body>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 1160;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("countries.json", function(error, world) {
if (error) return console.error(error);
console.log(world);
});
svg.append("path")
.datum(topojson.feature(world, world.objects.subunits))
.attr("d", d3.geo.path().projection(d3.geo.mercator()));
});
</script>
</body>
</html>
當我嘗試打開HTML頁面沒有出現,而它給人的錯誤是:https://bost.ocks.org/mike/map/:
Failed to load resource: net::ERR_FILE_NOT_FOUND
我從邁克·博斯托克的教程,可以在這裏找到以下。 我已經嘗試了許多命令,並試圖使用它的通訊路徑來訪問文件。 這些都是我試過(使用它們的記者本地主機連接)的命令:
python -m SimpleHTTPServer
python3 -m http.server
npm install -g http-server
http-server -c-1
如果有人能告訴我什麼,我做錯了,那就太棒了!我檢查了我的代碼一千次,我真的不知道我做錯了什麼。 在此先感謝!
我試圖在Firefox中打開文件,現在它給人的錯誤是:
TypeError: world.objects is undefined
'countries.json'與index.html位於同一個目錄嗎? – MrWillihog
是的,該文件是在同一個目錄! – Andrea
'svg.append'塊必須位於'd3.json'函數內。 –