可视化报表:
- 假设要生成一个简单的柱状图展示特定用户特定事件类型的出现次数。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Event Count Visualization</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<div id="chart"></div>
<script>
async function drawChart() {
const user_id = "123";
const event_type = "click_event";
const count = await fetchData(user_id, event_type);
const svg = d3.select("#chart")
.append("svg")
.attr("width", 300)
.attr("height", 200);
const barHeight = 30;
const bar = svg.append("rect")
.attr("x", 50)
.attr("y", 50)
.attr("width", count * 10)
.attr("height", barHeight)
.attr("fill", "steelblue");
const label = svg.append("text")
.attr("x", 60)
.attr("y", 70)
.text(`Count: ${count}`);
}
drawChart();
</script>
</body>
</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>Interactive Event Count Visualization</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
<select id="userSelector">
<option value="123">User 1</option>
<option value="456">User 2</option>
</select>
<select id="eventSelector">
<option value="click_event">Click Event</option>
<option value="scroll_event">Scroll Event</option>
</select>
<div id="chart"></div>
<script>
async function drawChart(user_id, event_type) {
const count = await fetchData(user_id, event_type);
const svg = d3.select("#chart")
.selectAll("svg")
.remove();
const newSvg = d3.select("#chart")
.append("svg")
.attr("width", 300)
.attr("height", 200);
const barHeight = 30;
const bar = newSvg.append("rect")
.attr("x", 50)
.attr("y", 50)
.attr("width", count * 10)
.attr("height", barHeight)
.attr("fill", "steelblue");
const label = newSvg.append("text")
.attr("x", 60)
.attr("y", 70)
.text(`Count: ${count}`);
}
d3.select("#userSelector").on("change", function () {
const user_id = this.value;
const event_type = d3.select("#eventSelector").property("value");
drawChart(user_id, event_type);
});
d3.select("#eventSelector").on("change", function () {
const event_type = this.value;
const user_id = d3.select("#userSelector").property("value");
drawChart(user_id, event_type);
});
async function fetchData(user_id, event_type) {
const response = await fetch(`/api/stats?user_id=${user_id}&event_type=${event_type}`);
const data = await response.json();
return data.count;
}
drawChart("123", "click_event");
</script>
</body>
</html>
- 在Ruby后端,需要实现对应的API接口来返回特定用户和事件类型的统计数据,例如:
require 'rack'
require'redis'
app = lambda do |env|
request = Rack::Request.new(env)
if request.get?
user_id = request.params['user_id']
event_type = request.params['event_type']
redis = Redis.new
count = get_event_count(redis, user_id, event_type)
[200, {'Content-Type' => 'application/json'}, [{'count' => count}.to_json]]
else
[405, {'Content-Type' => 'application/json'}, [{'status' => 'Method Not Allowed'}.to_json]]
end
end
Rack::Server.start(
app: app,
Port: 3000
)