Two pies, one legend with unique items (merge lege

2019-02-19 02:34发布

Is it possible to do two pies in one graphic and merge legends?

I did this : http://jsfiddle.net/Adysone/YpfBs/

chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'pie'
    },
    title: {
        text: "Pie Charts",
        align: 'center'
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'top'
    },
    tooltip: {
        formatter: function () {
            return this.key + ': ' + this.y + ' (' + Math.round(this.percentage) + '%)';
        }
    },
    plotOptions: {
        pie: {
            showInLegend: true,
            size: 200
        }
    },
    series: [
        {
            name: "2011",
            data: [
                {
                    name: "Apple",
                    color: "#FF3333",
                    y: 8
                },
                {
                    name: "Banana",
                    color: "#FFF55C",
                    y: 11
                },
                {
                    name: "Grape",
                    color: "#90007B",
                    y: 9
                },
                {
                    name: "Pear",
                    color: "#F1FFB8",
                    y: 3
                }
            ],
            center: [150,100],
            size: 150
        },
        {
            name: "2012",
            data: [
                {
                    name: "Apple",
                    color: "#FF3333",
                    y: 5
                },
                {
                    name: "Banana",
                    color: "#FFF55C",
                    y: 15
                },
                {
                    name: "Pear",
                    color: "#F1FFB8",
                    y: 8
                }
            ],
            center: [450,100],
            size: 150
        }
    ]
});

But legend items are duplicated since they are common in the two pies, how can I do to make these unique?

If it's not possible, can I make two legends separate?

Thanks!

标签: highcharts
1条回答
爷、活的狠高调
2楼-- · 2019-02-19 03:00

Please take look at example http://jsfiddle.net/u7FQS/15/ which used 3 pie charts and has common legend

$(chart.series[0].data).each(function(i, e) {
        e.legendItem.on('click', function(event) {
            var legendItem=e.name;

            event.stopPropagation();

            $(chart.series).each(function(j,f){
                   $(this.data).each(function(k,z){
                       if(z.name==legendItem)
                       {
                           if(z.visible)
                           {
                               z.setVisible(false);
                           }
                           else
                           {
                               z.setVisible(true);
                           }
                       }
                   });
            });

        });
    });
查看更多
登录 后发表回答