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.                + "\n下級設備2: "+cell.downDevice2     
  5.      + "\n下級設備3: "+cell.downDevice3  
  6.      + "\n下級設備數: "+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. };  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM