我是这里的初学者,并试图把我的头脑围绕在mxGraph库上。我的最终目的是从外部源读取mxGraphModel,并使用mxGraph -library (https://jgraph.github.io/mxgraph/)显示它,并提供功能反应组件。
但作为垫脚石,我正试图使这个示例(https://jgraph.github.io/mxgraph/docs/js-api/files/io/mxCodec-js.html)在组件中正常工作。
我也意识到这个库没有被维护--而且可能有其他的选择。
我设法使Hello -examples正常工作,所以导入和呈现工作得很好,但是我需要从外部XML变量或文件中读取该定义--而不是从内部定义它们。
https://jgraph.github.io/mxgraph/docs/tutorial.html
这就是我现在所拥有的。
import React, { useEffect, useCallback } from 'react';
import { StyledTestComponent} from '../styles/TestComponent.styled';
import mxgraph from 'mxgraph';
const {
mxGraph,
mxGraphModel,
// mxDragSource,
// mxUndoManager,
// mxCompactTreeLayout,
// mxGraphHandler,
// mxGuide,
// mxEdgeHandler,
// mxEdgeStyle,
// mxConstraintHandler,
// mxEvent,
// mxImage,
// mxConstants,
// mxKeyHandler,
// mxRubberband,
// mxPerimeter,
mxUtils,
// mxVertexHandler,
mxClient,
mxCodec,
} = mxgraph();
const DiagramRenderer = () => {
useEffect(() => {
console.log('Hello from useEffect');
}, []);
const xml =
'<root><mxCell id="2" value="Hello," vertex="1"><mxGeometry x="20" y="20" width="80" height="30" as="geometry"/></mxCell><mxCell id="3" value="World!" vertex="1"><mxGeometry x="200" y="150" width="80" height="30" as="geometry"/></mxCell><mxCell id="4" value="" edge="1" source="2" target="3"><mxGeometry relative="1" as="geometry"/></mxCell></root>';
if (!mxClient.isBrowserSupported()) {
mxUtils.error('Browser is not supported!', 200, false);
}
let graph = null;
const containerRef = useCallback((container) => {
if (container !== null) {
let model = new mxGraphModel();
graph = new mxGraph(container, model);
// const parent = graph.getDefaultParent();
let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());
graph.getModel().beginUpdate();
try {
// const v1 = graph.insertVertex(
// parent,
// null,
// 'Hello',
// 100,
// 100,
// 300,
// 100
// );
// const v2 = graph.insertVertex(
// parent,
// null,
// 'World',
// 500,
// 100,
// 300,
// 100
// );
// graph.insertEdge(parent, null, '', v1, v2);
var elt = doc.documentElement.firstChild;
var cells = [];
while (elt !== null) {
cells.push(codec.decodeCell(elt));
elt = elt.nextSibling;
}
graph.addCells(cells);
} finally {
graph.getModel().endUpdate();
}
}
}, []);
return (
<StyledTestComponent>
<div ref={containerRef}></div>
</StyledTestComponent>
);
};
export default TestComponent;
在这里记录图表时,我似乎有正确的东西(?)在那里,如果我是正确的,唯一的问题是画这个可见的,但我不完全确定。
任何小费都是非常感谢的!
发布于 2022-02-14 10:35:40
我设法做到了这一点,根据这个:mxCodec doesn't decode xml correctly -我添加了
window['mxGraphModel'] = mxGraphModel;
window['mxGeometry'] = mxGeometry;
在解析方法和更新我的导入之前
从“mxgraph图”导入mxgraph图;
const {
mxGraph,
mxGraphModel,
// mxDragSource,
// mxUndoManager,
// mxCompactTreeLayout,
// mxGraphHandler,
// mxGuide,
// mxEdgeHandler,
// mxEdgeStyle,
// mxConstraintHandler,
// mxEvent,
// mxImage,
// mxConstants,
// mxKeyHandler,
// mxRubberband,
// mxPerimeter,
mxGeometry,
mxUtils,
// mxVertexHandler,
mxClient,
mxCodec,
} = mxgraph();
这是整个函数组件,它读取XML并使用mxGraph将其呈现给屏幕。
这是整个解决方案:
import React, { useEffect, useCallback } from 'react';
import { StyledTestComponent } from '../styles/TestComponent.styled';
import mxgraph from 'mxgraph';
const {
mxGraph,
mxGraphModel,
mxGeometry,
mxUtils,
mxClient,
mxCodec,
} = mxgraph();
const TestComponent = () => {
useEffect(() => {
console.log('Hello from useEffect');
}, []);
const xml =
'<root><mxCell id="2" value="Hello," vertex="1"><mxGeometry x="20" y="20" width="80" height="30" as="geometry"/></mxCell><mxCell id="3" value="World!" vertex="1"><mxGeometry x="200" y="150" width="80" height="30" as="geometry"/></mxCell><mxCell id="4" value="" edge="1" source="2" target="3"><mxGeometry relative="1" as="geometry"/></mxCell></root>';
if (!mxClient.isBrowserSupported()) {
mxUtils.error('Browser is not supported!', 200, false);
}
let graph = null;
const containerRef = useCallback((container) => {
if (container !== null) {
let model = new mxGraphModel();
graph = new mxGraph(container, model);
window['mxGraphModel'] = mxGraphModel;
window['mxGeometry'] = mxGeometry;
let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());
graph.getModel().beginUpdate();
try {
var elt = doc.documentElement.firstChild;
var cells = [];
while (elt !== null) {
cells.push(codec.decodeCell(elt));
elt = elt.nextSibling;
}
graph.addCells(cells);
} finally {
graph.getModel().endUpdate();
}
}
}, []);
return (
<StyledTestComponent>
<div ref={containerRef}></div>
</StyledTestComponent>
);
};
export default TestComponent;
https://stackoverflow.com/questions/71105445
复制相似问题