mxgraph 之 让流程图文件(xml格式)以图的方式显示在面板下

mxgraph 之 让流程图文件(xml格式)以图的方式显示在面板上
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,你心动过么?
1 楼 qikitty 2011-09-01  
能发我一份源码么?356657939@qq.com
2 楼 lynnlysh 2011-09-02  
mxgraphShow.html
qikitty 写道
能发我一份源码么?356657939@qq.com

mxgraphShow.html 就是所需源码
新建web项目
将它和mxgraph.xml 放在WebRoot下,然后将附件中的图片放在webRoot/images/start_event_empty.png
下。
启动tomcat服务器 运行mxgraphShow.html页即可
3 楼 torch921 2011-09-09  
大虾,你好,我刚刚开始用mxGraph,有些问题想请教您,能加下QQ么?
我的是39306859
4 楼 lynnlysh 2011-09-20  
torch921 写道
大虾,你好,我刚刚开始用mxGraph,有些问题想请教您,能加下QQ么?
我的是39306859

你直接在这问吧,我如果会就回答,不会的话被真正的大虾看到也能帮你解答 ^_^
5 楼 xiaotiantian-58 2012-01-04  
楼主你好,我刚开始学习mxgraph,有些问题想请教您,加我吧,我的QQ:734373252
6 楼 lynnlysh 2012-02-02  
xiaotiantian-58 写道
楼主你好,我刚开始学习mxgraph,有些问题想请教您,加我吧,我的QQ:734373252

刚开始入门你可以从mxgraph的例子开始学,然后不会的地方看它的docs文件夹中的api文档。它的例子基本涵盖了mxgraph技术的各种应用,并且难度由浅入深。我就是这么学的,比问别人方便。就不加qq了哈 ^_^
7 楼 hty1902 2012-03-30  
楼主,我准备用mxgraph画拓扑图,能用你这种方法从数据库读出设备,然后在页面上画出来吗?
8 楼 lynnlysh 2012-05-30  
hty1902 写道
楼主,我准备用mxgraph画拓扑图,能用你这种方法从数据库读出设备,然后在页面上画出来吗?

mxgraph可以画拓扑图,但不一定用xml代码方式存在后台中.
9 楼 zxxcjb 2012-06-25  
xml里面有中文,直接解析不出来,怎怒解决呀?