Ds 的實際應用:進階資料視覺化技術與範例
基礎知識
首先,我們需要一個 HTML 檔案來匯入 D3.js 庫並準備畫布來放置我們的圖表。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Getting Started with D3.js Example</title> <script src="https://d3js.org/d3.v7.min.js"></script> </head> <body> <svg width="500" height="500"></svg> </body> </html>
創建一個簡單的折線圖
// Assume we have the following data var data = [4, 8, 15, 16, 23, 42]; // Create an SVG canvas var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 50}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom; // Create x and y scales var x = d3.scaleLinear() .domain(d3.extent(data, d => d)) .range([0, width]); var y = d3.scaleLinear() .domain([0, d3.max(data)]) .range([height, 0]); // Create the x and y axes var xAxis = d3.axisBottom(x), yAxis = d3.axisLeft(y); // Add axis svg.append("g") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .call(yAxis); // Draw the polyline var line = d3.line() .x(d => x(d)) .y(d => y(d)); svg.append("path") .datum(data) .attr("class", "line") .attr("d", line);
建立長條圖
// Suppose we have the following data var data = [4, 8, 15, 16, 23, 42]; // Creating the SVG canvas and scale var svg = d3.select("svg").attr("width", 500).attr("height", 500); var margin = {top: 20, right: 20, bottom: 30, left: 40}; var width = +svg.attr("width") - margin.left - margin.right; var height = +svg.attr("height") - margin.top - margin.bottom; var x = d3.scaleBand().rangeRound([0, width]).padding(0.1); var y = d3.scaleLinear().rangeRound([height, 0]); // Mapping data to scale x.domain(data.map(function(d) { return d; })); y.domain([0, d3.max(data)]); // Creating an SVG g Element var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); // Adding x and y axes g.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); g.append("g") .call(d3.axisLeft(y)); // Draw a bar chart g.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d); }) .attr("y", function(d) { return y(d); }) .attr("width", x.bandwidth()) .attr("height", function(d) { return height - y(d); });
創建圓餅圖
// Suppose we have the following data var data = [4, 8, 15, 16, 23, 42]; // Creating the SVG canvas and scale var svg = d3.select("svg").attr("width", 500).attr("height", 500); var radius = Math.min(svg.attr("width"), svg.attr("height")) / 2; // Creating an arc scale var arc = d3.arc().outerRadius(radius).innerRadius(0); var pie = d3.pie().value(function(d) { return d; }); // Draw a pie chart var g = svg.append("g") .attr("transform", "translate(" + radius + "," + radius + ")"); var arcs = g.selectAll("arc") .data(pie(data)) .enter().append("g") .attr("class", "arc"); arcs.append("path") .attr("d", arc) .attr("fill", function(d, i) { return d3.schemeCategory10[i]; }); arcs.append("text") .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; }) .attr("dy", ".35em") .text(function(d) { return d.data; });
互動性和動畫
互動範例:為長條圖新增懸停效果
// Assuming that the bar chart base code already exists // ... // Add hover effects g.selectAll(".bar") .on("mouseover", function(event, d) { d3.select(this) .transition() .duration(200) .attr("fill", "orange"); // Mouseover color change // Show Data Tips var tooltip = g.append("text") .attr("class", "tooltip") .attr("x", x(d) + x.bandwidth() / 2) .attr("y", y(d) - 10) .text(d); }) .on("mouseout", function(event, d) { d3.select(this) .transition() .duration(200) .attr("fill", "steelblue"); // Restore original color // Remove data tips g.selectAll(".tooltip").remove(); });
動畫範例:平滑過渡折線圖資料更新
// Assume that there is already a line chart basic code // ... // Update data var newData = [8, 15, 16, 23, 42, 45]; // Update scale domain x.domain(d3.extent(newData)); y.domain([0, d3.max(newData)]); // Update axis g.select(".axis--x").transition().duration(750).call(xAxis); g.select(".axis--y").transition().duration(750).call(yAxis); // Update path var path = g.select(".line"); path.datum(newData).transition().duration(750).attr("d", line);
複雜圖:力導向圖
力向圖展示了節點和邊之間的關係,非常適合視覺化網路和社交圖等資料。
// Assume we have data on nodes and edges var nodes = [{id: "A"}, {id: "B"}, {id: "C"}]; var links = [{source: nodes[0], target: nodes[1]}, {source: nodes[1], target: nodes[2]}]; // Creating the SVG Canvas var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height"); // Creating a Force Simulation var simulation = d3.forceSimulation(nodes) .force("link", d3.forceLink(links).id(function(d) { return d.id; })) .force("charge", d3.forceManyBody()) .force("center", d3.forceCenter(width / 2, height / 2)); // Creating links and nodes var link = svg.append("g") .attr("stroke", "#999") .attr("stroke-opacity", 0.6) .selectAll("line") .data(links) .join("line") .attr("stroke-width", 2); var node = svg.append("g") .attr("stroke", "#fff") .attr("stroke-width", 1.5) .selectAll("circle") .data(nodes) .join("circle") .attr("r", 5) .call(d3.drag() .on("start", dragstarted) .on("drag", dragged) .on("end", dragended)); node.append("title") .text(function(d) { return d.id; }); simulation.on("tick", ticked); function ticked() { link .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); node .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); } // Drag event handling function function dragstarted(event, d) { if (!event.active) simulation.alphaTarget(0.3).restart(); d.fx = d.x; d.fy = d.y; } function dragged(event, d) { d.fx = event.x; d.fy = event.y; } function dragended(event, d) { if (!event.active) simulation.alphaTarget(0); d.fx = null; d.fy = null; }
地圖視覺化
D3.js 可以使用 GeoJSON 等地理資料格式來建立互動式地圖。這包括國家、州、城市邊界等
基本步驟:
載入地圖資料:使用D3的d3.json或d3.geoJson載入GeoJSON資料。
建立比例:定義地理投影和比例,例如 Mercator 或 Albers USA。
綁定資料並繪製:將 GeoJSON 資料綁定到 SVG 路徑元素並套用投影。
新增互動:如懸停效果、點擊事件等
d3.json("world.geojson").then(function(geoData) { var svg = d3.select("svg"), projection = d3.geoMercator().scale(130).translate([400, 250]), path = d3.geoPath().projection(projection); svg.selectAll("path") .data(geoData.features) .enter().append("path") .attr("d", path) .attr("fill", "#ccc") .attr("stroke", "#fff"); });
資料綁定和動態更新
基本步驟:
初始化資料綁定:使用 data() 方法將資料綁定到 DOM 元素。
進入、更新、退出模式:處理新資料、更新現有資料、刪除無用資料。
動態更新:監控資料變化,重新執行綁定和渲染過程。
var svg = d3.select("svg"), data = [4, 8, 15, 16, 23, 42]; // Initialize the bar chart var bars = svg.selectAll("rect").data(data); bars.enter().append("rect") .attr("x", function(d, i) { return i * 50; }) .attr("y", function(d) { return 300 - d; }) .attr("width", 40) .attr("height", function(d) { return d; }); // Dynamic Updates setInterval(function() { data = data.map(function(d) { return Math.max(0, Math.random() * 50); }); bars.data(data) .transition() .duration(500) .attr("y", function(d) { return 300 - d; }) .attr("height", function(d) { return d; }); }, 2000);
複雜的圖表和先進的技術
進階技巧:
使用 D3 元件庫:D3fc 等函式庫提供進階圖表元件來簡化複雜圖表的建立。
動畫和過渡:使用transition()方法創建流暢的動畫效果。
互動性:新增點擊和懸停事件,並使用畫筆和縮放功能來增強使用者體驗。
效能最佳化:合理使用selectAll()、data()、enter()、exit()減少DOM操作,使用requestAnimationFrame()最佳化動畫效能。
以上是Ds 的實際應用:進階資料視覺化技術與範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

