mxGraph 学习笔记(一)

学习mxgraph一段时间了,列一些常用代码段,于己于人都方便

设置节点在线上(即:线和元素重叠式,线被节点遮住)

Java代码  收藏代码
  1. graph.selectEdges();//选中所有的线  
  2. graph.orderCells(true);//使线在所有元素的底下   
  3. graph.clearSelection();//取消选中的元素  

设置画布背景图片

Java代码  收藏代码
  1. var img = new mxImage(imageSrc,1280 ,1024);  // w:1280   h:1024  
  2. graph.setBackgroundImage(img);   
  3. graph.view.validate();  

  

自定义ToolTip

Java代码  收藏代码
  1. graph.setTooltips(true);  
  2. graph.getTooltipForCell = function(cell){  
  3.     return "下级设备1:"+cell.downDevice1       
  4.                + " 下级设备2: "+cell.downDevice2     
  5.      + " 下级设备3: "+cell.downDevice3  
  6.      + " 下级设备数: "+cell.downDeviceNum;  
  7. }  

事件

Java代码  收藏代码
  1. //移动元素触发事件  
  2. graph.addListener(mxEvent.CELLS_MOVED,function(sender, evt){  
  3.     //alert("CELLS_MOVED");     
  4.     var cell = evt.getProperty('cell');   
  5.                       
  6.     if(cell==null&&sender.graphHandler.cells!=null){  
  7.         cell = sender.graphHandler.cells[0];//保证cell有值,否则移动时cell  
  8.     }   
  9.                
  10.     if(cell != null && cell.vertex == 1) {//代表鼠标点击的是节点  
  11.         //alert("移动节点"+cell.id);      
  12.         cell.autoSaveNode = '1';//给cell节点增加一个自定义属性,标识处于可保存状态     
  13.     }    
  14. });  

更新指定节点图片,可配合ajax无刷新实现告警时自动闪烁

Java代码  收藏代码
  1. graph.getModel().beginUpdate();  
  2. try{  
  3.     for (var i = 0; i < nodelist.length; i++) {  
  4.                       
  5.         //alert(nodelist[i].deviceid+1);  
  6.         var cellId = nodelist[i].deviceid+1;  
  7.         var picUrl = "";  
  8.         //alert(cellId);   
  9.         if(nodelist[i].sr_alarmsum>0)  picUrl = "red.gif";    
  10.         else if (nodelist[i].sw_alarmsum >0)  picUrl = "orange.gif";                   
  11.         var cell = graph.getModel().getCell(cellId);   
  12.         // Updates the cell color and adds some tooltip information  
  13.         if (cell != null) {     
  14.             graph.setCellStyles(mxConstants.STYLE_IMAGE, "image;image="+picUrl, [cell]);   
  15.         }    
  16.     }  
  17.                   
  18. finally {  
  19.     graph.getModel().endUpdate();  
  20.     //alert("ol1");   
  21. }   

设置画布只能预览,禁止拖动或点击

Java代码  收藏代码
  1. graph.setEnabled(false);//graph只能预览  
  2. graph.getCursorForCell = function(cell){//预览时鼠标悬浮到节点时,改变鼠标样式  
  3.     if (cell != null && cell.value != null && cell.vertex ==1 )  
  4.     {  
  5.            return 'pointer';  
  6.     }  
  7. };  
原文地址:https://www.cnblogs.com/CoffeeHome/p/3544961.html