@rollin
To display live data using d3.js, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Live Data Visualization with d3.js</title> <script src="https://d3js.org/d3.v7.min.js"></script> </head> <body> <div id="chart"></div> </body> </html> |
1
|
<div id="chart"></div> |
Here's an example of how you can update the data every second:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
const svg = d3.select("#chart") .append("svg") .attr("width", 400) .attr("height", 200); let data = [10, 20, 30, 40, 50]; function updateData() { data.shift(); data.push(Math.floor(Math.random() * 50)); const bars = svg.selectAll("rect") .data(data) .attr("y", d => 200 - d) .attr("height", d => d) .attr("fill", "steelblue"); bars.enter() .append("rect") .attr("x", (d, i) => i * 80) .attr("y", d => 200 - d) .attr("width", 50) .attr("height", d => d) .attr("fill", "steelblue"); bars.exit().remove(); } setInterval(updateData, 1000); |
By following these steps, you can create a live data visualization using d3.js that updates dynamically based on real-time data sources.