Tharif
Tharif

Reputation: 13971

How to Hide rest of the region when user clicks pie chart?

I have pie chart representation of user locations as below in figure 1,i have successfully made the representation working but how can i make the rest of users hidden as figure 2 when click any particular sector ?

Figure 1:

enter image description here

Figure 2:

enter image description here

Javascript :

$(function () {
    $('#container').highcharts({
        chart: {
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'users location'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                    style: {
                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            data: [
                ['Africa',   45.0],
                ['Asia',       26.8],
                {
                    name: 'Australia',
                    y: 12.8,
                    sliced: true,
                    selected: true
                },
                ['Europe',    8.5],
                ['North America',     6.2],
                ['Others',   0.7]
            ]
        }]
    });
});

Fiddle Link

Upvotes: 0

Views: 168

Answers (2)

Paweł Fus
Paweł Fus

Reputation: 45079

And example in pure Highcharts. As an another answer, use pie.point.events.click handler, to hide/show elements: http://jsfiddle.net/5oLmj00L/8/

            point: {
                events: {
                    click: function() {
                        var _self = this,
                            undef,
                            method = _self.clicked ? "show" : "hide";

                        Highcharts.each(this.series.data, function(p, i) {
                            if(p !== _self) {
                                // hide/show slice
                                if(p.graphic) {
                                    p.graphic[method]();
                                }
                                // hide/show label
                                if(p.dataLabel) {
                                     p.dataLabel[method]();   
                                }
                                // hide/show connector
                                if(p.connector) {
                                     p.connector[method]();
                                }
                            }
                        });
                        // set flag for next click:
                        _self.clicked = _self.clicked !== undef ? !_self.clicked : true;
                    }    
                }
            }

Upvotes: 2

Raein Hashemi
Raein Hashemi

Reputation: 3384

You can use plotOptions.series.point.events.click function to tell the chart exactly what to do after the click of a slice:

series: {
    point: {
        events: {
            click: function () {
                var index = this.x;
                $('.highcharts-series-group g path').toggle();
                $('.highcharts-series-group g path:nth-child(' + (index+1) + ')').toggle();

                $('.highcharts-data-labels path').toggle();
                $('.highcharts-data-labels path:nth-child(' + (index+1) + ')').toggle();

                $('.highcharts-data-labels g').toggle();
                $($('.highcharts-data-labels g').get(index)).toggle();
            }
        }
    }
}

The first two toggles are for the slices themselves. $('.highcharts-series-group g path') refers to all the colored slices in the chart, and I changed back the one user just clicked by adding :nth-child.

The second pair of toggles are for the lines coming out of the slices connecting the datalabels to them. And the third pair is for the datalabels.

Here's the DEMO.

Upvotes: 3

Related Questions