Highcharts:在图例悬停时显示工具提示

时间:2017-04-19 18:47:34

标签: jquery twitter-bootstrap highcharts angular2-highcharts

我的Highchart如下所示,有没有办法在图例悬停上显示工具提示,就像我们将鼠标悬停在切片上一样。 https://plnkr.co/edit/yzXLz7AIDoWa1Pzxxl4k?p=preview

我的工具提示代码:

tooltip: {
    positioner: function(x, y){
         var center = this.chart.series[0].center;
         console.log(this, arguments);
         return { x: center[0] - x/2, y: center[1] + y/2 };
    },
    formatter: function() {
         return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage*100)/100 + ' %';
    }
}

3 个答案:

答案 0 :(得分:3)

您可以使用Black Label的自定义事件插件,计算点击标签的索引并使用tooltip.refresh()方法显示工具提示。

&#13;
&#13;
var options = {
  xAxis: {
    categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
      'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
    ],
    labels: {
      events: {
        click () {
          const label = this
          const chart = this.chart
          const data = this.axis.series[0].data
          const { categories } = this.axis
          const i = categories.indexOf(label.value)
          if (i !== -1) chart.tooltip.refresh(data[i])
        },
      }
    }
  },
  series: [{
    minPointLength: 10,
    data: [900, 0.1],
    type: 'column'
  }]
}

var chart = Highcharts.chart('container', options)
&#13;
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://blacklabel.github.io/custom_events/js/customEvents.js"></script>

<div id="container"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您已在代码中添加了bootstrap,因此请使用Bootstrap tooltip并在图表onload函数中添加以下代码,

var legend = chart.legend;
for (var i = 0, len = legend.allItems.length; i < len; i++) {
   (function(i) {
       var t=legend.allItems[i],
           content= '<b>'+ t.name +'</b>: '+ Math.round(t.percentage*100)/100 + ' %';
           jQuery($(t.legendItem.element)).tooltip({title:content,html:true});
   })(i);
}

<强> Plnkr Demo

答案 2 :(得分:1)

我实际上是使用鼠标悬停事件的模态,这是你如何做到这一点,请在你的图表函数中插入此代码:

$('#chart_container').on('mouseenter','.highcharts-legend-item',function(event) {
    var seriesName = $(this).text();
    if (seriesName === "this_series_name") {
        $('#myModal').modal('toggle');
    }
});

然后你可以使用下面的悬停事件打开一个模态div

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="myModalLabel">Note</h4>
            </div>
            <div class="modal-body">

                <div class="row">
                    <div class="col-md-12">
                        your message here
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    Ok
                </button>
            </div>
        </div>
    </div>
</div>

这是demo