0

对于下面的代码鼠标移出处理程序不起作用,但是当我将鼠标移到相关代码上时工作正常。(由于 chart.draw() 无法正常工作)。伙计们,你们能帮我解决这个问题吗?

  google.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

    var options = {
        title: 'My Daily Activities',
        legend: 'none',
        pieSliceText: 'percentage',
        slices: { }
    };

    var chart = new   google.visualization.PieChart(document.getElementById('piechart'));
    chart.draw(data, options);

    google.visualization.events.addListener(chart, 'onmouseout', function(e) {

        var slices = options.slices ;
        slices[e.row] = {};
        options['slices'] = slices;            
        chart.draw(data,options);
      } 
    );  // mouse out end

   google.visualization.events.addListener(chart, 'onmouseover', function(e) {

       var sliceid= e.row;
       console.log("mouse over"+sliceid);
       var slices = options.slices ;
       slices[e.row] = {offset:0.2};
       options['slices'] = slices;
       chart.draw(data,options);
           }
    );   // mouse over end
  }
4

1 回答 1

0

似乎问题是鼠标悬停重复触发,因此图表一直重绘,没有给鼠标悬停事件足够的时间触发。这应该使它工作:

   google.visualization.events.addListener(chart, 'onmouseover', function(e) {
       var sliceid= e.row;
       var slices = options.slices ;
       if(slices.hasOwnProperty(e.row)){
           if(slices[e.row].hasOwnProperty('offset')) return false // if still mouseover, skip
       }

工作小提琴:http: //jsfiddle.net/qm7f86ez/

于 2014-10-16T05:24:51.740 回答