简体   繁体   English

在饼图中使用mouseover并在d3 js中显示标签

[英]Use mouseover in pie chart and show label in d3 js

Hi I am new in d3js so I am unable to use mouseover event in given code of pie chart...i have a <div> with id named chart so how can I create some class that mouseover event and show a label? 嗨,我是d3js的新手,所以我无法在给定的饼图代码中使用mouseover事件...我有一个ID为chart<div> ,所以我如何创建该类的mouseover事件并显示标签?

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

I assume that what you want is a tooltip. 我假设您想要的是一个工具提示。 The easiest way to do this is to append an svg:title element to each circle, as the browser will take care of showing the tooltip and you don't need the mousehandler. 最简单的方法是在每个圆圈上附加一个svg:title元素,因为浏览器将负责显示工具提示,并且不需要鼠标处理程序。 The code would be something like vis.selectAll("circle") .data(datafiltered).enter().append("svg:circle") ... .append("svg:title") .text(function(d) { return dx; }); 该代码类似于vis.selectAll(“ circle”).data(datafiltered).enter()。append(“ svg:circle”)... .append(“ svg:title”).text(function(d ){return dx;}); If you want fancier tooltips, you could use tipsy for example. 如果您想获得更高级的工具提示,则可以使用Tipsy作为示例。 See here for an example. 请参阅此处的示例。

Add Styles on your HTML 在HTML上添加样式

   <style>
      #chart {                                                           
        height: 360px;                                                  
        position: relative;                                              
        width: 360px;                                                    
      }                                                                 
      .tooltip {                                                         
        background: #eee;                                                
        box-shadow: 0 0 5px #999999;                                    
        color: #333;                                                    
        display: none;                                                   
        font-size: 12px;                                                
        left: 130px;                                                    
        padding: 10px;                                                  
        position: absolute;                                             
        text-align: center;                                             
        top: 95px;                                                       
        width: 80px;                                                     
        z-index: 10;                                                     
      }                                                                 
      .legend {
        font-size: 12px;
      }
      rect {
        stroke-width: 2;
      }
    </style>

JS side JS端

    var width = 360;
    var height = 360;
    var radius = Math.min(width, height) / 2;
    var donutWidth = 75;
    var legendRectSize = 18;
    var legendSpacing = 4;

    var color = d3.scale.category20b();

    var svg = d3.select('#chart')
      .append('svg')
      .attr('width', width)
      .attr('height', height)
      .append('g')
      .attr('transform', 'translate(' + (width / 2) + 
        ',' + (height / 2) + ')');

    var arc = d3.svg.arc()
      .innerRadius(radius - donutWidth)
      .outerRadius(radius);

    var pie = d3.layout.pie()
      .value(function(d) { return d.count; })
      .sort(null);

    var tooltip = d3.select('#chart')                               
      .append('div')                                                
      .attr('class', 'tooltip');                                    

    tooltip.append('div')                                           
      .attr('class', 'label');                                      

    tooltip.append('div')                                          
      .attr('class', 'count');                                      

    tooltip.append('div')                                           
      .attr('class', 'percent');                                    


      var path = svg.selectAll('path')
        .data(pie(dataset))
        .enter()
        .append('path')
        .attr('d', arc)
        .attr('fill', function(d, i) { 
          return color(d.data.label); 
        });

      path.on('mouseover', function(d) {                            
        var total = d3.sum(dataset.map(function(d) {                
          return d.count;                                           
        }));                                                        
        var percent = Math.round(1000 * d.data.count / total) / 10; 
        tooltip.select('.label').html(d.data.label);                
        tooltip.select('.count').html(d.data.count);                
        tooltip.select('.percent').html(percent + '%');             
        tooltip.style('display', 'block');                          
      });                                                           

      path.on('mouseout', function() {                              
        tooltip.style('display', 'none');                           
      });                                                           



      var legend = svg.selectAll('.legend')
        .data(color.domain())
        .enter()
        .append('g')
        .attr('class', 'legend')
        .attr('transform', function(d, i) {
          var height = legendRectSize + legendSpacing;
          var offset =  height * color.domain().length / 2;
          var horz = -2 * legendRectSize;
          var vert = i * height - offset;
          return 'translate(' + horz + ',' + vert + ')';
        });

      legend.append('rect')
        .attr('width', legendRectSize)
        .attr('height', legendRectSize)                                   
        .style('fill', color)
        .style('stroke', color);

      legend.append('text')
        .attr('x', legendRectSize + legendSpacing)
        .attr('y', legendRectSize - legendSpacing)
        .text(function(d) { return d; });

I hope this helps you. 我希望这可以帮助你。 You might have to work around, it depends on how you want to show tool tip and how you populate data in your chart. 您可能需要解决,这取决于您要如何显示工具提示以及如何在图表中填充数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM