D3.js实现随机分布球形内含文字

在这里插入图片描述

var width = 700,
    height = 300;
var data = [{"name":"河北","val":"62356"},
			{"name":"河南","val":"89565"},
			{"name":"广东","val":"63658"},
			{"name":"江西","val":"68525"},
			{"name":"上海","val":"36585"},
			{"name":"广西","val":"66589"},
			{"name":"云南","val":"86955"},
			{"name":"江苏","val":"75565"},
			{"name":"陕西","val":"73668"},
			{"name":"湖南","val":"95684"}
]
//range(节点数目),radius大小
var nodes = d3.range(10).map(function(i) { 
	var data1 = {
		radius: parseFloat(data[i].val) / 2000,
		value:  parseFloat(data[i].val),
		label: data[i].name
	};
	return data1;


}),
    root = nodes[0],
    color = d3.scale.category10();

root.radius = 0;
root.fixed = true;

//力导向
var force = d3.layout.force()
    .gravity(0.01)  //中心产生重力(通俗点说就是改变圆球之间的距离),数值越大挨得越近,0则没有重力
//    .charge(function(d, i) { return i ? 0 : -2000; })
    .nodes(nodes)
    .size([width, height]);

force.start();
var padding = {left:-60, right:30, top:0, bottom:20};

var svg = d3.select("#countBall").append("svg")
    .attr("width", width)
    .attr("height", height);

//var ball = svg.selectAll("circle")
//  .data(nodes.slice(1))
//  .enter().append("circle")
//  .attr("r", function(d) { return d.radius; })
//  .style("fill", function(d, i) { return color(i % 3); })
//  .append("text")
//  .attr("class","text")
//  .text(d => d.label)
//  .attr("font-size", "30px")
//  .attr("fill", "red")
//  .attr("transform","translate(" + padding.left + "," + padding.top + ")");


var ball = svg.selectAll("g")
    .data(nodes.slice(1));
    
 var elemEnter = ball.enter()
 					.append("g")
    				.attr("transform","translate(" + padding.left + "," + padding.top + ")");
    				
  var circle =	elemEnter.append("circle")
  						.attr("r", function(d) { return d.radius; })
    					.style("fill", function(d, i) { return color(i % 1); })
    
    elemEnter.append("text")
    .attr("class","text")
    .text(d => d.label)
    .attr("font-size", "16px")
    .attr("fill", "#fff")



        
force.on("tick", function(e) {
var q = d3.geom.quadtree(nodes),
      i = 0,
      n = nodes.length;

//while (++i < n) q.visit(collide(nodes[i]));
svg.selectAll("circle")
      .attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; });
svg.selectAll("text")
      .attr("x", function(d) { return d.x - 16; })
      .attr("y", function(d) { return d.y + 6; });
});

svg.on("mousemove", function() {
var p1 = d3.mouse(this);
root.px = p1[0];
root.py = p1[1];
force.resume();
});

function collide(node) {
var r = node.radius + 16,
      nx1 = node.x - r,
      nx2 = node.x + r,
      ny1 = node.y - r,
      ny2 = node.y + r;
return function(quad, x1, y1, x2, y2) {
    if (quad.point && (quad.point !== node)) {
      var x = node.x - quad.point.x,
          y = node.y - quad.point.y,
          l = Math.sqrt(x * x + y * y),
          r = node.radius + quad.point.radius;
      if (l < r) {
        l = (l - r) / l * .5;
        node.x -= x *= l;
        node.y -= y *= l;
        quad.point.x += x;
        quad.point.y += y;
      }
    }
    return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
};
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3是一个流行的JavaScript框架,而D3.js是一个强大的JavaScript图形库,用于创建各种类型的数据可视化。在Vue3中使用D3.js可以轻松地创建网络图。 以下是使用Vue3和D3.js创建网络图的步骤: 1. 首先,您需要在Vue3项目中安装D3.js。可以使用npm包管理器执行以下命令:npm install d3 2. 在Vue组件中导入D3.js:import * as d3 from 'd3' 3. 在组件的mounted生命周期钩子中,使用D3.js创建SVG容器并设置其大小和位置: ``` mounted() { const svg = d3.select('#network-chart') .append('svg') .attr('width', this.width) .attr('height', this.height) } ``` 4. 接下来,您需要将网络数据传递给Vue组件。可以通过props来实现这一点。 5. 使用D3.js创建节点和连线。这可以通过以下代码完成: ``` const node = svg.selectAll('.node') .data(nodes) .enter() .append('circle') .attr('class', 'node') .attr('r', this.nodeRadius) .attr('fill', 'blue') const link = svg.selectAll('.link') .data(links) .enter() .append('line') .attr('class', 'link') .attr('stroke', 'gray') ``` 6. 最后,在组件的updated生命周期钩子中更新节点和连线的位置: ``` updated() { const node = svg.selectAll('.node') .attr('cx', d => d.x) .attr('cy', d => d.y) const link = svg.selectAll('.link') .attr('x1', d => d.source.x) .attr('y1', d => d.source.y) .attr('x2', d => d.target.x) .attr('y2', d => d.target.y) } ``` 以上就是使用Vue3和D3.js实现网络图的简单步骤,希望对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值