最近的工作与可视化有关,有展示血缘关系树的需求 ,类似于这样:

碰巧搜到 D3(用于可视化的js库,作者吕之华),瞬间无法自拔,它的树状图功能基于SVG、js ,暴露的可操作入口也简洁恰当,能帮助你快速完成svg开发。

D3的使用:  入门教程:http://wiki.jikexueyuan.com/project/d3wiki/author.html

开发笔记:

1. SVG DOM 与HTML DOM 不一样,属性、样式是不共用的,但也有相似之处

2. Svg中一段文本用<text>标签,如果要集中控制多个元素用<g>(group)标签

3. 钩子:

  1. nodeEnter nodeUpdate nodeExit
  1. var flare = {
  2. "name": "前端应用",
  3. "children": [
  4. {
  5. "name": "服务1",
  6. "children": [
  7. {
  8. "name": "cluster",
  9. "children": [
  10. {"name": "AgglomerativeCluster", "size": 3938}
  11. ]
  12. },
  13. {
  14. "name": "graph",
  15. "tablename" : "public.conf_hanzhi_pinyin",
  16. "dataowner" :"hippopMan",
  17. "datadeveloper":"hippopMan",
  18. "url":"/biReport/report/aabbcc.html",
  19. "children": [
  20. {"name": "BetweennessCentrality", "size": 3534},
  21. {"name": "LinkDistance", "size": 5731}
  22. ]
  23. },
  24. {
  25. "name": "optimization",
  26. "children": [
  27. {"name": "AspectRatioBanker", "size": 7074}
  28. ]
  29. }
  30. ]
  31. },
  32. {
  33. "name": "服务2",
  34. "children": [
  35. {"name": "Easing", "size": 17010},
  36. {"name": "FunctionSequence", "size": 5842}
  37. ]
  38. }
              ........
  39. ]
  40. };

  

