您好我是d3js中的新手,所以我无法在饼图的给定代码中使用鼠标悬停事件...我有一个名为图表的ID,所以如何创建一个鼠标悬停事件并显示标签的类?
以下是我用于绘制饼图的代码:
var w = 300;
var h = 300;
var dataset = [
{"year":"2017-07-01","value":"5"},
{"year":"2017-07-02","value":"10"},
{"year":"2017-07-03","value":"15"},
{"year":"2017-07-04","value":"20"},
{"year":"2017-07-05","value":"25"},
{"year":"2017-07-06","value":"30"},
{"year":"2017-07-07","value":"35"},
{"year":"2017-07-08","value":"40"},
{"year":"2017-07-09","value":"45"},
{"year":"2017-07-10","value":"50"},
{"year":"2017-07-11","value":"55"},
{"year":"2017-07-12","value":"60"},
{"year":"2017-07-13","value":"65"},
{"year":"2017-07-14","value":"70"}
];
var outerRadius = w / 2;
var innerRadius = 0;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie()
.value(function(d) {
return d.value;
});
var color = d3.scale.category20();
var svg = d3.select("#chart")
.append("svg")
.attr("width", w)
.attr("height", h);
var arcs = svg.selectAll("g.arc")
.data(pie(dataset))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
arcs.append("path")
.attr("fill", function(d, i) {
return color(i);
})
.attr("d", arc);
arcs.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d) {
return d.value;
});
答案 0 :(得分:2)
我使用工具提示:
Doorknob knob = opt(() -> house.getFloor(0).getWall(WEST).getDoor().getDoorknob());
然后调用工具提示,向节点添加一个事件监听器(我想这对你来说是弧线,但我还没有完成饼图):
var popup=d3.select("body").append("div").attr("class","tooltip").style("opacity",0);
然后将工具提示放在节点附近的函数(或者在这种情况下为饼):
nodes.on("mouseover", fade(.1,"over")).on("mouseout",fade(.8,"out"));
}}}
还有其他方法可以做到这一点,但这似乎非常受欢迎this example类似。
修改:查看bl.ocks.org了解更多示例。