不同JavaScript引擎在解析和執行JavaScript代碼時,效果會有所不同,因為每個引擎的實現原理和優化策略各有差異。 1.詞法分析:將源碼轉換為詞法單元。 2.語法分析:生成抽象語法樹。 3.優化和編譯:通過JIT編譯器生成機器碼。 4.執行:運行機器碼。 V8引擎通過即時編譯和隱藏類優化,SpiderMonkey使用類型推斷系統,導致在相同代碼上的性能表現不同。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

本文展示了與許可證確保的後端的前端集成,並使用Next.js構建功能性Edtech SaaS應用程序。 前端獲取用戶權限以控制UI的可見性並確保API要求遵守角色庫

我使用您的日常技術工具構建了功能性的多租戶SaaS應用程序(一個Edtech應用程序),您可以做同樣的事情。 首先,什麼是多租戶SaaS應用程序? 多租戶SaaS應用程序可讓您從唱歌中為多個客戶提供服務

從C/C 轉向JavaScript需要適應動態類型、垃圾回收和異步編程等特點。 1)C/C 是靜態類型語言,需手動管理內存,而JavaScript是動態類型,垃圾回收自動處理。 2)C/C 需編譯成機器碼,JavaScript則為解釋型語言。 3)JavaScript引入閉包、原型鍊和Promise等概念,增強了靈活性和異步編程能力。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。
