Johannes Christ
Johannes Christ

Reputation: 109

Highcharts How to display total of data classes in legend

I built a bubble map with Highmaps: see fiddle

Now I want to display the total of the data classes in the legend and therefore added this code:

labelFormatter: function () {
                total = this.total;


            return (this.name) + ' (' + total + ')';
        },

Unfortunately I do not get a value. What do I have to change?

Upvotes: 1

Views: 300

Answers (1)

ppotaczek
ppotaczek

Reputation: 39099

The amount of points that belong to a specific data class is not stored, so you need to calculate it by yourself, for example:

labelFormatter: function() {
  var chart = this.chart,
    from = this.from,
    to = this.to,
    data,
    total,
    index;

  if (chart.legend) {
    index = chart.legend.allItems.indexOf(this);
    data = chart.series[1].options.data;

    total = data.filter(function(el) {
      return (typeof from === 'undefined' || el.neupro >= from) &&
        (typeof to === 'undefined' || el.neupro < to)
    }).length;
  }

  return (this.name) + ' (' + total + ')';
}

Live demo: https://jsfiddle.net/BlackLabel/7ajxsu6y/

API Reference: https://api.highcharts.com/highcharts/legend.labelFormatter

Upvotes: 1

Related Questions