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

碰巧搜到 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. 钩子:

nodeEnter  nodeUpdate  nodeExit
var flare = {
"name": "前端应用",
"children": [
{
"name": "服务1",
"children": [
{
"name": "cluster",
"children": [
{"name": "AgglomerativeCluster", "size": 3938}
]
},
{
"name": "graph",
"tablename" : "public.conf_hanzhi_pinyin",
"dataowner" :"hippopMan",
"datadeveloper":"hippopMan",
"url":"/biReport/report/aabbcc.html",
"children": [
{"name": "BetweennessCentrality", "size": 3534},
{"name": "LinkDistance", "size": 5731}
]
},
{
"name": "optimization",
"children": [
{"name": "AspectRatioBanker", "size": 7074}
]
}
]
},
{
"name": "服务2",
"children": [
{"name": "Easing", "size": 17010},
{"name": "FunctionSequence", "size": 5842}
]
}
          ........
]
};

  

具体代码:

var margin = {top: 20, right: 120, bottom: 20, left: 120},
width = 3200 - margin.right - margin.left,
height = 1200 - margin.top - margin.bottom; var i = 0,
duration = 750,
root; var tree = d3.layout.tree()
.size([height, width]); var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; }); var svg = d3.select(".canvan").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + 80 + "," + 20 + ")"); function collapse(d) { if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.openFlag = false; //自定义属性
}else{
if(searchFlag){
d.openFlag = false;
}else{
if(reportKey){
d.openFlag = true;
} } }
} function update(source) { // Compute the new tree layout转换数据
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes); // Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 240; }); // Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); }); // Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }); //css3属性 二维动画 var htm = "<div class='infoBox'>123</div>";
//写文字的变迁
nodeEnter.append("circle") //画圆
.attr("x","0px")
.attr("y", "10px") //x,y代表坐标,注意在group元素中,坐标是指在该元素中的相对坐标
.attr("cx","0px")
.attr("cy", "0px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.style("fill-opacity", 1) //透明度 显示隐藏用display(none,block)
.style("fill", function (d) { //填充颜色
return d.openFlag ? "#fff":"#12a566"
})
.on("click", click) //给元素添加事件
.on("mouseover",hover); nodeEnter.append("text") //text标签
.attr("class", "tablename")
.attr("x","0px")
.attr("y", "15px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("width","60")
.attr("height","45")
.text(function(d) { return "表名" + d.tablename; }) //文字
.style("display",showTableName?"block":"none")
.style("fill-opacity", 1e-6);
......
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }); nodeUpdate.select("circle")
.attr("r", 4.5)
.style("fill", function(d) { return d.openFlag ? "#fff":"#12a566" }); nodeUpdate.select("text")
.style("fill-opacity", 1); // Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove(); nodeExit.select("circle")
.attr("r", 1e-6); nodeExit.select("text")
.style("fill-opacity", 1e-6); // Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; }); // Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
}); // Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal); // Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove(); // Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
} // Toggle children on click.
function click(d) {
if(d.childHasShowed){//已经加载出child
//收起 d.children 展开 d._children;
if (d.children) {
d._children = d.children;
d.children = null;
d.openFlag = false;
} else {
d.children = d._children;
d._children = null;
d.openFlag = true;
}
update(d);
}else{ //点击加载child
getDepenInfo(d.tablename, true, true ,d);
// d.children = obj.children;
// update(d);
}
} function hover(d) {
console.log("over")
}

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. VS2015如何调试自己写的DLL与调试

    转载: 1. https://blog.csdn.net/u014738665/article/details/79779632 2. https://blog.csdn.net/jacke121/a ...

  2. Communicating to 2 SPI Slaves with USART & SPI ports on Atmega16U2

    原文来自:https://www.avrfreaks.net/comment/2236256 I'm writing code for an embedded chip that consists o ...

  3. 安装memcache,步骤

    1.先下载emcache.rar安装包<ps:随便安装在哪里文件夹,或者新建文件夹都是可以的> 2.解压安装包<ps:运行里面的程序> 3.然后根据自己的phpstudy的版本 ...

  4. git 本地回滚到上一个版本

    linux下是 git reset --hard HEAD^ windows下是 git reset --hard HEAD"^"

  5. 【博弈论】CF 1215D Ticket Game

    题目大意 洛谷链接 给出一个长度为\(n\)的由数字组成的字符串(\(n\)是偶数).但可能有偶数个位上的数字为?. 现在有两个人\(A\)和\(B\),在?的位置上填\(0\)~\(9\)的数,一直 ...

  6. redis 各种数据结构的encoding实现

    redis 各种数据结构的encoding实现 Redis type命令实际返回的就是当前键的数据结构类型,它们分别是:string(字符串).hash(哈希).list(列表).set(集合).zs ...

  7. 【应用服务 App Service】在Azure App Service中使用WebSocket - PHP的问题 - 如何使用和调用

    问题描述 在Azure App Service中,有对.Net,Java的WebSocket支持的示例代码,但是没有成功的PHP代码. 以下的步骤则是如何基于Azure App Service实现PH ...

  8. Git Push 避免输入用户名和密码方法

    1 创建文件存储GIT用户名和密码 在%HOME%目录中,一般为C:\users\Administrator,也可以是你自己创建的系统用户名目录,反正都在C:\users\中.文件名为.git-cre ...

  9. hasmap hashtable

    hashMap和hashtable方面的知识点吧: 1. 关于HashMap的一些说法: a) HashMap实际上是一个"链表散列"的数据结构,即数组和链表的结合体.HashMa ...

  10. 18级北航软件学院算法复习--Samshui

    A 比特手链 简单模拟 判断 贪心 叶姐要想哥赠送一串比特手链,这个手链由0和1组成.想哥买了手链B,无意间得知叶姐想要同样长度的手链A.想哥囊中羞涩,只能手工调整手链.他希望最少通过以下操作进行最少 ...