具体代码:

  1. var margin = {top: 20, right: 120, bottom: 20, left: 120},
  2. width = 3200 - margin.right - margin.left,
  3. height = 1200 - margin.top - margin.bottom;
  4.  
  5. var i = 0,
  6. duration = 750,
  7. root;
  8.  
  9. var tree = d3.layout.tree()
  10. .size([height, width]);
  11.  
  12. var diagonal = d3.svg.diagonal()
  13. .projection(function(d) { return [d.y, d.x]; });
  14.  
  15. var svg = d3.select(".canvan").append("svg")
  16. .attr("width", width + margin.right + margin.left)
  17. .attr("height", height + margin.top + margin.bottom)
  18. .append("g")
  19. .attr("transform", "translate(" + 80 + "," + 20 + ")");
  20.  
  21. function collapse(d) {
  22.  
  23. if (d.children) {
  24. d._children = d.children;
  25. d._children.forEach(collapse);
  26. d.openFlag = false; //自定义属性
  27. }else{
  28. if(searchFlag){
  29. d.openFlag = false;
  30. }else{
  31. if(reportKey){
  32. d.openFlag = true;
  33. }
  34.  
  35. }
  36.  
  37. }
  38. }
  39.  
  40. function update(source) {
  41.  
  42. // Compute the new tree layout转换数据
  43. var nodes = tree.nodes(root).reverse(),
  44. links = tree.links(nodes);
  45.  
  46. // Normalize for fixed-depth.
  47. nodes.forEach(function(d) { d.y = d.depth * 240; });
  48.  
  49. // Update the nodes…
  50. var node = svg.selectAll("g.node")
  51. .data(nodes, function(d) { return d.id || (d.id = ++i); });
  52.  
  53. // Enter any new nodes at the parent's previous position.
  54. var nodeEnter = node.enter().append("g")
  55. .attr("class", "node")
  56. .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }); //css3属性 二维动画
  57.  
  58. var htm = "<div class='infoBox'>123</div>";
  59. //写文字的变迁
  60. nodeEnter.append("circle") //画圆
  61. .attr("x","0px")
  62. .attr("y", "10px") //x,y代表坐标,注意在group元素中,坐标是指在该元素中的相对坐标
  63. .attr("cx","0px")
  64. .attr("cy", "0px")
  65. .attr("dy", ".35em")
  66. .attr("text-anchor", "middle")
  67. .style("fill-opacity", 1) //透明度 显示隐藏用display(none,block)
  68. .style("fill", function (d) { //填充颜色
  69. return d.openFlag ? "#fff":"#12a566"
  70. })
  71. .on("click", click) //给元素添加事件
  72. .on("mouseover",hover);
  73.  
  74. nodeEnter.append("text") //text标签
  75. .attr("class", "tablename")
  76. .attr("x","0px")
  77. .attr("y", "15px")
  78. .attr("dy", ".35em")
  79. .attr("text-anchor", "middle")
  80. .attr("width","60")
  81. .attr("height","45")
  82. .text(function(d) { return "表名" + d.tablename; }) //文字
  83. .style("display",showTableName?"block":"none")
  84. .style("fill-opacity", 1e-6);
  85. ......
  86. // Transition nodes to their new position.
  87. var nodeUpdate = node.transition()
  88. .duration(duration)
  89. .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
  90.  
  91. nodeUpdate.select("circle")
  92. .attr("r", 4.5)
  93. .style("fill", function(d) { return d.openFlag ? "#fff":"#12a566" });
  94.  
  95. nodeUpdate.select("text")
  96. .style("fill-opacity", 1);
  97.  
  98. // Transition exiting nodes to the parent's new position.
  99. var nodeExit = node.exit().transition()
  100. .duration(duration)
  101. .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
  102. .remove();
  103.  
  104. nodeExit.select("circle")
  105. .attr("r", 1e-6);
  106.  
  107. nodeExit.select("text")
  108. .style("fill-opacity", 1e-6);
  109.  
  110. // Update the links…
  111. var link = svg.selectAll("path.link")
  112. .data(links, function(d) { return d.target.id; });
  113.  
  114. // Enter any new links at the parent's previous position.
  115. link.enter().insert("path", "g")
  116. .attr("class", "link")
  117. .attr("d", function(d) {
  118. var o = {x: source.x0, y: source.y0};
  119. return diagonal({source: o, target: o});
  120. });
  121.  
  122. // Transition links to their new position.
  123. link.transition()
  124. .duration(duration)
  125. .attr("d", diagonal);
  126.  
  127. // Transition exiting nodes to the parent's new position.
  128. link.exit().transition()
  129. .duration(duration)
  130. .attr("d", function(d) {
  131. var o = {x: source.x, y: source.y};
  132. return diagonal({source: o, target: o});
  133. })
  134. .remove();
  135.  
  136. // Stash the old positions for transition.
  137. nodes.forEach(function(d) {
  138. d.x0 = d.x;
  139. d.y0 = d.y;
  140. });
  141. }
  142.  
  143. // Toggle children on click.
  144. function click(d) {
  145. if(d.childHasShowed){//已经加载出child
  146. //收起 d.children 展开 d._children;
  147. if (d.children) {
  148. d._children = d.children;
  149. d.children = null;
  150. d.openFlag = false;
  151. } else {
  152. d.children = d._children;
  153. d._children = null;
  154. d.openFlag = true;
  155. }
  156. update(d);
  157. }else{
  158.  
  159. //点击加载child
  160. getDepenInfo(d.tablename, true, true ,d);
  161. // d.children = obj.children;
  162. // update(d);
  163. }
  164. }
  165.  
  166. function hover(d) {
  167. console.log("over")
  168. }

