【发布时间】:2020-07-11 11:08:28
【问题描述】:
如何使用 d3.js 版本 5 使海洋区域保持白色背景?我正在使用thisbl.ocks.org 页面中的内容,但版本不同。她使用的是版本 3,而我使用的是版本 5。我有两页,一页使用版本 3 here,一页使用版本 5 here。两者的 CSS 和 HTML 是相同的。唯一不同的是 D3 版本。如何使版本 5 的海洋部分看起来像版本 3 的海洋部分是白色的?谢谢。
这是我为地图设置的 css 和 js。
<style type="text/css">
svg {
background-color: white;
}
h1 {
color: rgb(115, 115, 115);
font-size: 18px;
font-family: sans-serif;
font-weight: bold;
margin: 0;
padding-bottom: 10px;
}
#container {
width: 90%;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
padding: 20px;
background-color: white;
box-shadow: 1px 1px 1px 2px rgb(217, 217, 217);
}
path:hover {
fill: rgba(157, 197, 243, 0.911);
cursor:pointer;
}
</style>
</head>
<body>
<div id="container">
</div>
<script type="text/javascript">
//Width and height
var w = 800;
var h = 600;
//Define map projection
var projection = d3.geoMercator() //utiliser une projection standard pour aplatir les pôles, voir D3 projection plugin
.center([ 23, 42 ]) //comment centrer la carte, longitude, latitude
.translate([ w/2, h/2 ]) // centrer l'image obtenue dans le svg
.scale([ w/1.5 ]); // zoom, plus la valeur est petit plus le zoom est gros
//Define path generator
var path = d3.geoPath()
.projection(projection);
//Create SVG
var svg = d3.select("#container")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load in GeoJSON data
d3.json("data/ne_50m_admin_0_countries_simplified.json").then( function(json) {
//Bind data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke", "rgba(8, 81, 156, 0.2)")
.attr("fill", "rgba(8, 81, 156, 0.6)");
});
</script>
</body>
【问题讨论】:
标签: javascript d3.js