简体   繁体   English

Highcharts如何显示某些工具提示而不显示其他工具提示

[英]Highcharts how to show certain tooltips and don't show others

I need to show tooltips for only the last two series when hovering the mouse over the graph. 将鼠标悬停在图形上时,我仅需要显示最后两个系列的工具提示。 In this example the idea will be to show only the tool tips for the values 1699 and 1750. The rest of the values can't show any tooltips when hovering the mouse. 在此示例中,想法是仅显示值1699和1750的工具提示。将鼠标悬停时,其余值将不显示任何工具提示。 How can I achive that? 我该如何实现? I haven't found any way to do so. 我还没有找到任何办法。 Is it possible? 可能吗? Thanks in advance. 提前致谢。

 (function () { var categories= ['1350', '1400', '1450', '1500', '1550', '1699', '1750']; Highcharts.chart('grafica1', { chart: { type: 'area', }, title: { text: ' ' }, xAxis: { labels: { enabled: true, formatter: function () { return categories[this.value]; } }, tickmarkPlacement: 'on', title: { enabled: false } }, yAxis: { title: { text: 'Percent' } }, tooltip: { pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.percentage:.1f}%</b> ({point.y:,.0f} millions)<br/>', split: true }, plotOptions: { area: { stacking: 'percent', lineColor: '#ffffff', lineWidth: 1, marker: { lineWidth: 1, lineColor: '#ffffff', } } }, series: [{ name: 'Africa', data: [502, 635, 809, 947, 1402, 3634, 5268] }, { name: 'L. America', data: [106, 107, 111, 133, 221, 767, 1766] }, { name: 'Oceania', data: [163, 203, 276, 408, 547, 729, 628] }, { name: 'SE. Asia', data: [18, 31, 54, 156, 339, 818, 1201] }, { name: 'Japan', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'China', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'Near East', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'Asian CIS', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'Russia', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'East Europe', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'Central Europe', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'W. Europe - Nordic', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'Nordic', data: [2, 2, 2, 6, 13, 30, 46] }, { name: 'N. America', data: [2, 2, 2, 6, 13, 30, 46] }] }); }); 
 <script src="https://code.highcharts.com/highcharts.js"></script> <script src="https://code.highcharts.com/modules/exporting.js"></script> <div id="grafica1" style="min-width: 310px; height: 400px; margin: 0 auto"></div> 

You can wrap tooltip.prototype.renderSplit method to show the tooltip only if the points have certain x values. 您可以包装tooltip.prototype.renderSplit方法以仅在点具有某些x值时显示工具提示。

Highcharts.wrap(Highcharts.Tooltip.prototype, 'renderSplit', function (p, labels, points) {
  if (!this.options.showOnLastTwo) {
    return p.call(this, labels, points)
  }

  const point = points[0]

  if (point) {
    const allowedXs = point.series.xData.slice(-2)

    if (allowedXs.includes(point.x)) {
      return p.call(this, labels, points)
    }
  }

  this.destroy()
})

And set in tooltip options showOnLastTwo flag to true: 并在工具提示选项showOnLastTwo标志设置为true:

tooltip: {
  split: true,
  showOnLastTwo: true
},

The flag is not necessary but the wrap changes Highcharts globally so it will affect every chart - the flag will prevent this. 该标志不是必需的,但是换行会全局更改Highcharts,因此它将影响每个图表-该标志将防止这种情况发生。

live example: http://jsfiddle.net/sLvekuht/1/ 实时示例: http//jsfiddle.net/sLvekuht/1/

You can use the formatter like this : 您可以像这样使用formatter

    tooltip: {
        formatter: function() {
          if(this.points[0].key < (this.points[0].series.xData.length -2)){
                return false;
          }
          var s = [];
          s.push(this.x);
          this.points.forEach(function(point) {
            s.push('<b>' + point.series.name + '</b>: ' + point.y);
          });

          return s;
        },
        split: true
    },

Fiddle 小提琴

Not the cleanest code (there still an error in the console) but it's work. 不是最干净的代码(控制台中仍然存在错误),但是可以正常工作。

Edit 编辑
Following Deep 3015 comment, code and fiddle updated 继Deep 3015注释,代码和小提琴更新后

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

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