2024-11-21 20:39:49 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
2024-11-21 21:08:35 +01:00
|
|
|
<title>Live Heatmap with Smooth Scrolling</title>
|
2024-11-21 20:39:49 +01:00
|
|
|
<script src="https://d3js.org/d3.v7.min.js"></script>
|
|
|
|
<style>
|
2024-11-21 21:08:35 +01:00
|
|
|
svg {
|
|
|
|
display: block;
|
|
|
|
margin: auto;
|
2024-11-21 20:39:49 +01:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
2024-11-21 21:08:35 +01:00
|
|
|
<h2>Live Heatmap with Smooth Scrolling</h2>
|
2024-11-21 20:39:49 +01:00
|
|
|
<div id="chart"></div>
|
|
|
|
|
|
|
|
<script>
|
2024-11-21 21:08:35 +01:00
|
|
|
// Dimensions
|
|
|
|
const margin = { top: 20, right: 20, bottom: 20, left: 50 };
|
|
|
|
const width = 800 - margin.left - margin.right;
|
|
|
|
const height = 500 - margin.top - margin.bottom;
|
2024-11-21 20:39:49 +01:00
|
|
|
|
|
|
|
// Create SVG
|
|
|
|
const svg = d3.select("#chart")
|
|
|
|
.append("svg")
|
2024-11-21 21:08:35 +01:00
|
|
|
.attr("width", width + margin.left + margin.right)
|
|
|
|
.attr("height", height + margin.top + margin.bottom)
|
2024-11-21 20:39:49 +01:00
|
|
|
.append("g")
|
2024-11-21 21:08:35 +01:00
|
|
|
.attr("transform", `translate(${margin.left},${margin.top})`);
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Time scales
|
|
|
|
const y = d3.scaleTime()
|
|
|
|
.domain([new Date(Date.now() - 60000), new Date()]) // Past 1 min to now
|
|
|
|
.range([height, 0]);
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
const x = d3.scaleBand()
|
|
|
|
.domain(d3.range(10)) // Example: 10 categories
|
|
|
|
.range([0, width]);
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Heatmap color scale
|
|
|
|
const colorScale = d3.scaleSequential(d3.interpolateYlOrRd)
|
|
|
|
.domain([0, 100]); // Adjust for event intensity
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Axis
|
|
|
|
const yAxis = svg.append("g")
|
|
|
|
.attr("class", "y-axis")
|
|
|
|
.call(d3.axisLeft(y).ticks(20));
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
const grid = svg.append("g");
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Initialize heatmap
|
|
|
|
function updateHeatmap(data) {
|
|
|
|
const cells = grid.selectAll("rect").data(data, d => d.id);
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Enter new data
|
|
|
|
cells.enter()
|
|
|
|
.append("rect")
|
|
|
|
.attr("x", d => x(d.category))
|
|
|
|
.attr("width", x.bandwidth())
|
|
|
|
.attr("y", d => y(new Date(d.timestamp)))
|
|
|
|
.attr("height", 5) // Fixed height for heatmap blocks
|
|
|
|
.attr("fill", d => colorScale(d.value))
|
|
|
|
.merge(cells) // Merge updates
|
2024-11-21 20:39:49 +01:00
|
|
|
.transition()
|
2024-11-21 21:08:35 +01:00
|
|
|
.duration(500)
|
|
|
|
.attr("y", d => y(new Date(d.timestamp)))
|
|
|
|
.attr("visibility", d =>
|
|
|
|
y(new Date(d.timestamp)) >= 0 && y(new Date(d.timestamp)) <= height
|
|
|
|
? "visible"
|
|
|
|
: "hidden"
|
|
|
|
); // Hide items out of bounds
|
|
|
|
|
|
|
|
// Remove old data that moves off the screen
|
|
|
|
cells.exit().remove();
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Shift all cells upward to simulate scrolling
|
|
|
|
grid.selectAll("rect")
|
|
|
|
.transition()
|
|
|
|
.duration(500)
|
|
|
|
.attr("y", d => y(new Date(d.timestamp)));
|
2024-11-21 20:39:49 +01:00
|
|
|
}
|
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Simulate real-time updates
|
|
|
|
let allData = [];
|
2024-11-21 20:39:49 +01:00
|
|
|
setInterval(() => {
|
2024-11-21 21:08:35 +01:00
|
|
|
// Generate random data
|
|
|
|
const newData = Array.from({ length: 10 }, (_, i) => ({
|
|
|
|
id: `${Date.now()}-${i}`,
|
|
|
|
category: i,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
value: Math.random() * 100,
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Add new data to the global array and keep only recent data
|
|
|
|
allData = [...allData, ...newData].filter(d =>
|
|
|
|
new Date(d.timestamp) >= new Date(Date.now() - 60000)
|
|
|
|
);
|
2024-11-21 20:39:49 +01:00
|
|
|
|
2024-11-21 21:08:35 +01:00
|
|
|
// Update time scale domain
|
|
|
|
y.domain([new Date(Date.now() - 60000), new Date()]);
|
|
|
|
yAxis.transition().duration(500).call(d3.axisLeft(y).ticks(20));
|
|
|
|
|
|
|
|
// Update heatmap with filtered data
|
|
|
|
updateHeatmap(allData);
|
|
|
|
}, 1000);
|
2024-11-21 20:39:49 +01:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|