D3.js是一個JavaScript庫,用於在Web瀏覽器中生成動態的交互式數據可視化。它利用可伸縮矢量圖形,HTML5和級聯樣式表標準。 d3.js中的geoMercator()函數用於繪製球形墨卡托投影。
用法:
d3.geoMercator()
參數:此方法不接受任何參數。
返回:此方法根據給定的json數據創建Mercator投影。
範例1:下麵的示例繪製中心為(0,0)且旋轉為0的世界的Mercator投影。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width,
initial-scale=1.0"
/>
</head>
<body>
<div style="width:700px; height:500px;">
<center>
<h3 style="color:black;"></h3>
</center>
<svg width="600"
height="450"></svg>
</div>
<script src="https://d3js.org/d3.v4.js"></script>
<script src=
"https://d3js.org/d3-geo-projection.v2.min.js">
</script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
// Mercator projection
// Center(0, 0) with 0 rotation
var gfg = d3
.geoMercator()
.scale(width / 2.5 / Math.PI)
.rotate([0, 0])
.center([0, 0])
.translate([width / 2, height / 2]);
// Loading the json data
// Used json file stored at
/*https://raw.githubusercontent.com/janasayantan
/datageojson/master/world.json*/
d3.json(
"https://raw.githubusercontent.com/janasayantan/datageojson/master/world.json",
function (data) {
// Drawing the map
svg.append("g").selectAll(
"path").data(data.features).enter().append(
"path").attr("fill", "black").attr(
"d", d3.geoPath().projection(gfg)).style(
"stroke", "#ffff");
});
</script>
</body>
</html>
輸出:

墨卡托投影世界,無旋轉且以(0,0)為中心
範例2:以下示例在更改中心和旋轉後繪製墨卡托世界投影。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width,
initial-scale=1.0"
/>
</head>
<body>
<div style="width:700px; height:600px;">
<center>
<h3 style="color:black;"></h3>
</center>
<svg width="500" height="450"></svg>
</div>
<script src="https://d3js.org/d3.v4.js"></script>
<script src=
"https://d3js.org/d3-geo-projection.v2.min.js">
</script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
// Mercator projection
// Center(20, 20) and 90 degree rotation w.r.t Y axis
var gfg = d3
.geoMercator()
.scale(width / 2.5 / Math.PI)
.rotate([90, 0])
.center([20, 20])
.translate([width / 2, height / 2]);
// Loading the json data
// Used json file stored at
/*https://raw.githubusercontent.com/janasayantan
/datageojson/master/world.json*/
d3.json(
"https://raw.githubusercontent.com/janasayantan/datageojson/master/world.json",
function (data) {
// Draw the map
svg.append("g").selectAll(
"path").data(data.features).enter().append(
"path").attr("fill", "Turquoise").attr(
"d", d3.geoPath().projection(gfg)).style(
"stroke", "#ffff");
});
</script>
</body>
</html>
輸出:

墨卡托投影,Y軸旋轉90度,並以(20,20)為中心
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
- PHP Ds\Deque pop()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 D3.js geoMercator() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。