【问题标题】:Highlight tooltip shared item depending on hovered serie in Highcharts根据 Highcharts 中悬停的系列突出显示工具提示共享项目
【发布时间】:2016-04-13 13:49:17
【问题描述】:

我有以下图表: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>

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

例如,如果我将鼠标悬停在“Apples”图表系列上的绿色区域,我想在工具提示中突出显示“Joe”,等等......

我发现了一些有关悬停事件的提示,但渲染效果不佳。

【问题讨论】:

    标签: javascript css angularjs highcharts highcharts-ng


    【解决方案1】:

    您应该禁用共享参数并使用格式化程序,它允许迭代所有系列和点。添加检查当前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/

    【讨论】:

      【解决方案2】:

      link

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

      试试这个

      【讨论】:

      • 这不是我要找的结果,我想在工具提示对象中保留 'shared': true 。
      【解决方案3】:

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

      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/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        相关资源
        最近更新 更多