Adnan
Adnan

Reputation: 2996

Highcharts Pie Chart: How to ignore disabled legend items

Please see this JSFiddle.

In this example, you can see sum of all values at top left corner.

If you click on any legend item, it will be disabled, but the total value doesn't reflect it. The text should be updated to not include that disabled item. How can I do this?

Upvotes: 1

Views: 135

Answers (2)

Adnan
Adnan

Reputation: 2996

While Ondkloss's answer works fine. But I have found a simpler solution, by using redraw event instead of load. JSFiddle

Upvotes: 1

Halvor Holsten Strand
Halvor Holsten Strand

Reputation: 20536

You can use the legendItemClick event to update the rendered text by keeping a reference to the Element. For example:

var totalText;

var chart = new Highcharts.Chart({
    chart: {
        events: {
            load: function(event) {
                totalText = this.renderer.text(
                    'Total: ' + total,
                    this.plotLeft,
                    this.plotTop - 20
                ).attr({
                    zIndex: 5
                }).add()
            }
        }
    }

    //...

    plotOptions: {
        pie: {
            point: {
                events: {
                    legendItemClick: function(e) {
                        var newTotal = this.series.total + (this.visible ? -this.y : this.y);
                        totalText.attr({ text: 'Total: '+newTotal });
                    }
                }
            }
        }
    }
});

See this updated JSFiddle for a demonstration.

Upvotes: 0

Related Questions