高亮显示工具提示共享项,具体取决于Highcharts中悬停的系列

Highlight tooltip shared item depending on hovered serie in Highcharts

本文关键字:Highcharts 取决于 悬停 系列 工具提示 显示 共享 高亮      更新时间:2024-05-30

我有以下图表:http://jsfiddle.net/5oso60oq/

JS:

$(function () {
    $('#container').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Stacked bar chart'
        },
        xAxis: {
            categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Total fruit consumption'
            }
        },
        legend: {
            reversed: true
        },
        tooltip: {
        	shared: true
        },
        plotOptions: {
            series: {
                stacking: 'normal'
            }
        },
        series: [{
            name: 'John',
            data: [5, 3, 4, 7, 2]
        }, {
            name: 'Jane',
            data: [2, 2, 3, 2, 1]
        }, {
            name: 'Joe',
            data: [3, 4, 4, 2, 5]
        }]
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>

我想做的是根据图表上悬停的系列来突出显示(或更改)工具提示的系列元素。

例如,如果我将鼠标悬停在"苹果"图表系列的绿色区域,我想在工具提示中突出显示"乔",依此类推…

我在系列中找到了一些悬停事件的技巧,但它渲染得不好。

您应该禁用共享参数并使用格式化程序,它允许对所有序列和点进行迭代。添加检查当前x值和打印点的条件。

tooltip: {
  shared: false,
  formatter: function() {
    var series = this.series.chart.series,
      each = Highcharts.each,
      x = this.point.x,
      txt = '<span style="font-size: 10px">' + this.key + '</span><br/>';

            each(series, function(serie,i) {
        each(serie.data, function(data, j){
        if(data.x === x) {
            txt += '<span style="color:' + data.color + '">'u25CF</span> ' + serie.name + ': <b>' + data.y + '</b><br/>';
        }
      });
    });
    return txt;
  }
},

示例:-http://jsfiddle.net/fsfyq89p/

链接

      tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
      }

试试这个

我一直在寻找同样的东西,并找到了更好的解决方案,我认为

formatter(tooltip) {
  // sort the values
  // eslint-disable-next-line no-nested-ternary
  this.points.sort((a, b) => ((a.y < b.y) ? -1 : (a.y > b.y) ? 1 : 0))
    .reverse()
  tooltip.chart.series.forEach((item) => {
    if (!item.state) item.setState('inactive')
  })
  return tooltip.defaultFormatter.call(this, tooltip)
},

https://jsfiddle.net/h3ar4yen/