javascript - D3js强制布局 - 节点之间的渐变线

标签 javascript d3.js svg gradient

这是我的 d3 force 布局: (请运行代码片段)

var width = 600,
    height = 600;

var svg = d3.select('body').append('svg')
    .attr('width', width)
    .attr('height', height);

var color = d3.scale.category20();

var dataNodes = [
    { x:   width/3, y:   height/3 , group: 0, color: 'blue'},
    { x: 2*width/3, y:   height/3, group: 1, color: 'red' },
    { x:   width/2, y: 2*height/3, group: 2, color: 'green'}
];

var dataLinks = [
  { source: 0, target: 1},
  { source: 1, target: 2},
  { source: 2, target: 0}
];

var force = d3.layout.force()
    .charge(-400)
    .linkDistance(height/2)
    .size([width, height])
    .linkStrength(1.3)
    .friction(0.8)
    .gravity(0.9);

force
    .nodes(dataNodes)
    .links(dataLinks)
    .start();

var link = svg.selectAll(".link")
      .data(dataLinks)
    .enter().append("line")
      .attr("class", "link");

var node = svg.selectAll(".node")
    .data(dataNodes)
  .enter().append("circle")
    .attr("class", function(d){ return "node " + d.color})
    .attr("r", width/20)
    .call(force.drag);

node.append("title")
      .text(function(d) { return d.color; });


force.on('tick', function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
});
.node {
    fill: #ccc;
    stroke: #fff;
    stroke-width: 0;
}
.node.blue {
    fill: blue;
}
.node.red {
    fill: red;
}
.node.green {
    fill: green;
}

.link {
    fill: none;         
    stroke: black;
    stroke-width: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

这是我想要实现的: enter image description here

这怎么可能?我如何在节点之间的链接上应用渐变? 如果有什么不清楚的地方,请询问。

谢谢!

最佳答案

这是结果:https://jsfiddle.net/tekh27my/11/

定义部分和@Cyril的差不多

var gradient = d3.select("svg").append("defs")
    .append("linearGradient")
    .attr("id", "gradient")
    .attr("spreadMethod", "pad");
  //start color white
  gradient.append("stop")
    .attr("offset", "0%")
    .attr("stop-color", "red")
    .attr("stop-opacity", 1);
  //end color steel blue
    gradient.append("stop")
    .attr("offset", "100%")
    .attr("stop-color", "green")
    .attr("stop-opacity", 1);

但是在每次报价时都需要对 x1,y1 和 x2,y2 进行动态更新

所以这是“tick”函数的代码:

var linkVector = new Vector2(d.target.x-d.source.x,d.target.y-d.source.y).getUnitVector();
var perpVector = linkVector.perpendicularClockwise().scale(radius);
var gradientVector = linkVector.scale(0.5);


gradient
    .attr("x1", 0.5-gradientVector.X)
    .attr("y1", 0.5-gradientVector.Y)
    .attr("x2", 0.5+gradientVector.X)
    .attr("y2", 0.5+gradientVector.Y);

0.5 是路径的中间(如您所猜),因为根据我的计算,这些是单位向量。

gradientVector 是缩放到 0.5 的单位向量。

下面是单位向量计算代码:

var Vector2 = function(x,y) {
  this.magnitude = Math.sqrt(x*x+y*y);
  this.X = x;
  this.Y = y;
};

Vector2.prototype.perpendicularClockwise = function(){
  return new Vector2(-this.Y, this.X);
};

Vector2.prototype.perpendicularCounterClockwise = function(){
  return new Vector2(this.Y, -this.X);
};

Vector2.prototype.getUnitVector = function(){
  return new Vector2(this.X/this.magnitude, this.Y/this.magnitude);
};

Vector2.prototype.scale = function(ratio){
  return new Vector2(ratio*this.X, ratio*this.Y);
};

注意:“刻度”内路径的 sourceDelta/targetDelta 计算与本题无关。

关于javascript - D3js强制布局 - 节点之间的渐变线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35229576/

相关文章:

javascript - 更改从文件加载的 SVG 的颜色

javascript - 在 Javascript 中访问对象数组(或数组数组)

javascript - JS InfiniteRotator 随机顺序

javascript - d3 在鼠标悬停时过滤数据后从选择中提取值

javascript - 如何禁用 networkD3/d3Network 网络图中的鼠标悬停效果(GNU R 包)

javascript - 饼图外弧边界d3(svg)

javascript - 为什么我的 html 按钮在 I.E 中移动?他们不会在 Chrome 或 FF 中移动

javascript - 类型错误 : Cannot read property 'concat' of undefined

javascript - 绘制路径以连接两个节点而不与其他节点相交+ d3.js

html - 变换 : translate Not working across all browsers