【发布时间】:2016-05-10 05:40:06
【问题描述】:
这里我正在使用 D3 网络图表,在我的fiddle 中,您可以看到指向目标节点(右侧)的箭头,请参见下面的屏幕截图:
有人知道我是怎么做到的吗?
这是我的代码:
var width = 500
height = 550;
var cluster = d3.layout.cluster()
.size([height - height * 0, width - width * 0.5]);
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.y, d.x];
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("style", "padding-left:5%")
.attr("pointer-events", "all")
.call(d3.behavior.zoom().on("zoom", redraw));
// Define the div for the tooltip
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var vis = svg
.append("svg:g");
vis.append("svg:rect")
.attr("width", width)
.attr("height", height)
.attr("fill", 'white');
function redraw() {
vis.attr("transform",
"translate(" + d3.event.translate + ")" +
" scale(" + d3.event.scale + ")");
}
var root = {
"name": "output",
"children": [{
"name": "sum",
"children": [{
"name": "Base",
"children": [],
"weight": 1.0000002576768052
}, {
"name": "H0",
"children": [],
"weight": 2.5767680512326025e-7
}]
}]
};
var nodes = cluster.nodes(root),
links = cluster.links(nodes);
vis.append("svg:defs").selectAll("marker")
.data(["end"]) // Different link/path types can be defined here
.enter().append("svg:marker") // This section adds in the arrows
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 15)
.attr("refY", -1.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var link = vis.selectAll(".link")
.data(links)
.enter().append("path")
.attr("class", "link")
.attr("marker-end", "url(#end)")
.attr("d", diagonal);
var node = vis.selectAll(".node")
.data(nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
})
.on("mouseover", mouseover)
.on("mouseout", mouseout);
node.append("circle")
.attr("r", 4.5)
.style("fill", "#3182bd");
node.append("svg:text")
.attr("dx", function(d) {
return d.children ? -8 : 8;
})
.attr("dy", 3)
.style("text-anchor", function(d) {
return d.children ? "end" : "start";
})
.style("fill", "#3182bd")
.text(function(d) {
return d.name;
});
function mouseover() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 9)
d3.select(this).select("text").transition()
.duration(750)
.style("stroke-width", ".5px")
.style("font", "22.8px serif")
.style("opacity", 1);
}
function mouseout() {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 4.5)
d3.select(this).select("text").transition()
.duration(750)
.style("font", "12px serif")
.style("opacity", 0.8);
}
d3.select(self.frameElement).style("height", height + "px");
【问题讨论】:
标签: javascript d3.js