Expand description
§arc-graph — GPUI 知识图谱画布(本地设计,方案 docs/graph/knowledge-graph-plan.md)
2D 知识图谱视图:Barnes–Hut 力导向布局在进程内后台线程跑(纯 UI 组件, 无任何后端服务),本视图每帧只取最新坐标快照渲染。
§使用方法
use arc_graph::Graph2D;
use arc_graph_core::KnowledgeGraph;
use gpui_kit::{Context, Render, Window, div, prelude::*};
struct Page {
canvas: gpui_kit::Entity<Graph2D>,
}
impl Page {
pub fn new(cx: &mut Context<Self>) -> Self {
let mut g = KnowledgeGraph::new();
let person = g.add_kind("人物");
let a = g.add_node("张三", Some(person));
let b = g.add_node("李四", Some(person));
g.add_edge(a, b, 1.0, None).unwrap();
// 力导向布局在后台线程自动迭代;已有坐标可用 `new_static` 跳过布局。
let canvas = cx.new(|cx| Graph2D::new("graph", g, cx).fit_on_mount());
Self { canvas }
}
}
impl Render for Page {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div().size_full().child(self.canvas.clone())
}
}§交互与接入(G4,无后端)
- 滚轮 / 背景拖动平移;Ctrl/⌘+滚轮或捏合缩放。
- 悬停高亮一度邻居,其余节点和边淡化(所有 LOD 一致)。
- 点击选择,Shift+点击切换多选,Shift+背景拖动框选。
- 拖动节点后保持固定,用
set_pinned(id, false, cx)解除。 search(query)返回当前可见匹配项;点击搜索结果后用fly_to(id, cx)弹簧定位。set_kind_filter(Some(kind), cx)筛选类型,None恢复全部; 筛选后双击节点或调用expand(id, cx)揭示一度邻居。- 搜索框、类型按钮由宿主页组合;完整示例见 gallery 的
graph-canvas页。
fn locate(canvas: &Entity<Graph2D>, cx: &mut App) {
if let Some(id) = canvas.read(cx).search("张").first().copied() {
canvas.update(cx, |view, cx| view.fly_to(id, cx));
}
canvas.update(cx, |view, cx| {
view.set_kind_filter(None, cx);
view.set_pinned(NodeId(1), false, cx);
});
}LOD(方案 §5):缩得很小时节点批量画 quad 点;中等画外壳; 放大才渲染 div 卡片(含文字)。缩小模式下按度数优先级显示浮动标签, 屏幕网格碰撞剔除,同屏最多 200 个。
Re-exports§
pub use motion::Appear;pub use motion::GraphMotion;pub use view::Graph2D;pub use view::Lod;pub use view::RenderCounts;
Modules§
- motion
- G4 Arc 动效(方案 §6;复用 arc-flow F6 的纯状态机套路,见
docs/graph/knowledge-graph-plan.md)。 - view
- 知识图谱 2D 画布。数据来自
arc_graph_core,渲染走 arc-tokens。
Structs§
- Edge
- 关系边。知识图谱常为有向(A → B),也支持无向整体开关。
- EdgeId
- Force
Layout2D - 2D 力导向布局(四叉树 Barnes–Hut)。
- Graph
Payload - 布局输入快照:与维度无关的槽位化图数据(节点按
NodeId升序,槽位稳定)。 - Knowledge
Graph - 知识图谱。有向 / 无向在图级别声明(布局与邻接口径一致)。
- Layout
Config - 布局参数。默认值适合百到万级节点的知识图谱;视觉调参走 arc-tokens(G3/G4)。
- Node
- 图节点。
weight是力导向质量,pinned表示布局中固定不动。 - NodeId
- Node
Kind - 节点类型(如「人物」「机构」「概念」)。只存名称;颜色映射在视图层按类型序号取 arc-tokens 调色板。
Enums§
- Graph
Error - 顶层错误。
Display与 arc-flow-core 的GraphError风格一致。