D3 关联关系图 力学图

经常分享天眼查 企查查 的一些好的可视化图,是因为本人也是在一家相同性质公司搬砖 经常会被产品强制性要求仿做某个效果图。分享出来就是希望可以帮到大家。有问题 我看到不忙的时候我会回复。

 首先该图中信息属于公示信息 不违法也不牵扯机密。

图1 这个是仿照其查查的关联关系图做的 因为本公司数据量小 没有采用企查查做法。当然我之前也吧企查查代码拔下来了参考了下。下面图2 这个是企查查 

话不多说 先看效果图。再贴代码 本文内容不做注释 有问题可以加qq问我2557551314

css

.link {
  fill: none;
  stroke: #666;
  stroke-width: 1.5px;
}

#licensing {
  fill: green;
}

.link.licensing {
  stroke: green;
}

.link.resolved {
  stroke-dasharray: 0,2 1;
}

circle {
  fill: #ccc;
  stroke: #333;
  stroke-width: 1.5px;
}

text {
  font: 12px Microsoft YaHei;
  pointer-events: none;
  /* text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff; */
}

js 

<script>

// http://blog.thomsonreuters.com/index.php/mobile-patent-suits-graphic-of-the-day/
var links = [
    {source: "恒天中岩投资管理有限公司", target: "恒天中岩投资管理有限公司", type: "3", rela:"自身"},
    {source: "上海恒天润逸金融信息服务有限公司", target: "广东恒天财富投资管理有限公司", type: "1", rela:"股东(机构)"},
    {source: "广东恒天财富投资管理有限公司", target: "朱学锦", type: "2", rela:"法人(自然人)"},
    {source: "朱学锦", target: "恒天中岩投资管理有限公司", type: "2", rela:"相关公司"},
    // {source: "朱学锦", target: "北京实创天成资产管理有限公司", type: "3", rela:"股东"},
    // {source: "上海恒天润逸金融信息服务有限公司", target: "广东恒天财富投资管理有限公司", type: "2", rela:"股东(机构)"},
    {source: "广东恒天财富投资管理有限公司", target: "恒天融泽资产管理有限公司", type: "2", rela:"对外投资"},
    // {source: "恒天融泽资产管理有限公司", target: "广东恒天财富投资管理有限公司", type: "2", rela:"股东"},
    // {source: "恒天融泽资产管理", target: "广东恒天财富投资管理有限公司", type: "2", rela:"股东"},

    {source: "上海恒天润逸金融信息服务有限公司", target: "广东恒天财富投资管理有限公司", type: "2", rela:"股东(机构)"},
    {source: "广东恒天财富投资管理有限公司", target: "北京实创天成资产管理有限公司", type: "2", rela:"对外投资"},
    {source: "北京实创天成资产管理有限公司", target: "恒天中岩投资管理有限公司", type: "2", rela:"相同地址,相同法人"},
    {source: "恒天融泽资产管理有限公司", target: "恒天中岩投资管理有限公司", type: "2", rela:"相同地址,相同法人"},

    // {source: "朱学锦", target: "恒天中岩投资管理有限公司", type: "2", rela:"相关公司"},
    // {source: "恒天中岩投资管理有限公司", target: "恒天中岩投资管理有限公司", type: "resolved", },
];

var nodes = {};

links.forEach(function(link) {
  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});

var width = 1560,
    height = 800;
var corpname = "恒天中岩投资管理有限公司";
var force = d3.layout.force()//layout将json格式转化为力学图可用的格式
    .nodes(d3.values(nodes))//设定节点数组
    .links(links)//设定连线数组
    .size([width, height])//作用域的大小
    .linkDistance(250)//连接线长度
    .charge(-4000)//顶点的电荷数。该参数决定是排斥还是吸引,数值越小越互相排斥
    .on("tick", tick)//指时间间隔,隔一段时间刷新一次画面
    .start();//开始转换



var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height)
    // .call(zoom);

//箭头
var marker=
    svg.append("marker")
    //.attr("id", function(d) { return d; })
    .attr("id", "resolved")
    //.attr("markerUnits","strokeWidth")//设置为strokeWidth箭头会随着线的粗细发生变化
    .attr("markerUnits","userSpaceOnUse")
    .attr("viewBox", "0 -5 10 10")//坐标系的区域
    .attr("refX",59)//箭头坐标
    .attr("refY", 0)
    .attr("markerWidth", 8)//标识的大小
    .attr("markerHeight", 8)
    .attr("orient", "auto")//绘制方向,可设定为:auto(自动确认方向)和 角度值
    .attr("stroke-width",2)//箭头宽度
    .append("path")
    .attr("d", "M0,-5L10,0L0,5")//箭头的路径
    .attr('fill','#595D68');//箭头颜色

//  将连接线设置为曲线
// var path = svg.append("g").selectAll("path")
//     .data(force.links())
//     .enter().append("path")
//     .attr("class", function(d) { return "link " + d.type; })
//     .style("stroke",function(d){
//         //console.log(d);
//        return "#A254A2";//连接线的颜色
//     })
//     .attr("marker-end", function(d) { return "url(#" + d.type + ")"; });


