一、介绍:
Raphael,一个开源和轻量级的JS库,它使用SVG和VML在浏览器中绘制矢量图形。 它提供了很多非常方便的用户绘制和变换矢量图的方法,在支持直接操作DOM的同时还支持导入位图以及文本的绘制。它就是SVG和VML的优秀合体。一位内它已经将底层封装完善,我们在使用时不需要去考虑SVG和VML的方面问题,只要集中精力搞好我们的Raphael就可以了。
二、本文demo实现效果
实现一个简单的横向和纵向的图排列
三、代码:
1、先开发一个process_manager.jsp页面
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<%@include file="/coframe/tools/skins/common.jsp" %>
<!--
- Author(s): wanghl
- Date: 2013-02-28 13:45:18
- Description:
-->
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="<%=request.getContextPath()%>/raphael/js/raphael-min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/raphael/js/raphael_extend.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/raphael/js/renhe.js"></script>
<title>SOP环节展示</title>
</head>
<body oncontextmenu=self.event.returnValue = false onselectstart="return false">
<style>
#table1 .tit{
height: 10px;
line-height: 10px;
}
#table1 td{
height: 10px;
line-height: 10px;
}
</style>
<div class="mini-splitter" allowResize="true" style="width:100%;height:100%;">
<div size="30%" showCollapseButton="false" style="padding:5px;">
<div id="canvas" style="width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; bottom: 0px; right: 0px;"></div>
</div>
<div showCollapseButton="false">
<input id="text1" class="nui-textarea" style="width: 100%; height: 100%;position: absolute;top: 0px; left: 0px; bottom: 0px; right: 0px;"/>
</div>
</div>
<script type="text/javascript">
nui.parse();
$_renhe.load(101);//竖向展示
$_renhe.load_horizontal(101);//横向展示
function showSOPData(sopID){
alert("ID2:"+sopID);
nui.get("text1").setValue(sopID);
}
</script>
</body>
</html>
2、再开发一个renhe.js文件
//申明工作空间
var renhe = renhe || {};
renhe.workspace = {};
var $_renhe = renhe.workspace;
//创建一个节点对象
$_renhe.SOPNode = function(x,y,width,height,Paper,xid,url,tip){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.Paper = Paper;
this.xid = xid;
this.url = url;
this.tip = tip;
this.show = function(){
var sopID = this.xid;
var rectR = Paper.rect(this.x, this.y, this.width, this.height, 10);
rectR.attr({
'stroke-width': 1,
fill:"url('"+this.url+"')"
});
rectR.node.onclick = function(evt){
/*debugger;
if(1==evt.which){//左键单击
alert("左键");
}else if(3==evt.which){//右键单击
alert("右键");
}*/
showSOPData(sopID);};
var titleLen = tip.length;
var temStr ="";
var i;
for(i=0;i<titleLen/7;i++){
temStr = temStr+this.tip.substr(i*7,7)+"\n";
}
var title = Paper.text(this.x+this.width+10, this.y+this.height/2, temStr);
title.attr({'font-size':15, fill:'black', 'text-anchor':'start'});
}
};
//创建一个水平节点对象
$_renhe.SOPNode_horizontal = function(x,y,width,height,Paper,xid,url,tip){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.Paper = Paper;
this.xid = xid;
this.url = url;
this.tip = tip;
this.show = function(){
var sopID = this.xid;
var rectR = Paper.rect(this.x, this.y, this.width, this.height, 10);
rectR.attr({
'stroke-width': 1,
fill:"url('"+this.url+"')"
});
rectR.node.onclick = function(evt){
/*debugger;
if(1==evt.which){//左键单击
alert("左键");
}else if(3==evt.which){//右键单击
alert("右键");
}*/
showSOPData(sopID);};
var titleLen = tip.length;
var temStr ="";
var i;
for(i=0;i<titleLen/7;i++){
temStr = temStr+this.tip.substr(i*7,7)+"\n";
}
var title = Paper.text(this.x, this.y+this.height+30, temStr);
title.attr({'font-size':15, fill:'black', 'text-anchor':'start'});
}
};
//创建一个连线对象
$_renhe.SOPLine = function(x1,y1,x2,y2,width,Paper){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.width = width;
this.show = function(){
Paper.path("M"+this.x1+" "+this.y1+"L"+this.x2+" "+this.y2+"").attr({
'arrow-end': 'classic-wide-long',
stroke: "#0000ff",
"stroke-width": this.width
});
}
};
//创建一个开始节点对象
$_renhe.SOPStart = function(x,y,r,color,Paper){
this.x = x;
this.y =y;
this.r = r;
this.color = color;
this.show = function(){
Paper.circle(this.x,this.y,this.r).attr({fill: this.color});
}
};
//创建一个结束节点对象
$_renhe.SOPEnd = function(x,y,r,color,Paper){
this.x = x;
this.y =y;
this.r = r;
this.color = color;
this.show = function(){
Paper.circle(this.x,this.y,this.r).attr({fill: this.color});
}
};
//纵向加载流程图函数
$_renhe.load = function(xid){
$.ajax({
url:"com.renhe.raphael.renhe.QuerySOP.biz.ext",
type:'POST',
data:"{xid:"+xid+"}",
cache: false,
contentType:'text/json',
success:function(text){
var returnJson = nui.decode(text.results);
if(returnJson.exception == null){
var offsetHeight = document.getElementById("canvas").offsetHeight;
var offsetWidth =document.getElementById("canvas").offsetWidth;
if(offsetHeight<900){offsetHeight=900;}
debugger;
var Paper = Raphael("canvas",300,offsetHeight);
$_renhe.draw(80,20,returnJson,Paper);
}else{
alert("数据查询异常");
}
}
});
};
//横向加载流程图函数
$_renhe.load_horizontal = function(xid){
$.ajax({
url:"com.renhe.raphael.renhe.QuerySOP.biz.ext",
type:'POST',
data:"{xid:"+xid+"}",
cache: false,
contentType:'text/json',
success:function(text){
var returnJson = nui.decode(text.results);
if(returnJson.exception == null){
var offsetHeight = document.getElementById("canvas").offsetHeight;
var offsetWidth =document.getElementById("canvas").offsetWidth;
if(offsetWidth<1600){offsetWidth=1600;}
debugger;
var Paper = Raphael("canvas",offsetWidth,300);
$_renhe.draw_horizontal(80,80,returnJson,Paper);
}else{
alert("数据查询异常");
}
}
});
};
$_renhe.draw =function(x,y,json,Paper){
debugger;
this.Paper = Paper;
var i ;
var r =14;
var resultLen = json.length;
new $_renhe.SOPStart(x,y,r,"#00C000",this.Paper).show();
for(i=0;i<resultLen;i++){
var data = json[i];
var imageUrl = "";
var width = 64;
var height = 44;
var jiange =40;
var tempY = jiange+y+jiange*2*(i);
if(i==0){
new $_renhe.SOPLine(x,y+r,x,tempY,2,this.Paper).show();
}else{
new $_renhe.SOPLine(x,jiange+y+jiange*2*(i-1)+height,x,tempY,2,this.Paper).show();
}
if(data.isok=="Y"){
imageUrl="../images/manual_7_0.gif";
}else{
imageUrl="../images/manual_1.gif";
}
new $_renhe.SOPNode(x-width/2, tempY, width, height,this.Paper,data.sortno,imageUrl,data.title).show();
if(i==resultLen-1){
new $_renhe.SOPLine(x,tempY+height,x,jiange+y+jiange*2*(resultLen)-r,2,this.Paper).show();
}
}
new $_renhe.SOPEnd(x,jiange+y+jiange*2*(resultLen),r,"#00C000",this.Paper).show();
};
$_renhe.draw_horizontal =function(x,y,json,Paper){
debugger;
this.Paper = Paper;
var i ;
var r =14;
var resultLen = json.length;
new $_renhe.SOPStart(x,y,r,"#00C000",this.Paper).show();
for(i=0;i<resultLen;i++){
var data = json[i];
var imageUrl = "";
var width = 64;
var height = 44;
var jiange =70;
var tempX = jiange+x+jiange*2*(i);
if(i==0){
new $_renhe.SOPLine(x+r,y,jiange+x+jiange*2*(i),y,2,this.Paper).show();
}else{
new $_renhe.SOPLine(x+jiange*i+width*i,y,x+jiange*(i+1)+width*i,y,2,this.Paper).show();
}
if(data.isok=="Y"){
imageUrl="../images/manual_7_0.gif";
}else{
imageUrl="../images/manual_1.gif";
}
new $_renhe.SOPNode_horizontal(x+jiange*(i+1)+width*i, y-height/2, width, height,this.Paper,data.sortno,imageUrl,data.title).show();
if(i==resultLen-1){
new $_renhe.SOPLine(x+jiange*(i+1)+width*(i+1),y,x+jiange*(i+2)+width*(i+1)-r,y,2,this.Paper).show();
}
}
new $_renhe.SOPEnd(x+jiange*(resultLen+1)+width*resultLen,y,r,"#00C000",this.Paper).show();
};
$_renhe.showSOPData = function(sopID){
alert("ID:"+sopID);
}
3、项目结构如下
四、结论
五、参照:
[https://www.cnblogs.com/idealer3d/p/LearningRaphaelJSVectorGraphics.html]