一、基本概念
可視化軌跡圖是一種呈現移動路徑或時間序列信息的數據可視化形式。它可以將移動物體、人員或者其他實體的路徑或移動歷史用曲線或者點進行可視化呈現。最早的應用是在氣象學領域中,用來呈現風向風速的移動歷史。目前,可視化軌跡圖已經被廣泛應用於交通運輸、物流、地圖應用、數據分析等領域。
基本思路是在二維甚至三維平面中繪製移動物體的路徑信息,縱軸表示時間進度,橫軸表示空間位置,繪製形式包括折現等平面圖形和點等。圖中的顏色、寬度、形狀等信息可代表其它意義。在根據需求詳細過濾後,可以反映數據分析的結果,對例如交通擁堵、人員遷移等行為模式進行分析和預測。
開發人員可以使用多種開源可視化軌跡圖庫,例如D3.js和ECharts等。這些庫使用JavaScript編寫,對於初學者而言需要一定的學習成本,最好可以進行完整範例練習再進行實際應用開發。
二、數據來源與格式
可視化軌跡圖的數據來源多種多樣。例如,GPS軌跡信息(經緯度坐標)、手機應用中的用戶行為軌跡、網站伺服器中的用戶訪問記錄都可以用於繪製可視化軌跡圖。數據格式通常是JSON或CSV格式的一些數據欄位,其中數據項包括經緯度、時間戳等。開發者可以根據需求對數據欄位進行自定義。一般地,數據可視化庫會提供專門的數據格式標準和API,這些信息可以在對應的官方網站上查閱到。
三、繪製可視化軌跡圖
以下為繪製D3.js可視化軌跡圖的代碼示例:
// 定義SVG容器
var svg = d3.select("body").append("svg").attr("width", 500).attr("height", 500);
// 讀取數據並預處理
d3.csv("data.csv", function(data) {
var coordinates = data.map(function(d) { return [+d.lon, +d.lat]; });
var times = data.map(function(d) { return new Date(d.time); });
// 定義投影
var projection = d3.geoMercator().fitExtent([[0, 0], [500, 500]], { type: "LineString", coordinates: coordinates });
var path = d3.geoPath().projection(projection);
// 繪製路徑
svg.append("path").datum({ type: "LineString", coordinates: coordinates }).attr("d", path).style("stroke", "red").style("stroke-width", 2);
// 繪製時間刻度
var ticks = d3.timeMinute.every(10).range(times[0], times[times.length - 1]);
var tickLabels = svg.append('g').selectAll('.tick').data(ticks).enter().append('g').attr('class', 'tick').attr('transform', function(d) {
var p = projection([coordinates[0][0], coordinates[0][1]]);
return 'translate(' + p[0] + ', ' + p[1] + ')';
});
tickLabels.append('text').attr('transform', function(d) { return 'rotate(' + (projection([coordinates[Math.round((d - times[0]) / (times[times.length - 1] - times[0]) * (coordinates.length - 1))][0], coordinates[Math.round((d - times[0]) / (times[times.length - 1] - times[0]) * (coordinates.length - 1))][1]])[1] > 250 ? 90 : -90) + ')'; }).attr('x', function(d) { return projection([coordinates[Math.round((d - times[0]) / (times[times.length - 1] - times[0]) * (coordinates.length - 1))][0], coordinates[Math.round((d - times[0]) / (times[times.length - 1] - times[0]) * (coordinates.length - 1))][1]])[1] > 250 ? -5 : 5; }).attr('y', 4).attr('text-anchor', function(d) { return projection([coordinates[Math.round((d - times[0]) / (times[times.length - 1] - times[0]) * (coordinates.length - 1))][0], coordinates[Math.round((d - times[0]) / (times[times.length - 1] - times[0]) * (coordinates.length - 1))][1]])[1] > 250 ? 'end' : 'start'; }).text(function(d) { return d.toLocaleString(); });
});
以上代碼中,首先使用D3.js創建SVG容器,在該容器之上繪製軌跡路徑和時間刻度。示例中使用了D3.csv()函數讀入一份CSV格式的數據文件,然後將其中的經緯度及時間轉換為對應的數組變數,並使用d3.geoMercator()函數定義了投影方式。接下來,使用D3.js的path生成器創建路徑,並最終通過SVG的path元素展示路徑。同時,在時間軸上,我們使用D3.js的時間相關函數和元素繪製了刻度,從而在時間上展現軌跡圖信息。
四、可視化軌跡圖的應用
可視化軌跡圖在許多領域中有著廣泛的應用。例如:
- 交通和物流領域:軌跡圖能夠展示路上交通情況,提供合適的路線規劃和實時交通預測。它也可以用於之後的物流和包裹追蹤。
- 天氣和自然災害領域:軌跡圖能夠追蹤並預報風向、風速和地震等自然災害的移動路線。
- 安防領域:軌跡圖能夠跟蹤恐怖分子、犯罪嫌疑人以及其他威脅的人員。
- 生物學領域:軌跡圖能夠跟蹤動物遷徙路線和觀察植物的生命歷程。
以交通領域為例,下面我們展示一個基於D3.js可視化軌跡圖庫的實例,提供給用戶實時路況分析及線路規劃服務:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>實時路況分析及線路規劃服務</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.15.0/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-geo/1.13.3/d3-geo.min.js"></script>
<style>
#map {
height: 800px;
}
.line {
fill: none;
stroke: #0000ff;
stroke-width: 2px;
}
</style>
</head>
<body>
<h2>實時路況分析及線路規劃服務</h2>
<div id="map"></div>
<script>
// 定義投影
var projection = d3.geoMercator()
.scale(40000)
.center([114.15, 22.57])
.translate([500, 500]);
var path = d3.geoPath()
.projection(projection);
// 讀取軌跡數據
d3.json("data.json").then(function(data) {
// 軌跡提示信息
var tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
// 繪製實時軌跡
setInterval(function() {
d3.select("svg").remove();
var svg = d3.select("#map").append("svg")
.attr("width", 1000)
.attr("height", 1000);
var now = Date.now();
var pathFeature = svg.append("g").attr("class", "path").selectAll("path")
.data(data.features)
.enter().append("path")
.attr("class", "line")
.attr("d", function(d) { return path(d.geometry.coordinates); })
.style("stroke-opacity", function(d) {
var delta = (now - Date.parse(d.properties.time)) / 1000;
return (delta < 30) ? 0.8 : (delta < 60) ? 0.5 : 0.3;
}).on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html("時間: " + d.properties.time + "<br>車速: " + d.properties.speed + " km/h")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
}).on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
var circleFeature = svg.append("g").attr("class", "point").selectAll("circle")
.data(data.features)
.enter().append("circle")
.attr("cx", function(d) { return projection(d.geometry.coordinates)[0]; })
.attr("cy", function(d) { return projection(d.geometry.coordinates)[1]; })
.attr("r", function(d) {
var delta = (now - Date.parse(d.properties.time)) / 1000;
return (delta < 30) ? 6 : (delta < 60) ? 4 : 2;
}).style("fill", function(d) {
var delta = (now - Date.parse(d.properties.time)) / 1000;
return (delta < 30) ? "#ff0000" : (delta < 60) ? "#00ff00" : "#0000ff";
}).style("fill-opacity", function(d) {
var delta = (now - Date.parse(d.properties.time)) / 1000;
return (delta < 30) ? 0.8 : (delta < 60) ? 0.5 : 0.3;
}).on("mouseover", function(d) {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html("時間: " + d.properties.time + "<br>車速: " + d.properties.speed + " km/h")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
}).on("mouseout", function(d) {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
}, 10000);
});
</script>
</body>
</html>
以上實例用D3.js繪製出了實時路況軌跡圖,以圓圈和折線的形式進行繪製,對於不同的速度和時效等,給予了不同的顏色和大小等標識。
五、總結
可視化軌跡圖是一種形象、直觀呈現移動路徑或時間序列信息的數據可視化形式,它在多個領域中有著廣泛的應用。在實際開發中,開發者需要
原創文章,作者:UAAKI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/361602.html