mxgraph有encode 和decode方法,既然可以通过encode编码为xml文件:
var encoder = new mxCodec();
var node = encoder.encode(graph.getModel());
那么解码成图像也可以实现:
var req = mxUtils.load('jbpm/mxgraph.xml');
var root = req.getDocumentElement();
var dec = new mxCodec(root);
dec.decode(root, graph.getModel());
graph.getModel().endUpdate();
需要注意的是:
mxgraph.xml中自定义的style在这个解码图像的js中也要写一遍,如:
var style = new Object();
style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_IMAGE;
style[mxConstants.STYLE_PERIMETER] = mxPerimeter.RectanglePerimeter;
style[mxConstants.STYLE_IMAGE] = 'editors/images/bigicon/start_event_empty.png';
style[mxConstants.STYLE_IMAGE_WIDTH] = '48';
style[mxConstants.STYLE_IMAGE_HEIGHT] = '48';
style[mxConstants.STYLE_FONTCOLOR] = '#000000';
style[mxConstants.STYLE_VERTICAL_LABEL_POSITION] = mxConstants.ALIGN_CENTER;
graph.getStylesheet().putCellStyle('start-s', style);
完整的一个显示xml流程图的代码:
mxgraphShow.html
<!--
$Id: uiconfig.html,v 1.6 2010-01-02 09:45:14 gaudenz Exp $
Copyright (c) 2006-2010, JGraph Ltd
UIConfig example for mxGraph. This example demonstrates using a config
file to configure the toolbar and popup menu in mxEditor.
-->
<html>
<head>
<title>UIConfig example</title>
<!-- Sets the basepath for the library if not in same directory -->
<!-- Loads and initiaizes the library -->
<link rel="stylesheet" type="text/css" href="ext-3.3.0/resources/css/ext-all.css" />
<script type="text/javascript" src="ext-3.3.0/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="ext-3.3.0/ext-all.js"></script>
<script type="text/javascript">
mxBasePath = 'ext-3.3.0/src';
</script>
<!-- Loads and initiaizes the library -->
<script type="text/javascript" src="ext-3.3.0/mxclient-chrome.js"></script>
<script type="text/javascript" src="ext-3.3.0/mxclient-ff.js"></script>
<script type="text/javascript" src="ext-3.3.0/mxclient-ie.js"></script>
<!-- Example code -->
<script type="text/javascript">
// Program starts here. Creates a sample graph in the
// DOM node with the specified ID. This function is invoked
// from the onLoad event handler of the document (see below).
function main()
{
// Checks if the browser is supported
if (!mxClient.isBrowserSupported())
{
// Displays an error message if the browser is not supported.
mxUtils.error('Browser is not supported!', 200, false);
}
else
{
var container = document.getElementById('lala');
container.style.position = 'absolute';
container.style.overflow = 'hidden';
container.style.left = '0px';
container.style.top = '0px';
container.style.right = '0px';
container.style.bottom = '0px';
document.body.appendChild(container);
var model = new mxGraphModel();
var graph = new mxGraph(container, model);
var style = new Object();
style[mxConstants.STYLE_SHAPE] = mxConstants.SHAPE_IMAGE;
style[mxConstants.STYLE_PERIMETER] = mxPerimeter.RectanglePerimeter;
style[mxConstants.STYLE_IMAGE] = 'images/start_event_empty.png';
style[mxConstants.STYLE_IMAGE_WIDTH] = '48';
style[mxConstants.STYLE_IMAGE_HEIGHT] = '48';
style[mxConstants.STYLE_FONTCOLOR] = '#000000';
style[mxConstants.STYLE_VERTICAL_LABEL_POSITION] = mxConstants.ALIGN_CENTER;
graph.getStylesheet().putCellStyle('start-s', style);
graph.getModel().beginUpdate();
var req = mxUtils.load('mxgraph.xml');
var root = req.getDocumentElement();
var dec = new mxCodec(root);
dec.decode(root, graph.getModel());
graph.getModel().endUpdate();
}
}
</script>
</head>
<!-- Page passes the container for the graph to the grogram -->
<body onload="main();" style="margin:0px;">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div id="lala"></div></td></tr>
</table>
</body>
</html>
mxgraph.xml
<mxGraphModel>
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" style="start-s" vertex="1" parent="1">
<mxGeometry x="130" y="40" width="60" height="60" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
将附件中图片放在webRoot/images/start_event_empty.png
*******************结束的格叽格叽************************
感动比不上心动,Lysh,你心动过么?