// 设置连接线    
var edges_line = svg.selectAll(".edgepath")
    .data(force.links())
    .enter()
    .append("path")
    .attr({

          'd': function(d) {return 'M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y},
          'class':'edgepath',
          //'fill-opacity':0,
          //'stroke-opacity':0,
          //'fill':'blue',
          //'stroke':'red',
          'id':function(d,i) {return 'edgepath'+i;}})
    .style("stroke",'#595D68')
    .style("pointer-events", "none")
    .style("stroke-width",1)//线条粗细
    .attr("marker-end", function(d){
        if(d.rela=='自身'){
            return ''
        }else{
            return 'url(#resolved)'
        }
    } );//根据箭头标记的id号标记箭头

var edges_text = svg.append("g").selectAll(".edgelabel")
.data(force.links())
.enter()
.append("text")
.style("pointer-events", "none")
//.attr("class","linetext")
.attr({  'class':'edgelabel',
               'id':function(d,i){return 'edgepath'+i;},
               'dx':60,
               'dy':-5
               //'font-size':10,
               //'fill':'#aaa'
               });

//设置线条上的文字
edges_text.append('textPath')
.attr('xlink:href',function(d,i) {return '#edgepath'+i})
.style("pointer-events", "none")
.text(function(d){return d.rela;});

//圆圈
console.log(force.nodes())
var circle = svg.append("g").selectAll("circle")
    .data(force.nodes())//表示使用force.nodes数据
    .enter().append("circle")
    .style("fill",function(node){
        var color;//圆圈背景色
        var link=links[node.index];
        // console.log(link.name)
        if(link){
              console.log(link.rela)
            if(link.type == "1"){ //p2p
                return color="#C03939"; 
            }else if(link.type == "3"){
                return color="#5095FF"; 
            }else if(link.source.name.length <= 4){//人
                return color="#CD994C"; 
            }else { 
                return color = '#1943AC'
            }
        }
    })
    .style('stroke',function(node){ 
        var color;//圆圈线条的颜色
        var link=links[node.index];
        if(link){
              console.log(link.rela)
            if(link.type == "1"){ //p2p
                return color="#C03939"; 
            }else if(link.type == "3"){
                return color="#5095FF"; 
            }else if(link.source.name.length <= 4){//人
                return color="#CD994C"; 
            }else { 
                return color = '#1943AC'
            }
        }
        // return color;
    })
    .attr("r", 40)//设置圆圈半径
    // .on("click",function(node){
    //     //单击时让连接线加粗
    //     edges_line.style("stroke-width",function(line){
    //         console.log(line);
    //         if(line.source.name==node.name || line.target.name==node.name){
    //             return 4;
    //         }else{
    //             return 0.5;
    //         }
    //     });
    //     //d3.select(this).style('stroke-width',2);
    // })
    .call(force.drag);//将当前选中的元素传到drag函数中,使顶点可以被拖动


  //圆圈的提示文字
//   circle.append("svg:title")  
//         .text(function(node) { 
//             var link=links[node.index];
//             // if(node.name==link.source.name && link.rela=="主营产品"){
//             //     return "双击可查看详情"
//             // }
//          });  
var text = svg.append("g").selectAll("text")
    .data(force.nodes())
    //返回缺失元素的占位对象(placeholder),指向绑定的数据中比选定元素集多出的一部分元素。
    .enter()
    .append("text")
    .attr("dy", ".35em")  
    .attr("text-anchor", "middle")//在圆圈中加上数据  
    .style('fill',function(node){
        var color;//文字颜色
        var link=links[node.index];
        // if(node.name==link.source.name && link.rela=="主营产品"){
        //     color="#B43232";
        // }else{
        //     color="#A254A2";
        // }
        return color =  '#ffffff';
    }).attr('x',function(d){
        // console.log(d.name+"---"+ d.name.length);
        var re_en = /[a-zA-Z]+/g;
        //如果是全英文,不换行
        if(d.name.match(re_en)){
             d3.select(this).append('tspan')
             .attr('x',0)
             .attr('y',2)
             .text(function(){return d.name;});
        }
        //如果小于四个字符,不换行
        else if(d.name.length<=4){
             d3.select(this).append('tspan')
            .attr('x',-2)
            .attr('y',2)
            .text(function(){return d.name;});
        }else if(d.name.length>4&&d.name.length<=8) {//大于4  这两行
            var top=d.name.substring(0,4);
            var bot=d.name.substring(4,d.name.length);

            d3.select(this).text(function(){return '';});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',-7)
                .text(function(){return top;});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',10)
                .text(function(){return bot;});


        }else if(d.name.length>8&&d.name.length<=12){// 文字长度大于8 折三行
            var top=d.name.substring(0,4);
            var bot=d.name.substring(4,8);
            var bot1=d.name.substring(8,d.name.length);

            d3.select(this).text(function(){return '';});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',-15)
                .text(function(){return top;});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',2)
                .text(function(){return bot;});
                
            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',16)
                .text(function(){return bot1;});

        }else if(d.name.length>12 && d.name.length<=16){//文字长度大于12 折四行'

            var top=d.name.substring(0,4);
            var bot=d.name.substring(4,8);
            var bot1=d.name.substring(8,12);
            var bot2=d.name.substring(12,d.name.length);

            d3.select(this).text(function(){return '';});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',-20)
                .text(function(){return top;});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',-3)
                .text(function(){return bot;});
                
            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',10)
                .text(function(){return bot1;});
            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',23)
                .text(function(){return bot2;});


        }else if(d.name.length>16 ){//文字长度大于16  方案
            var top=d.name.substring(0,4);
            var bot=d.name.substring(4,8);
            var bot1=d.name.substring(8,12);
            var bot2=d.name.substring(12,14);

                bot2 += '...'
            d3.select(this).text(function(){return '';});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',-22)
                .text(function(){return top;});

            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',-7)
                .text(function(){return bot;});
                
            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',10)
                .text(function(){return bot1;});
            d3.select(this).append('tspan')
                .attr('x',0)
                .attr('y',25)
                .text(function(){return bot2;});
        }
        //直接显示文字    
        /*.text(function(d) { 
        return d.name; */
    })

/*  将文字显示在圆圈的外面 
var text2 = svg.append("g").selectAll("text")
     .data(force.links())
    //返回缺失元素的占位对象(placeholder),指向绑定的数据中比选定元素集多出的一部分元素。
    .enter()
    .append("text")
    .attr("x", 150)//设置文字坐标
    .attr("y", ".50em")
    .text(function(d) { 
        //console.log(d);
        //return d.name; 
        //return d.rela;
        console.log(d);
        return  '1111';
    });*/

function tick() {
  //path.attr("d", linkArc);//连接线
  circle.attr("transform", transform1);//圆圈
  text.attr("transform", transform2);//顶点文字
  //edges_text.attr("transform", transform3);
  //text2.attr("d", linkArc);//连接线文字
  //console.log("text2...................");
  //console.log(text2);
  //edges_line.attr("x1",function(d){ return d.source.x; });
  //edges_line.attr("y1",function(d){ return d.source.y; });
  //edges_line.attr("x2",function(d){ return d.target.x; });
  //edges_line.attr("y2",function(d){ return d.target.y; });
    
  //edges_line.attr("x",function(d){ return (d.source.x + d.target.x) / 2 ; });
  //edges_line.attr("y",function(d){ return (d.source.y + d.target.y) / 2 ; });


  edges_line.attr('d', function(d) { 
      var path='M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y;
      return path;
  });  
    
  edges_text.attr('transform',function(d,i){
        if (d.target.x<d.source.x){
            bbox = this.getBBox();
            rx = bbox.x+bbox.width/2;
            ry = bbox.y+bbox.height/2;
            return 'rotate(180 '+rx+' '+ry+')';
        }
        else {
            return 'rotate(0)';
        }
   });
}
//设置连接线的坐标,使用椭圆弧路径段双向编码
function linkArc(d) {
    //var dx = d.target.x - d.source.x,
  // dy = d.target.y - d.source.y,
     // dr = Math.sqrt(dx * dx + dy * dy);
  //return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
  //打点path格式是:Msource.x,source.yArr00,1target.x,target.y  
  
  return 'M '+d.source.x+' '+d.source.y+' L '+ d.target.x +' '+d.target.y
}
//设置圆圈和文字的坐标
function transform1(d) {
  return "translate(" + d.x + "," + d.y + ")";
}
function transform2(d) {
      return "translate(" + (d.x) + "," + d.y + ")";
}



</script>

假数据在js中 

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
d3关系是一种数据可视化的表,可以用来展示数据之间的关联和连接。在d3关系中,节点代表着数据的一个实体,可以是一个人、一个物品或者一个地点等等。而每个节点都有一个对应的片来表示该实体。 要在d3关系中将节点的片进行更改,首先需要准备好代表各个节点实体的片资源。可以选择符合需求和主题的标或者照片作为节点片,比如人像、物品的标、地点的照片等等。 然后,通过d3的节点设置方法,将节点的片链接或者路径指定为新的片资源。可以使用d3中的"image"或者"pattern"属性来设置节点的片样式。在属性中,可以将片源设置为节点数据中的一个属性,或者直接指定一个固定的片链接。 举个例子,假设我们有一个节点数据的数组,每个节点都有一个属性"image",代表着该节点对应的片链接。那么可以使用如下的代码将节点的片更改为新的片资源: d3.selectAll(".node") // 选择所有的节点元素 .attr("image", function(d) { return d.image; }); // 将节点的片属性设置为节点数据中的"image"属性值 以上代码使用了d3的选择器selectAll来选择所有的节点元素。然后,通过设置节点的"image"属性,将节点的片链接更改为节点数据中指定的片链接。 需要注意的是,片资源的尺寸和比例要与节点的大小和布局相匹配,以保证表的美观和准确性。另外,在d3之外可能还需要考虑片资源的加载和缓存等问题。 综上所述,通过更改d3关系中节点的片,可以将节点以更直观和个性化的方式展示出来,以增强数据可视化的效果和吸引力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值