Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highcharts add legend on export

I'm trying to add a legend on a pie when exporting the chart as PNG. Here is my code :

var chart_23_106;
$(document).ready(function () {
chart_23_106 = new Highcharts.Chart({
    chart: { type: 'pie', renderTo: 'container_23_106', plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false },
    title: { text: 'NRJ' },
    tooltip: { pointFormat: '{series.name}: <b>{point.percentage}%</b>', percentageDecimals: 1 },
    plotOptions: {
        pie: { borderWidth: 0, shadow: false, allowPointSelect: true, cursor: 'pointer', dataLabels: { enabled: false } }
    },
    colors: ['#9F9F9F','#BE6EBE','#FFA74F','#B7B7D0','#CBE22A','#00C8C8'],
    credits: { enabled: false, text: "Source: Air Normand", href: "" },
    exporting:{ buttons: {
            printButton: {enabled:false},
            exportButton: {menuItems:null,onclick:function(){this.exportChart(null, 
                                        { chart: {reflow: false, width: 400}, 
                                          title: {text: "Répartition de la Consommation"}, 
                                          subtitle: {text: "Haute-Normandie"}, 
                                          plotOptions: {pie: {dataLabels: {enabled: true}, showInLegend: true}}, 
                                          credits: {enabled: true} }
                                    );}}
    }},
    lang: {exportButtonTitle: "Export image au format PNG"},
    series: [{
        type: 'pie',
        name: 'Proportion',
        data: [
        ['Activite 1',   684.6],
        ['Activite 2',   564.7],
        ['Activite 3',   244.4],
        ['Activite 4',   42.8],
        ]
    }]
});
});

In the function exportChart, all but the plotOptions gives the right effect. In the PNG file, the title is changed, subtitle and credits are added, but the dataLabels and the legend don't appear...
Anyone knowing why ?
Could anyone help me ? Thanks

like image 871
user1820362 Avatar asked Nov 13 '12 09:11

user1820362


People also ask

How do I add extra legends in highcharts?

So best is to add your extraText in data while JSON creating and provide it to highcharts. At chart , it will show in desired format using the the code I posted in answer/fiddle. putting static text for "Extra" won't work in real scenario, so use the code in fiddle jsfiddle.net/wjnnt28p/3.

What is Highchart legend?

The legend is a box containing a symbol and name for each series item or point item in the chart. Each series (or points in case of pie charts) is represented by a symbol and its name in the legend. It is possible to override the symbol creator function and create custom legend symbols.

What is plotOptions in highcharts?

The plotOptions is a wrapper object for config objects for each series type. The config objects for each series can also be overridden for each series item as given in the series array. Configuration options for the series are given in three levels. Options for all series in a chart are given in the plotOptions.

How do I hide the Highchart watermark?

credits: { enabled: false }, that will remove the "Highcharts.com" text from the bottom of the chart.


1 Answers

Yes it is possible by disabling legend in chart and in exporting parameters (http://api.highcharts.com/highcharts#exporting.chartOptions) set this option as active.

Working example: http://jsfiddle.net/xvQNA/

var chart;
$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false
        },
        title: {
            text: 'Browser market shares at a specific website, 2010'
        },
        tooltip: {
            pointFormat: '{series.name}: <b>{point.percentage}%</b>',
            percentageDecimals: 1
        },
        legend:{
            enabled:false
        },
        exporting:{
            chartOptions:{
                legend:{
                    enabled:true
                }
            }
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: true,
                    color: '#000000',
                    connectorColor: '#000000',
                    formatter: function() {
                        return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                    }
                }
            }
        },
        series: [{
            type: 'pie',
            name: 'Browser share',
            showInLegend:true,
            data: [
                ['Firefox',   45.0],
                ['IE',       26.8],
                {
                    name: 'Chrome',
                    y: 12.8,
                    sliced: true,
                    selected: true
                },
                ['Safari',    8.5],
                ['Opera',     6.2],
                ['Others',   0.7]
            ]
        }]
    });
});
like image 193
Sebastian Bochan Avatar answered Sep 21 '22 00:09

Sebastian Bochan