分享

mxgraph

 我心永恒lz 2015-01-27

东西学得杂乱无章..没有一个可以深入的..很无奈,更无奈的是这东西基本没有文档..没办法工作要用到..也好,看看大牛们怎么在web里实际应用<图论>的.

mxgraph的第一天,加油..

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www./1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>无标题文档</title>  
  6. <script type="text/javascript" src="../mxGraph-1.7.1.6.debug.js"></script>  
  7. </head>  
  8. <body>  
  9. <div id="ct" style="overflow:hidden;width:321px;height:241px;background:url('editors/images/grid.gif');cursor:default;"></div>  
  10. </body>  
  11. <script type="text/javascript">  
  12.     (function main(container) {  
  13.         if(!mxClient.isBrowserSupported()) {  
  14.             alert('1');  
  15.         } else {  
  16.             //container承装graph的容器  
  17.             //graph相当于画布  
  18.             var graph = new mxGraph(container);  
  19.             graph.setHtmlLabels(true);  
  20.               
  21.             var keyHandler = new mxKeyHandler(graph);  
  22.             //辅助方法,得到鼠标事件选定的是哪个value  
  23.             var getFieldnameFormEvent = function(cell, evt) {  
  24.                 if (evt != null) {  
  25.                     //将鼠标相对于游览器视口的坐标转换为相对于graph容器的坐标  
  26.                     var point = mxUtils.convertPoint(graph.container, evt.clientX, evt.clientY)  
  27.                     var state = graph.getView().getState(cell);  
  28.                     if (state != null) {  
  29.                         //减去cell的x,y  
  30.                         point.x -= state.x;  
  31.                         point.y -= state.y;  
  32.                         //鼠标事件发生在大于cell高度一半的地方,选定第二个值  
  33.                         if (point.y > state.height / 2) {  
  34.                             return 'second';  
  35.                         }  
  36.                     }  
  37.                 }  
  38.                 return 'first';  
  39.             };  
  40.         //@Override  
  41.         graph.getLabel = function(cell) {  
  42.            var table = document.createElement('table');  
  43.                     table.style.height = '100%';  
  44.                     table.style.width = '100%';  
  45.                       
  46.                     var body = document.createElement('tbody');  
  47.                     var tr1 = document.createElement('tr');  
  48.                     var td1 = document.createElement('td');  
  49.                     td1.style.fontSize = '12px';  
  50.                     //写入  
  51.                     mxUtils.write(td1, cell.value.first);  
  52.                       
  53.                     var tr2 = document.createElement('tr');  
  54.                     var td2 = document.createElement('td');  
  55.                     td2.style.fontSize = '12px';  
  56.                     mxUtils.write(td2, cell.value.second);  
  57.                       
  58.                     tr1.appendChild(td1);  
  59.                     tr2.appendChild(td2);  
  60.                     body.appendChild(tr1);  
  61.                     body.appendChild(tr2);  
  62.                     table.appendChild(body);  
  63.                       
  64.                     return table;  
  65.         };  
  66.         //@Override  
  67.         graph.getEditingValue = function(cell, evt) {  
  68.             evt.fieldname = getFieldnameFormEvent(cell, evt);  
  69.               
  70.             return cell.value[evt.fieldname] || '';  
  71.         };  
  72.         //@Override  
  73.         graph.labelChanged = function(cell, newValue, trigger) {  
  74.             var name = (trigger != null) ? trigger.fieldname : null;  
  75.             if (name != null) {  
  76.                 //不要再原值上修改  
  77.                 var value = mxUtils.clone(cell.value);  
  78.                 value[name] = newValue;  
  79.                 newValue = value;  
  80.                   
  81.                 mxGraph.prototype.labelChanged.apply(this, arguments);  
  82.             }  
  83.         };  
  84.           
  85.         var value = {  
  86.             first: '第一个值',  
  87.             second: '第二个值'  
  88.         };  
  89.           
  90.         var parent = graph.getDefaultParent();  
  91.         graph.getModel().beginUpdate();  
  92.         try {  
  93.             //插入一个顶点  
  94.             var v1 = graph.insertVertex(parent, null, value, 100, 60, 120, 80, 'overflow=fill;');  
  95.         } finally {  
  96.             //更新视图  
  97.             graph.getModel().endUpdate();  
  98.         }  
  99.     }  
  100.     })(document.getElementById('ct'));  
  101. </script>  
  102. </html>  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多