加载和存储数据
您可以通过以下方式为 DHTMLX Diagram 填充数据:
准备要加载的数据
DHTMLX Diagram 接受 JSON 格式的数据。对于默认模式、组织结构图模式和思维导图模式,数据是一个包含一组数据对象的数组。共有 5 种对象类型:
- 形状对象
const data = [
// 配置形状
{ "id": "shape_1", "type": "start", "x": 200, y: 0, "text": "Start" },
{ "id": "shape_2", "type": "process", "x": 200, y: 120, "text": "Call Client and \n set-up Appointment" },
{ "id": "shape_3", "type": "decision", "x": 200, "y": 240, "text": "Decision" },
// 配置连接线
{ "id": "line_1", "type": "line", "from": "shape_1", "to": "shape_2" },
{ "id": "line_2", "type": "dash", "from": "shape_2", "to": "shape_3" },
// 配置连接线标题
{ "id": "title_1", "type": "lineTitle", "parent": "line_1", "text": "Some text" }
];
该库为您提供了各种类型的默认形状,这些形状既有通用选项,也有特定选项。您可以在 API 参考中查看 shape 对象的完整属性列表。
您还可以创建自定义类型的形状,并为形状对象添加任意自定义属性。
- 连接线对象
const data = [
// 配置形状
{ "id": "shape_1", "type": "start", "x": 200, y: 0, "text": "Start" },
{ "id": "shape_2", "type": "process", "x": 200, y: 120, "text": "Call Client and \n set-up Appointment" },
{ "id": "shape_3", "type": "decision", "x": 200, "y": 240, "text": "Decision" },
// 配置连接线
{ "id": "line_1", "type": "line", "from": "shape_1", "to": "shape_2" },
{ "id": "line_2", "type": "dash", "from": "shape_2", "to": "shape_3" },
// 配置连接线标题
{ "id": "title_1", "type": "lineTitle", "parent": "line_1", "text": "Some text" }
];
数据集中是否存在连接线对象,取决于所选择的形状连接方式。您可以在 API 参 考中查看 line 对象的完整属性列表。
- 连接线标题对象
const data = [
// 配置形状
{ "id": "shape_1", "type": "start", "x": 200, y: 0, "text": "Start" },
{ "id": "shape_2", "type": "process", "x": 200, y: 120, "text": "Call Client and \n set-up Appointment" },
{ "id": "shape_3", "type": "decision", "x": 200, "y": 240, "text": "Decision" },
// 配置连接线
{ "id": "line_1", "type": "line", "from": "shape_1", "to": "shape_2" },
{ "id": "line_2", "type": "dash", "from": "shape_2", "to": "shape_3" },
// 配置连接线标题
{ "id": "title_1", "type": "lineTitle", "parent": "line_1", "text": "Some text" }
];
您可以在 API 参考中查看 连接线标题 对象的完整属性列表。
- 组对象
const data = [
{
"type": "$group",
"id": 1,
"width": 400,
"height": 200,
"x": 0,
"y": 0,
"header": {
"text": "Top and collapsed header with tеxt alignment",
"editable": true,
"closable": true,
"textAlign": "left", // "left", "center", "right"
"textVerticalAlign": "center", // "top", "center", "bottom"
},
// 组的子项目
"groupChildren": [1.1, 1.2],
"open": false,
},
// 配置要放入组中的形状
{ "type": "rectangle", "id": 1.1, "x": 50, "y": 75, "text": "Shape 1.1" },
{ "type": "rectangle", "id": 1.2, "x": 200, "y": 75, "text": "Shape 1.2" }
];
您可以在 API 参考中查看 group 对象的完整属性列表。
- 泳道及其单元格对象
const data = [
{
"id": "main",
"type": "$swimlane",
"height": 730,
"width": 1195,
"header": {
"closable": true,
"text": "Waterfall diagram template"
},
"layout": [
[1, 2, 3, 4]
],
"subHeaderCols": {
"headers": [
{
"text": "September",
"fill": "#f35c4f66"
},
{
"text": "October",
"fill": "#9b60f866"
},
{
"text": "November",
"fill": "#ffae1266"
},
{
"text": "December",
"fill": "#3cc97a66"
}
]
}
},
// 配置泳道的单元格
{
"id": 1,
"type": "$sgroup",
"groupChildren": ["s1"],
"style": {
"fill": "#D4DAE4"
},
"x": 0,
"y": 80
},
// 配置要放入单元格中的形状
{
"id": "s1", "type": "end", "text": "Step 1", "x": 20, "y": 110
}
];
您可以在 API 参考中查看 swimlane 及其单元格对象的完整配置属性列表。