1
votes

I am using highcharts to build a Pie chart, I am hiding the default legend that comes with highcharts and making my own. I am able to build one for line graphs, but when I do it for a pie chart, I can not get show()/hide() methods of the data to work because they are all in the same series.

How can I get show()/hide() to work on a pie chart?

var chart = $('#chartdiv').highcharts();
$(".legend div").html("");
$(chart.series).each(function(seriesKey, series){
    $(series.data).each(function(k, v){
        var color = v.color;
        var name = v.name;
        var total = v.options.y;
        var activeStatus = v.visible ? "active" : "inactive";
        var item = $("<div data-color=\""+color+"\" data-series=\""+k+"\" class=\""+activeStatus+"\"><p>"+name+"</p><p>"+total+"</p></div>");
        $(".legend > div").append(item);
        if(item.hasClass("active")){
            item.css("border-bottom", "solid 3px " + color);
        }
        item.on("mouseover mouseleave click", function(e){
            if(e.type === "mouseover"){
                $(this).css("border-bottom", "solid 3px " + color);
            }else if(e.type === "mouseleave"){
                if($(this).hasClass("active")){
                    $(this).css("border-bottom", "solid 3px " + color);
                }else{
                    $(this).css("border-bottom", "solid 3px #dddddd");
                }
            }else if(e.type === "click"){
                $(this).removeClass("active inactive");
                if(v.visible){
                    $(this).addClass("inactive");
                    v.hide();
                }else{
                    $(this).addClass("active");
                    v.show();
                }
            }
        });
    });
});
1

1 Answers

3
votes

A similar mechanism exists for pie. It operates on each individual Point. You just have use setVisible(boolean) on the point you want to show/hide, for example like this:

chart.series[0].points[0].setVisible(false);

See this JSFiddle demonstration for tips on how to show/hide using a clickable div.