在饼图中使用mouseover并在d3 v3 js中显示标签 [英] Use mouseover in pie chart and show label in d3 v3 js

查看:214
本文介绍了在饼图中使用mouseover并在d3 v3 js中显示标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是d3js的新手,所以我无法在给定的饼图代码中使用mouseover事件...我有一个名为chart的ID,因此如何创建某个类来进行mouseover事件并显示标签?

Hi I am new in d3js so I am unable to use mouseover event in given code of pie chart...i have a with id named chart so how can I create some class that mouseover event and show a label?

这是我用来绘制饼图的代码:

Here is the code that I am using to draw pie chart:

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

推荐答案

我使用工具提示:

var popup=d3.select("body").append("div").attr("class","tooltip").style("opacity",0);

然后调用工具提示,在节点上添加一个事件侦听器(我想这对您来说是弧线,但我还没有完成饼图):

Then to call the tooltip, add an event listener to the nodes (I guess it would be arcs for you, but I haven't done pie charts):

nodes.on("mouseover", fade(.1,"over")).on("mouseout",fade(.8,"out"));

然后将工具提示放到节点(在这种情况下为饼图)附近的函数:

Then the function to put the tooltip near the node (or pie in this case):

function fade (opacity, event){
return function (d){
    if(event === "over"){
    popup.transition().duration(100).style("opacity", .9).style("display", "inline-block");
    popup.html("Year: " + d.year + "</br> Value: " + d.value)
    .style("left", (d3.event.pageX + 20) + "px")
    .style("top", (d3.event.pageY - 20) + "px");
    d3.select(this).classed("node-mouseover", true);}
else if(event==="out"){
    popup.transition().duration(100).style("opacity",0);
    d3.select(this).classed("node-mouseover",false);

}}}

还有其他方法,但这似乎很流行此示例相似.

There are other ways of doing it, but this seems to be pretty popular this example is similar.

查看 bl.ocks.org 了解更多示例.

这篇关于在饼图中使用mouseover并在d3 v3 js中显示标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