简体   繁体   English

Highcharts 如何将数据 label 添加到系列的最后一个数据点

[英]Highcharts How to add data label to last data point of a series

I built a line chart: fiddle我建立了一个折线图:小提琴

Now I want to add a data label to the last data point of each series.现在我想在每个系列的最后一个数据点添加一个数据 label。

I tried with formatter:我尝试使用格式化程序:

if (this.x == visiblePoints[visiblePoints.length - 1].x)

but some series do not have a data point for the last date.但有些系列没有最后日期的数据点。 How can I get the length of existing data?如何获取现有数据的长度?

As you rightly notice you need to use the formatter function for data labels.正如您正确地注意到的,您需要将formatter function 用于数据标签。 Use below code to check if the point is the last one.使用下面的代码检查该点是否是最后一个。

series: {
  dataLabels: {
    enabled: true,
    formatter: function() {
      var seriesPoints = this.series.points;
      if (this.point === seriesPoints[seriesPoints.length - 1]) {
        return 'Last label point';
      }
    }
  }
}

Live demo: http://jsfiddle.net/BlackLabel/uqrt2j9d/现场演示: http://jsfiddle.net/BlackLabel/uqrt2j9d/

API Reference: https://api.highcharts.com/highcharts/plotOptions.series.dataLabels API 参考: https://api.highcharts.com/highcharts/plotOptions.series.dataLabels

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

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