簡體   English   中英

d3淡化非連接節點的圖標

[英]d3 fade icons of non connected nodes

我已經創建了一個我的Twitter時間軸的力導向圖,並且在每個節點上都是一個帶有用戶屏幕名稱的配置文件圖標,當鼠標懸停在一個節點上時,它會淡化未連接的節點

但是非連接節點的圖標圖像並沒有褪色,我已經讓文字和線條褪色但圖標保持不變。

我怎樣才能使節點的圖像褪色?

這是圖的jsfiddle

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});
  link.source.icon = link.icon;
});

var width = 1400, height = 1200;
fill = d3.scale.category20();
var force = d3.layout.force()
    .nodes(d3.values(nodes))
    .links(links)
    .size([width, height])
    .linkDistance(120)
    .charge(-300)
    .on("tick", tick)
    .start();

var svg = d3.select("#main-page").append("svg:svg")
    .attr("width", width)
    .attr("height", height)
    .attr("pointer-events", "all")
    .append('svg:g')
    .call(d3.behavior.zoom().on("zoom", redraw))
    .append('svg:g');


// Per-type markers, as they don't inherit styles.
svg.append("defs").selectAll("marker")
    .data(["suit", "licensing", "resolved"])
  .enter().append("marker")
    .attr("id", function(d) { return d; })
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 15)
    .attr("refY", -1.5)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
  .append("path")
    .attr("d", "M0,-5L10,0L0,5");

var path = svg.append("g").selectAll("path")
    .data(force.links())
  .enter().append("path")
    .attr("class", function(d) { 
        return "link " + d.type; 
    }).attr("marker-end", function(d) { 
        return "url(#" + d.type + ")"; });

var image = svg.append("g").selectAll("image")
    .data(force.nodes())
    .enter().append("image")
    .attr("xlink:href", function(d) { return d.icon; })
    .attr("x", -8)
    .attr("y", -8)
    .attr("width", 20)
    .attr("height", 20)
 .style("stroke", function(d) {
    return d3.rgb(fill(d.group)).darker();
}).call(force.drag)
.on("mouseover", fade(.1))
.on("mouseout", fade(1));

var text = svg.append("g").selectAll("text")
    .data(force.nodes())
  .enter().append("text")
    .attr("x", 8)
    .attr("y", ".31em")
    .text(function(d) { return d.name; });

function redraw() {
    console.log("here", d3.event.translate, d3.event.scale);
    svg.attr("transform",
             "translate(" + d3.event.translate + ")"
             + " scale(" + d3.event.scale + ")");
}
var linkedByIndex = {};
    links.forEach(function(d) {
    linkedByIndex[d.source.index + "," + d.target.index] = 1;
});

function isConnected(a, b) {
    return linkedByIndex[a.index + "," + b.index] || linkedByIndex[b.index + "," + a.index] || a.index == b.index;
}

function fade(opacity) {
    return function(d) {
        image.style("stroke-opacity", function(o) {
            thisOpacity = isConnected(d, o) ? 1 : opacity;
            this.setAttribute('fill-opacity', thisOpacity);

            return thisOpacity;
        });
        text.style("stroke-opacity", function(o) {
            thisOpacity = isConnected(d, o) ? 1 : opacity;
            this.setAttribute('fill-opacity', thisOpacity);
            return thisOpacity;
        });

        path.style("stroke-opacity", function(o) {
            return o.source === d || o.target === d ? 1 : opacity;
        });
    };
}

// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
  path.attr("d", linkArc);

  text.attr("transform", transform);
 image.attr("transform", transform);
}

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;
}

function transform(d) {
  return "translate(" + d.x + "," + d.y + ")";
}

在你的fade功能里面替換

image.style("stroke-opacity", function(o) {
    thisOpacity = isConnected(d, o) ? 1 : opacity;
    this.setAttribute('fill-opacity', thisOpacity);

    return thisOpacity;
});

image.style("opacity", function(o) {
    thisOpacity = isConnected(d, o) ? 1 : opacity;
    this.setAttribute('opacity', thisOpacity);

    return thisOpacity;
});

為你的形象

更新小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM