Rust数据可视化库re_data_ui的使用,re_data_ui提供高效的数据渲染与交互式UI组件
Rust数据可视化库re_data_ui的使用,re_data_ui提供高效的数据渲染与交互式UI组件
re_data_ui是rerun系列crate的一部分,为Rerun Viewer提供组件数据的UI元素。
安装
在项目目录中运行以下Cargo命令:
cargo add re_data_ui
或者在Cargo.toml中添加以下行:
re_data_ui = "0.24.1"
使用示例
以下是一个完整的示例代码,展示如何使用re_data_ui进行数据可视化:
use re_data_ui::Viewer;
use re_log_types::EntityPath;
use re_viewer_context::{CommandSender, SystemCommand, SystemCommandSender};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建Viewer实例
let mut viewer = Viewer::new();
// 设置UI主题
viewer.set_theme(re_data_ui::Theme::Light);
// 创建命令发送器
let command_sender = CommandSender::new();
// 添加数据到Viewer
let entity_path = EntityPath::from("example/points");
let points = vec![
glam::Vec3::new(0.0, 0.0, 0.0),
glam::Vec3::new(1.0, 1.0, 0.0),
glam::Vec3::new(2.0, 0.0, 1.0),
];
// 发送添加点数据的命令
command_sender.send_system(SystemCommand::UpdateBlueprint(
entity_path,
points.into(),
));
// 运行Viewer
viewer.run()?;
Ok(())
}
完整示例代码
以下是一个更完整的示例,展示如何使用re_data_ui创建交互式数据可视化应用:
use re_data_ui::Viewer;
use re_log_types::EntityPath;
use re_viewer_context::{CommandSender, SystemCommand, SystemCommandSender};
use glam::Vec3;
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 初始化Viewer
let mut viewer = Viewer::new();
// 设置暗色主题
viewer.set_theme(re_data_ui::Theme::Dark);
// 创建命令发送器
let command_sender = CommandSender::new();
// 创建多个实体路径
let points_path = EntityPath::from("demo/points");
let lines_path = EntityPath::from("demo/lines");
// 生成点数据
let points = (0..10).map(|i| {
let x = i as f32 * 0.5;
Vec3::new(x, x.sin(), x.cos())
}).collect::<Vec<_>>();
// 生成线数据
let lines = (0..20).map(|i| {
let t = i as f32 * 0.1;
Vec3::new(t, t.sin() * 0.5, t.cos() * 0.5)
}).collect::<Vec<_>>();
// 发送点数据
command_sender.send_system(SystemCommand::UpdateBlueprint(
points_path.clone(),
points.into(),
));
// 发送线数据
command_sender.send_system(SystemCommand::UpdateBlueprint(
lines_path.clone(),
lines.into(),
));
// 模拟实时数据更新
std::thread::spawn(move || {
let mut t = 0.0;
loop {
std::thread::sleep(Duration::from_millis(100));
t += 0.1;
// 更新动态点
let dynamic_point = vec![Vec3::new(t, t.sin(), 0.0)];
command_sender.send_system(SystemCommand::UpdateBlueprint(
EntityPath::from("demo/dynamic"),
dynamic_point.into(),
));
}
});
// 运行Viewer
viewer.run()?;
Ok(())
}
功能特点
- 提供高效的2D/3D数据渲染能力
- 支持交互式UI组件
- 内置多种可视化图表类型
- 支持实时数据更新
- 提供主题定制功能
许可证
re_data_ui采用MIT或Apache-2.0双许可证。
1 回复
Rust数据可视化库re_data_ui使用指南
re_data_ui是一个高效的Rust数据可视化库,专注于提供快速的数据渲染和交互式UI组件。它特别适合需要高性能可视化解决方案的应用场景。
主要特性
- 高性能数据渲染
- 交互式UI组件
- 简洁的API设计
- 支持多种图表类型
- 实时数据更新能力
安装方法
在Cargo.toml中添加依赖:
[dependencies]
re_data_ui = "0.7" # 请使用最新版本
基本使用方法
1. 创建简单图表
use re_data_ui::{
plot::{LinePlot, PlotSeries},
AppBuilder,
};
fn main() {
// 创建数据系列
let series = PlotSeries::new("示例数据")
.with_data(vec![(0.0, 1.0), (1.0, 3.0), (2.0, 2.0), (3.0, 4.0)]);
// 创建折线图
let plot = LinePlot::new("示例图表").with_series(series);
// 构建并运行应用
AppBuilder::new("数据可视化示例")
.add_plot(plot)
.run();
}
2. 交互式表格
use re_data_ui::{table::DataTable, AppBuilder};
fn main() {
// 准备表格数据
let data = vec![
vec!["Alice".to_string(), "25".to_string(), "Engineer".to_string()],
vec!["Bob".to_string(), "30".to_string(), "Designer".to_string()],
vec!["Charlie".to_string(), "35".to_string(), "Manager".to_string()],
];
// 设置表头
let headers = vec!["Name".to_string(), "Age".to_string(), "Role".to_string()];
// 创建数据表格
let table = DataTable::new("员工数据")
.with_headers(headers)
.with_data(data);
// 构建并运行应用
AppBuilder::new("表格示例")
.add_table(table)
.run();
}
3. 实时数据更新
use re_data_ui::{
plot::{LinePlot, PlotSeries},
AppBuilder,
};
use std::time::Duration;
fn main() {
// 创建空数据系列
let mut series = PlotSeries::new("实时数据").with_data(vec![]);
// 创建折线图并绑定数据
let plot = LinePlot::new("实时图表").with_series(series.clone());
// 构建应用(不立即运行)
let app = AppBuilder::new("实时数据示例")
.add_plot(plot)
.build();
// 在后台线程模拟实时数据更新
std::thread::spawn(move || {
let mut x = 0.0;
loop {
std::thread::sleep(Duration::from_secs(1));
let y = (x * 0.1).sin(); // 生成正弦波数据
series.push((x, y)); // 添加新数据点
x += 1.0;
}
});
// 运行应用
app.run();
}
高级功能
自定义样式
use re_data_ui::{
plot::{LinePlot, PlotSeries, LineStyle},
AppBuilder,
style::{Color, Font},
};
fn main() {
// 创建数据系列并自定义线条样式
let series = PlotSeries::new("自定义样式")
.with_data(vec![(0.0, 1.0), (1.0, 3.0), (2.0, 2.0), (3.0, 4.0)])
.with_style(LineStyle::new()
.with_color(Color::rgb(255, 0, 0)) // 红色线条
.with_width(2.0)); // 线宽2像素
// 创建图表并自定义标题字体
let plot = LinePlot::new("样式示例")
.with_series(series)
.with_title_font(Font::new().with_size(16.0)); // 标题字体大小16px
// 构建并运行应用
AppBuilder::new("样式示例")
.add_plot(plot)
.run();
}
多图表布局
use re_data_ui::{
plot::{LinePlot, BarPlot, PlotSeries},
AppBuilder,
layout::GridLayout,
};
fn main() {
// 准备折线图数据
let line_data = PlotSeries::new("折线图")
.with_data(vec![(0.0, 1.0), (1.0, 3.0), (2.0, 2.0), (3.0, 4.0)]);
// 准备柱状图数据
let bar_data = PlotSeries::new("柱状图")
.with_data(vec![("A", 10), ("B", 20), ("C", 15)]);
// 创建2行1列的网格布局
let layout = GridLayout::new(2, 1)
// 第一行放置折线图
.with_plot(0, 0, LinePlot::new("折线图示例").with_series(line_data))
// 第二行放置柱状图
.with_plot(1, 0, BarPlot::new("柱状图示例").with_series(bar_data));
// 构建并运行应用
AppBuilder::new("多图表布局示例")
.with_layout(layout)
.run();
}
性能优化技巧
- 对于大数据集,使用
with_lazy_loading
启用延迟加载 - 避免频繁的完整数据更新,使用增量更新方法
- 对于静态数据,可以预渲染图表
完整示例:综合仪表盘
use re_data_ui::{
plot::{LinePlot, BarPlot, PiePlot, PlotSeries},
table::DataTable,
AppBuilder,
layout::GridLayout,
style::{Color, Font, LineStyle},
};
use std::time::Duration;
fn main() {
// 1. 准备各种图表数据
// 折线图数据 - 正弦波
let line_series = PlotSeries::new("正弦波")
.with_data((0..100).map(|x| (x as f32 * 0.1, (x as f32 * 0.1).sin())).collect::<Vec<_>>())
.with_style(LineStyle::new().with_color(Color::rgb(0, 0, 255)));
// 柱状图数据 - 月度销售额
let bar_series = PlotSeries::new("销售额")
.with_data(vec![
("1月", 120),
("2月", 200),
("3月", 150),
("4月", 300),
]);
// 饼图数据 - 市场份额
let pie_series = PlotSeries::new("市场份额")
.with_data(vec![
("苹果", 45),
("三星", 30),
("华为", 15),
("其他", 10),
]);
// 表格数据 - 员工信息
let table_data = vec![
vec!["张三".to_string(), "28".to_string(), "研发".to_string(), "5000".to_string()],
vec!["李四".to_string(), "35".to_string(), "市场".to_string(), "6000".to_string()],
vec!["王五".to_string(), "42".to_string(), "管理".to_string(), "8000".to_string()],
];
// 2. 创建各种图表
let line_plot = LinePlot::new("正弦波示例")
.with_series(line_series)
.with_title_font(Font::new().with_size(14.0));
let bar_plot = BarPlot::new("月度销售额")
.with_series(bar_series);
let pie_plot = PiePlot::new("市场份额")
.with_series(pie_series);
let table = DataTable::new("员工信息表")
.with_headers(vec!["姓名".to_string(), "年龄".to_string(), "部门".to_string(), "薪资".to_string()])
.with_data(table_data);
// 3. 创建3x2的网格布局
let layout = GridLayout::new(3, 2)
.with_plot(0, 0, line_plot) // 第一行第一列: 折线图
.with_plot(0, 1, bar_plot) // 第一行第二列: 柱状图
.with_plot(1, 0, pie_plot) // 第二行第一列: 饼图
.with_table(2, 0, table) // 第三行: 表格(跨两列)
.with_col_span(2, 0, 2); // 设置表格跨两列
// 4. 构建并运行应用
AppBuilder::new("综合仪表盘示例")
.with_layout(layout)
.run();
}
re_data_ui提供了丰富的功能和灵活的配置选项,可以满足从简单到复杂的数据可视化需求。通过组合不同的组件和布局,可以构建出功能强大的数据可视化应用。