基本用法
引入
import AIFlow from '@baidu/aiflow';
基本用法
定义一个 AIFlow 容器,通过 new AIFlow 进行渲染。
function App() {
const node1 = {
id: '100',
defineData: {
nodeName: '数据拆分'
},
// 画布中位置
position: [100, 100],
// 输入
input: [
{}
],
// 输出
output: [
{}
]
};
const node2 = {
id: '101',
defineData: {
nodeName: '随机采样'
},
// 画布中位置
position: [300, 100],
// 输入
input: [
{}
],
// 输出
output: [
{}
]
};
const node3 = {
// id
id: '102',
defineData: {
nodeName: '列选择'
},
// 画布中位置
position: [100, 300],
// 输入
input: [
{}
],
// 输出
output: [
{}
]
};
const nodes = [
node1,
node2,
node3
];
const edges = [
{
// 连线起点,输出节点的id : 输出port的index
// 100:0 表示 id 为 100 的节点的第一个输出
src: '100:0',
// 连线终点,输入节点的id : 输入port的index
// 101:0 表示 id 为 101 的节点的第一个输入
to: '101:0'
},
{
src: '100:0',
to: '102:0'
}
];
// AIFlow 全局配置
const GLOBAL_CONFIG = {
// 这里宽高和容器保持一致
cWidth: 1000,
cHeight: 1000,
// 是否静态图片,true 节点和连线都不可拖动,false 可以添加事件监听
isStatic: false,
// 开启辅助线
guideLineOn: true
};
useEffect(() => {
const aiflowIns = new AIFlow(
// 容器 DOM 对象
document.getElementById('ai-flow-box'),
{
nodes,
edges
},
// 全局配置
GLOBAL_CONFIG
);
}, [])
return (
<div className="ai-flow-out-box">
<div id="ai-flow-box" className="ai-flow-box"></div>
</div>
);
}
API
Flow
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| data | 初始化数据 | {nodes, edges} | [] |
| config | 配置 | Object | -- |
| ref | 组件实例 | ref | -- |
| templates | 模版列表 | Array | [] |
data node
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| id | 唯一标识符 | string | -- |
| defineData | 模版中的自定义数据 | Object | null |
| position | 在画布中的位置 | Array | [0, 0] |
| input | 输入点集合,一个对象代表一个输入点 | Array | [] |
| output | 输出点集合,一个对象代表一个输出点 | Array | [] |
data edge
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| src | 连线起点,格式为'{node.id}:{node的输出index}' | string | -- |
| to | 连线终点,格式为'{node.id}:{node的输入index}' | string | -- |
config
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| isStatic | 是否静态图片 | boolean | false |
| relationMapping | 关系映射方式 目前支持一对多(o2m)、多对多(m2m)、暂不支持多对一(m2o)、一对一(o2o) 默认值 o2m | string | o2m |
| cWidth | 画布宽度 | number | 400 |
| cHeight | 画布高度 | number | 400 |
| templateName | 是否静态图片 | boolean | false |
| isStatic | 整个图默认模版 | string | defaultTemplate |
| guideLine | 辅助线相关配置 | Object | -- |
| guideLine.on | 是否开启辅助线 | boolean | true |
| guideLine.style | 辅助线样式 | Object | -- |
| autoSort | 自动排序相关配置 | Object | -- |
| autoSort.on | 是否需要自动排序 true: 程序智能计算每个 node 的 位置 false: 根据 node position 来定位 | boolean | false |
| autoSort.horizontal | 是否水平排序 true: 水平排序 false: 垂直排序 | boolean | false |
| autoSort.align | node 对齐方式 start: 水平排列时表示上对齐,垂直排列时表示左对齐 middle: 中间对齐 end: 水平排列时表示下对齐,垂直排列时表示右对齐 | string | middle |
| autoSort.beginX | 起点 X 坐标 | number | 0 |
| autoSort.beginY | 起点 Y 坐标 | number | 0 |
| autoSort.spaceX | 横向间距 | number | 50 |
| autoSort.spaceY | 纵向间距 | number | 20 |
| autoSort.type | 排序算法选择,基础的排序算法 (default),中位数排序算法 (withMedium) | string | default |
| autoSort.hryType | 排序算法分层方法选择:基础分层方法(default)、重心法(barycenter) | string | default |
| frameSelect | 框选参数 | Object | -- |
| frameSelect.style | 框选样式 | Object | {} |
| zoom | 鼠标滚动触发缩放事件相关配置 | Object | -- |
| zoom.default | 是否添加默认的缩放响应,即基于鼠标位置对整个图进行缩放 | boolean | false |
| zoom.minScale | 默认缩放响应的缩放下限 | number | 0.2 |
| zoom.maxScale | 默认缩放响应的缩放上限 | number | 5 |
| pan | 鼠标拖动触发平移事件相关配置 | Object | -- |
| pan.default | 是否添加默认的平移响应,添加默认的 pan 事件,即在框选不打开和没有节点选中时,平移整个图 | boolean | false |
| dragWithinCanvas | 拖动节点时,检测并限制节点不移动到 canvas 外部 | boolean | true |
templates
需要注册的模版
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| name | 模版名称 | string | -- |
| content | 模版对象 | Object | -- |
模版内容可以参考:
https://ecomfe.github.io/zrender-doc/public/api.html#zrenderdisplayable
TODO...