@gravity-ui/graph ## 页面存档备份,存于互联网档案馆

一个融合了两个世界中最好的图可视化库:
- 查看全图时显示高性能的相册
- HTML/React 用于放大时丰富的交互
不能再在表现和互动之间做出选择了。 适合大型图表、流程图和节点编辑器.

动机
现代的网络应用往往需要复杂的可视化和交互性,但现有的解决方案通常侧重于单一渲染技术:
- 帆布 为复杂图形提供高性能,但文本处理和互动有限.
- HTML 数据源 对界面来说是方便的,但对复杂的图形或大量元素来说效率较低.
@ gravity-ui/ graph 通过基于缩放级别在 Canvas 和 HTML 之间自动切换来解决这个问题 :
- 缩小: 高效渲染全图的用相机
- 中缩放: 显示具有基本交互作用的示意图视图
- 缩放: 切换到 HTML/React 组件用于丰富的交互
如何运作
库使用智能渲染系统,自动管理Canvas和React组件之间的过渡:
- 在低缩放水平上,所有东西都用 Canvas 来表现
- 当放大到详细视图时
GraphCanvas组件 :- 跟踪相机视图和比例变化
- 计算在当前视图端口可见的块( 带有平滑滚动的粘贴)
- 仅针对可见块重现组件
- 滚动或缩放时自动更新列表
- 缩放时删除反射组件
// Example of React components rendering
const MyGraph = () => {
return (
<GraphCanvas
graph={graph}
renderBlock={(graph, block) => (
<MyCustomBlockComponent
graph={graph}
block={block}
/>
)}
/>
);
};
安装
npm install @gravity-ui/graph
使用量
反应示例
import React, { useEffect } from "react";
import type { Graph, TBlock } from "@gravity-ui/graph";
import { EAnchorType, GraphState } from "@gravity-ui/graph";
import { GraphCanvas, GraphBlock, useGraph } from "@gravity-ui/graph/react";
const config = {};
export function GraphEditor() {
const { graph, setEntities, start } = useGraph(config);
useEffect(() => {
setEntities({
blocks: [
{
is: "block-action",
id: "action_1",
x: -100,
y: -450,
width: 126,
height: 126,
selected: true,
name: "Block #1",
anchors: [
{
id: "out1",
blockId: "action_1",
type: EAnchorType.OUT,
index: 0,
},
],
},
{
id: "action_2",
is: "block-action",
x: 253,
y: 176,
width: 126,
height: 126,
selected: false,
name: "Block #2",
anchors: [
{
id: "in1",
blockId: "action_2",
type: EAnchorType.IN,
index: 0,
},
],
},
],
connections: [
{
sourceBlockId: "action_1",
sourceAnchorId: "out1",
targetBlockId: "action_2",
targetAnchorId: "in1",
},
],
});
}, [setEntities]);
const renderBlockFn = (graph: Graph, block: TBlock) => {
return (
<GraphBlock graph={graph} block={block}>
{block.id}
</GraphBlock>
);
};
return (
<GraphCanvas
graph={graph}
renderBlock={renderBlockFn}
onStateChanged={({ state }) => {
if (state === GraphState.ATTACHED) {
start();
graph.zoomTo("center", { padding: 300 });
}
}}
/>
);
}
香草 Java 脚本示例
import { Graph } from "@gravity-ui/graph";
// Create container element
const container = document.createElement('div');
container.style.width = '100vw';
container.style.height = '100vh';
container.style.overflow = 'hidden';
document.body.appendChild(container);
// Initialize graph with configuration
const graph = new Graph({
configurationName: "example",
blocks: [],
connections: [],
settings: {
canDragCamera: true,
canZoomCamera: true,
useBezierConnections: true,
showConnectionArrows: true
}
}, container);
// Add blocks and connections
graph.setEntities({
blocks: [
{
is: "block-action",
id: "block1",
x: 100,
y: 100,
width: 120,
height: 120,
name: "Block #1",
anchors: [
{
id: "out1",
blockId: "block1",
type: EAnchorType.OUT,
index: 0
}
]
},
{
is: "block-action",
id: "block2",
x: 300,
y: 300,
width: 120,
height: 120,
name: "Block #2",
anchors: [
{
id: "in1",
blockId: "block2",
type: EAnchorType.IN,
index: 0
}
]
}
],
connections: [
{
sourceBlockId: "block1",
sourceAnchorId: "out1",
targetBlockId: "block2",
targetAnchorId: "in1"
}
]
});
// Start rendering
graph.start();
// Center the view
graph.zoomTo("center", { padding: 100 });
现场实例
文档
目录
许可证
根据麻省理工学院的许可证分发。 见 自由主义 详细情况.
对于人工智能特工
一个基于节点的图的混合Canvas/React图编辑器——它可以达到建立流程图,节点编辑器,或者大型交互式图,Canvas在低缩放时给出性能,而React组件在缩放时给出丰富的交互性.
何时使用
- 以节点为主的编辑器(流程图,管线,视觉构建器),有上百/千个节点和连接.
- 混合渲染:用于全图概览的 Canvas,用于高缩放时在取景口所可见的块的回放组件.
- 香草JS或反应消费者——核心
Graph类为框架不可知;@gravity-ui/graph/react提供反应约束.
不使用时
- 绘制数字数据序列(行/条/散图),使用
@gravity-ui/charts或者说@gravity-ui/yagr——图是节点/尖端图编辑器,不是数据图表. - 对于一个静态,非可编辑且节点不多的图,如果没有Canvas/React Viewport机器,SVG或更简单的图 lib可能就足够了.
常见陷阱
- 迷幻导入
GraphEditor反应组件是GraphCanvas, (中文(简体) ),GraphBlock,则useGraph钩子,从@gravity-ui/graph/react;核心类是Graph从@gravity-ui/graph。 。 。 . - 调用图形方法
ATTACHED状态 - 打电话start()编号 :zoomTo(...)内部onStateChanged回调时state === GraphState.ATTACHED而不是上马. - 忘记
setEntities—— 说useGraph返回时graph, (中文(简体) ),setEntities, (中文(简体) ),start; 数据仅出现在后面setEntities({blocks, connections})。 。 。 . - 混合锚类型 – 连接必须使用匹配来引用已存在的锚标识
EAnchorType(单位:千美元)IN编号 :OUT在源头和目标块.
AI代理的文件
安装版本的可代理读取文档位于 node_modules/@gravity-ui/graph/build/docs/INDEX.md。 。 。 .