六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 33|回复: 0

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

[复制链接]

升级  5.33%

14

主题

14

主题

14

主题

秀才

Rank: 2

积分
58
 楼主| 发表于 2013-2-7 16:49:44 | 显示全部楼层 |阅读模式
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 supportedif (!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  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,你心动过么?
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表