javascript - D3:它没有显示美国地图
问题描述
我的控制台中没有错误,并且我的 xampp 服务器已启动。我在浏览器中使用http://localhost/d3/project/graph1.html调用代码,这应该是正确的路径。但是页面只是白色的,它没有向我显示美国的地图。这些是我放在头上的脚本
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="https://d3js.org/d3-queue.v2.min.js"></script>
这是我的代码,忽略注释的东西:
<head>
.state{
fill:none;
stroke: #333;
stroke-width: 1.0;
}
.county{
fill: #e3e3e3;
stroke: #fff;
stroke-width: 0.5;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
var margin = {top: 0, left: 0, right: 0, bottom: 0},
height = 400 - margin.top - margin.bottom,
width = 800 - margin.left - margin.right;
var svg = d3.select("#map")
.append("svg")
.attr("height", height + margin.top + margin.bottom)
.attr("width", width + margin.left + margin.right)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.queue()
.defer(d3.json, "us.json")
//.defer(d3.csv, "final_cleaned_wildlife_impacts.csv")
.await(ready);
var projection = d3.geoAlbersUsa()
.translate([width/2, height/2])
.scale(800);
var path = d3.geoPath()
.projection(projection);
function ready(error, data/*, airport*/){
var counties = topojson.feature(data, data.objectes.counties).features;
svg.selectAll(".county")
.data(counties)
.enter()
.append("path")
.attr("class", "county")
.attr("d", path);
var states = topojson.feature(data, data.objectes.states).features;
svg.selectAll(".state")
.data(states)
.enter()
.append("path")
.attr("class", "state")
.attr("d", path);
/*
svg.selectAll(".airport")
.data(airports)
.enter().append("circle")
.attr("class", "airport")
.attr("r", 2)
.attr("cx", function(d){
var coords = projection([d.long, d.lat])
return coords[0]
})
.attr("cy", function(d){
var coords = projection([d.long, d.lat])
return coords[1]
})
.attr("opaciy", "0.25");
*/
}
</script>
</body>
解决方案
推荐阅读
- sql - 编写具有相同条件但不同列并使用聚合函数的查询
- swift - 如何在 swift 上使用 hsplitview
- react-native - 在抽屉导航器中制作自定义抽屉时“超出最大更新深度”
- node.js - Sequelize 内连接无法获得正确的值
- neo4j - 在一个查询中计算关系的最高和最低出现 [Neo4j]
- python - 如何更新 AlexNet 以使用 FCN?对上采样感到困惑
- javascript - 为什么结果不是我所期望的?
- css - Material UI Drawer响应高度?
- python - PyTorch softmax 返回
- javascript - 在 REACT/JSX 中处理嵌套数组