SVG--D3--血缘关系树的更多相关文章

  1. 用D3.js画树状图

    做项目遇到一个需求,将具有层级关系的词语用树状图的形式展示它们之间的关系,像这样: 或者是这样: 上面的图片只是样例,跟我下面的代码里面用的数据不同 网上有很多这种数据可视化展示的js控件,我这里选择 ...

  2. 基于spark logicplan的表血缘关系解析实现

    随着公司平台用户数量与表数量的不断增多,各种表之间的数据流向也变得更加复杂,特别是某个任务中会对源表读取并进行一系列复杂的变换后又生成新的数据表,因此需要一套表血缘关系解析机制能清晰地解析出每个任务所 ...

  3. svg + d3

    为了实现元素的添加,删除,拖拽,左键点击,右键单击,悬浮等功能,使用了d3 + svg 的技术来实现界面. 最开始是采用canvas,但是由于功能原因放弃了该技术,可以看下 canvas简介 另附:c ...

  4. d3生成的树状图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 基于MaxCompute InformationSchema进行血缘关系分析

    一.需求场景分析 在实际的数据平台运营管理过程中,数据表的规模往往随着更多业务数据的接入以及数据应用的建设而逐渐增长到非常大的规模,数据管理人员往往希望能够利用元数据的分析来更好地掌握不同数据表的血缘 ...

  6. 一款好用的数据血缘关系在线工具--SQLFlow

      l  数据血缘关系(data lineage) 数据血缘属于数据治理中的一个概念,是在数据溯源的过程中找到相关数据之间的联系,它是一个逻辑概念.数据治理中经常提到血缘分析,血缘分析是保证数据融合的 ...

  7. 血缘关系分析工具SQLFLOW--实践指南

    SQLFlow 是用于追溯数据血缘关系的工具,它自诞生以来以帮助成千上万的工程师即用户解决了困扰许久的数据血缘梳理工作. 数据库中视图(View)的数据来自表(Table)或其他视图,视图中字段(Co ...

  8. 使用grabit分析mysql数据库中的数据血缘关系

    使用grabit分析mysql数据库中的数据血缘关系 Grabit 是一个辅助工具,用于从数据库.GitHub 等修订系统.bitbucket 和文件系统等各种来源收集 SQL 脚本和存储过程,然后将 ...

  9. d3实现家族树

      1.  jQuery和CSS3支持移动手机的DOM元素移动和缩放插件:panzoom   2.拖动:jqueryUI-Draggable.touchpunch   3.图表:echart.heig ...

随机推荐

  1. centos7下安装fabric2.2

    准备基础环境 1.安装curl.git yum install curl yum install git 2.go环境搭建 下载解压 cd /home mkdir app cd app wget ht ...

  2. node-macaddress

    下载 node-macaddressnode-macaddress 检索Linux.OS X和Windows中的MAC地址. 关于MAC地址的一个常见误解是,每个主机只有一个MAC地址, 虽然一个主机 ...

  3. 第五周:面向对象部分内容总结(5)---java设计规则

    面向对象设计原则 1.开闭原则 开闭原则理解: 简单说就是一个软件实体支持扩展,不支持修改.就是在不改变源码的基础上,扩展其它的功能. 其实笔者认为,开闭原则无非就是想表达这样一层意思:用抽象构建框架 ...

  4. Java虚拟机诊断利器

    Java虚拟机诊断利器  

  5. 如何免费安装正版Adobe

    现在正版的Adobe都非常的贵,如果你想不花钱又想下载正版的Adobe,那么就请花几分钟时间学习以下本篇博客,告诉你如何免费下载正版Adobe! [一定要读完,不要看到一半就以为教您下载的是付费版] ...

  6. 安装两个Eclipse 版本不一致,高版本无法打开

    Could not create the JavaVirtual Machine,A fatal exception has occurred. 首先删除了 工作空间的配置 然后删除掉C:\Windo ...

  7. turtle库元素语法分析

    一.turtle原理理解: turtle库是Python中一个有趣的图形绘制函数库.原名(海龟),我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形. 对于小海龟 ...

  8. 多测师讲解python _练习题003_高级讲师肖sir

    python 003作业题:# 1.分别打印100以内的所有偶数和奇数并存入不同的列表当中# 2.请写一段Python代码实现删除一个list = [1, 3, 6, 9, 1, 8]# 里面的重复元 ...

  9. Windows下CertUtil校验和编码文件

    目录 前言 CertUtil计算文件hash 计算MD2 计算MD4 计算MD5 计算SHA1 计算SHA256 计算SHA384 计算SHA512 文件base64编码 文件base64解码 文件h ...

  10. 资源管理神器Clover

    开开心心地上班,这时你得打开我的电脑,点进D盘,打开某个项目;然后还得打开XX文档,还有.... 最后的最后,你的桌面便成了这个样子 每天你都得天打开多个文件夹,切换时找文件找的晕头转向而烦恼. 每天 ...