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

时间:2018-02-28 16:54:21

标签: javascript highcharts

将鼠标悬停在图表上时,我只需显示最后两个系列的工具提示。在此示例中,我们的想法是仅显示值1699和1750的工具提示。其余值在悬停鼠标时无法显示任何工具提示。我怎么能做到这一点?我还没有找到任何办法。可能吗?提前谢谢。

(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: 'S-E. 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>

2 个答案:

答案 0 :(得分:2)

只有当点具有特定的x值时,您才可以使用tooltip.prototype.renderSplit方法显示工具提示。

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()
})

并在工具提示选项showOnLastTwo标志中设置为true:

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

该标志不是必需的,但换行全局更改Highcharts,因此它将影响每个图表 - 该标志将阻止此。

直播示例:http://jsfiddle.net/sLvekuht/1/

答案 1 :(得分:1)

您可以像这样使用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

不是最干净的代码(控制台中仍然存在错误),但它的工作正常。

修改
关注Deep 3015评论,代码和小提琴更新