[英]Export mxGraph to SVG (Or any type of image)
I have implemented a diagram editor with mxGraph with javascript
, (Same as the one in the example provided by them), I can get an XML, here I let you an example: 我已经使用mxGraph和
javascript
实现了一个图编辑器(与它们提供的示例中的示例相同),我可以获取XML,在这里让我举一个例子:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mxGraphModel connect="1" fold="1" grid="1" gridSize="10" guides="1" page="0" pageHeight="1169" pageScale="1" pageWidth="826" tooltips="1">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" parent="1" style="whiteSpace=wrap" value="" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="80" y="70"/>
</mxCell>
<mxCell id="3" parent="1" style="whiteSpace=wrap" value="" vertex="1">
<mxGeometry as="geometry" height="60" width="120" x="280" y="70"/>
</mxCell>
<mxCell edge="1" id="4" parent="1" source="2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;" target="3">
<mxGeometry as="geometry" relative="1"/>
</mxCell>
</root>
</mxGraphModel>
I generate this xml from the javascript editor and sending it to a java class with ajax. 我从javascript编辑器生成此xml,并将其发送到带有ajax的java类。
I can't find a way to save an image svg (or any other kind of image), based on the xml I get on the java class. 我找不到基于保存在java类上的xml的图像svg(或任何其他类型的图像)的方法。
All the examples I find on internet shows how to export an image based on the mxgraph created directly on java, but not how to get it from the xml 我在Internet上找到的所有示例都显示了如何基于直接在Java上创建的mxgraph导出图像,但没有如何从xml获取图像
You can use mxXmlUtils.parseXml api. 您可以使用mxXmlUtils.parseXml api。 Here is the sample code snippet
这是示例代码片段
Document doc = mxXmlUtils.parseXml(erXmlString);
mxGraph graph = new mxGraph();
mxCodec codec = new mxCodec(doc);
codec.decode(doc.getDocumentElement(), graph.getModel());
mxGraphComponent graphComponent = new mxGraphComponent(graph);
BufferedImage image = mxCellRenderer.createBufferedImage(graphComponent.getGraph(), null, 1, Color.WHITE, graphComponent.isAntiAlias(), null, graphComponent.getCanvas());
mxPngEncodeParam param = mxPngEncodeParam.getDefaultEncodeParam(image);
param.setCompressedText(new String[] { "mxGraphModel", erXmlString });
FileOutputStream outputStream = new FileOutputStream(new File(filename));
mxPngImageEncoder encoder = new mxPngImageEncoder(outputStream, param);
if (image != null) {
encoder.encode(image);
return image;
}
outputStream.close();
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.