我是FE,也是Fe

          前端來(lái)源于不斷的點(diǎn)滴積累。我一直在努力。

          統(tǒng)計(jì)

          留言簿(15)

          閱讀排行榜

          評(píng)論排行榜

          基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2

          xRelation by RGraph2

          RGraph2

          RGraph是一個(gè)基于Raphaeljs完整的點(diǎn)->線圖的web實(shí)現(xiàn)。

          online demo :RGraph2 Demo

          github地址 :

          git clone https://github.com/hafeyang/rgraph2.git
          

          RGraph2已經(jīng)支持bower包規(guī)范,可以直接使用bower安裝

          bower install rgraph2
          

          由于Raphaeljs目前不支持bower規(guī)范,所以沒(méi)有引入Raphaeljs依賴

          RGraph包含特性:

          • 點(diǎn)->線圖布局算法(2.0新增)
          • 基本畫(huà)布拖拽
          • 點(diǎn)拖動(dòng),鼠標(biāo)hover高亮相關(guān)點(diǎn)
          • 支持放大/縮小/鼠標(biāo)滾輪事件
          • 提供API增/刪/改節(jié)點(diǎn)

          Quick Demo

          <script type="text/javascript" src="https://raw.github.com/hafeyang/rgraph2/master/lib/raphael-min.js"></script>
          <script type="text/javascript" src="https://raw.github.com/hafeyang/rgraph2/master/lib/rgraph2.js"></script>
          <script type="text/javascript">
          var graph = new RGraph("canvas");
          //加載數(shù)據(jù)
          graph.loadData({
          nodes:{
          //label:顯示文字,支持Raphael.fullfill格式。
          //rectStyle,textStyle 文本框的顏色,文字顏色
          "1":{"label":"{name}",name:"節(jié)點(diǎn)1",rectStyle:{fill:"#616130",stroke:"#616130"},textStyle:{fill:"#fff"}},
          "2":{"label":"節(jié)點(diǎn)2"}
          },
          edges:[
          //source,target 是節(jié)點(diǎn)的ID ,箭頭由source->target
          //arrowStyle,指定箭頭顏色
          {source:"1",target:"2",label:"1->2",arrowStyle:{fill:"#ab4",stroke:"#ab4"}}
          ]
          });
          //將ID為1的節(jié)點(diǎn)移至視口中央
          graph.center("1");
          </script>
          

          初始化參數(shù)

          var graph = new RGraph("canvas",opts);
          

          opts.drawNodeFn

          繪制節(jié)點(diǎn)的Raphaeljs擴(kuò)展方法,默認(rèn)為rgraphnode,RGraph2內(nèi)部默認(rèn)實(shí)現(xiàn),自定義節(jié)點(diǎn)繪制方法為:

          (!Raphael.fn.rgraphnode ) && (Raphael.fn.rgraphnode=function(node){
          // your implementation
          return {
          //移動(dòng)節(jié)點(diǎn)時(shí)會(huì)調(diào)用該方法
          move:function(dx,dy){},
          //高亮節(jié)點(diǎn)時(shí)會(huì)調(diào)用該方法
          highlight:function(){},
          //清除高亮
          unhighlight:function(){},
          //更新節(jié)點(diǎn)文本
          update:function(){}
          }
          });
          

          具體可以參見(jiàn)RGraph2內(nèi)部實(shí)現(xiàn)

          opts.onnodeclick(e,node)

          節(jié)點(diǎn)節(jié)點(diǎn)觸發(fā)function,參數(shù)event,node

          opts.onnodemouseover(e,node)

          節(jié)點(diǎn)mouseover觸發(fā)function,參數(shù)event,node

          opts.onnodemouseout(e,node)

          節(jié)點(diǎn)mouseout觸發(fā)function,參數(shù)event,node

          opts.nnodestartdrag(e,node)

          節(jié)點(diǎn)開(kāi)始拖拽觸發(fā)function,參數(shù)event,node

          opts.onnodedragging(e,node)

          節(jié)點(diǎn)拖拽中觸發(fā)function,參數(shù)event,node

          opts.onnodeenddrag(e,node)

          節(jié)點(diǎn)拖拽結(jié)束觸發(fā)function,參數(shù)event,node

          API

          調(diào)用方法:graphInstance.method()
          

          instance.removeNode(nodeId)

          刪除節(jié)點(diǎn)以及節(jié)點(diǎn)相關(guān)的線

          instance.updateNode(nodeId)

          更新節(jié)點(diǎn)文本

          var node = graphInstance.getNode("nodeId");
          node.label = "{name} after update";
          graphInstance.updateNode(node.nodeId);
          

          instance.getNode(nodeId)

          根據(jù)節(jié)點(diǎn)ID獲取節(jié)點(diǎn)對(duì)象

          instance.addNode(node)

          添加節(jié)點(diǎn),節(jié)點(diǎn)必須有nodeId,label屬性,rectStyle用于定義節(jié)點(diǎn)邊框樣式,樣式格式詳見(jiàn)Raphaeljs Element.attrtextStyle定義文字樣式。

          新增文字節(jié)點(diǎn)位置視口內(nèi)隨機(jī)。

          graphInstance.addNode({nodeId:"nodeId---",label:"{nodename}",nodename:"Name"});
          

          instance.existEdge(sourceNodeId,targetNodeId)

          是否存在sourceNode與targetNode之間的線,如果有返回該Edge對(duì)象,否則返回false

          instance.addEdge(edge)

          添加一條線,edge必須有source,target屬性箭頭由source->target,label屬性定義箭頭上的label文字。arrowStyle定義箭頭的顏色樣式,格式以及可以設(shè)置的屬性參見(jiàn) Raphaeljs Element.attr

          instance.loadData(graphData)

          加載圖數(shù)據(jù),數(shù)據(jù)格式見(jiàn)demo

          instance.relayout()

          重新布局點(diǎn)->線位置

          instance.getNodeEdges(nodeId)

          根據(jù)nodeId獲取節(jié)點(diǎn)相關(guān)的邊

          instane.reset()

          方法倍數(shù)設(shè)置成1。視口還原。

          instance.center(nodeId)

          將nodeId節(jié)點(diǎn)移至視口中央

          References

          Raphaeljs

          Graph JavaScript framework

          Graph visualization code in javascript

          JavaScript Graph Library Showcase

          Knows Issues

          Raphaeljs Element.getBBox方法在IE<9下獲取值不正確導(dǎo)致IE9下不能兼容


          Thanks,any questions ,contact me freely!

          posted on 2012-12-11 20:08 衡鋒 閱讀(4476) 評(píng)論(12)  編輯  收藏 所屬分類: javascriptWeb開(kāi)發(fā)

          評(píng)論

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2[未登錄](méi) 2014-05-29 14:53 chris

          您好,我想問(wèn)下,如果我想要兩個(gè)節(jié)點(diǎn)之間有兩條連線而已不重合,應(yīng)該怎么辦?  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2 2014-05-29 17:55 衡鋒

          @chris
          估計(jì)要在兩個(gè)店之間找兩個(gè)不同關(guān)鍵點(diǎn),然后分別連線就不會(huì)重合吧。  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2[未登錄](méi) 2014-06-12 11:15 chris

          @衡鋒
          多謝您!還有就是您這個(gè)畫(huà)布放大然后可以拖拽畫(huà)布是怎么實(shí)現(xiàn)的呢?  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2 2014-06-12 12:33 衡鋒

          @chris
          可以看看rgraph2的源碼。  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2[未登錄](méi) 2014-06-12 17:49 chris

          @衡鋒
          但是有一點(diǎn)不明白。您的這個(gè)getViewBox獲取的是什么數(shù)據(jù)?  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2 2014-06-13 12:35 衡鋒

          @chris
          獲取的是當(dāng)前視口在整個(gè)圖中的左上角偏差和寬高  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2[未登錄](méi) 2014-06-24 14:54 chris

          又來(lái)打擾您了。我按您的方法嘗試了拖動(dòng)整個(gè)canvas,但是如果連續(xù)拖動(dòng)的話會(huì)飄的很,您有什么方法解決嗎  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2 2014-06-24 16:41 衡鋒

          @chris
          這個(gè)時(shí)能用類似underscore里面的bounce或者throttle來(lái)解決了。  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2 2014-06-25 16:08 TIMI

          請(qǐng)教下,我要是添加圖片該怎么添加呢?我用src不管用呀!  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2 2014-06-25 16:51 TIMI

          在您rgraph2中添加節(jié)點(diǎn),要是想添加圖片改怎么添加呢?我試了src屬性沒(méi)有效果。  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2 2014-06-27 12:29 衡鋒

          @TIMI
          看Raphaeljs的文檔  回復(fù)  更多評(píng)論   

          # re: 基于raphaeljs的web圖實(shí)現(xiàn)-rgraph2[未登錄](méi) 2015-08-03 18:53 aa

          刪除節(jié)點(diǎn)不好用  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 嘉荫县| 区。| 永春县| 沾益县| 仁化县| 云梦县| 乌兰浩特市| 方城县| 大城县| 扶绥县| 三河市| 怀宁县| 马山县| 武清区| 兴安盟| 北安市| 化隆| 海宁市| 尤溪县| 汝州市| 滨州市| 华安县| 藁城市| 广汉市| 灌阳县| 繁峙县| 绥德县| 寿宁县| 林芝县| 长丰县| 保山市| 宜兴市| 商南县| 巴彦县| 富顺县| 聂荣县| 呼图壁县| 扬中市| 吉水县| 兖州市| 龙州县|