HighchartsExtension: LegendToggle - Demo1

https://bitbucket.org/jkowalleck/highcharts-legendextension

by J. Albert Bowden

HTML

<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<input type="button" value="toggle legend" id="toggleBtn" />
<input type="button" value="hide legend" id="hideBtn" />
<input type="button" value="show legend" id="showBtn" />
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

JavaScript

/*
 Extention for Highcharts3 and Highstocks1.3 to add the Chart some functions: 
 legendHide() , legendShow() , legendToggle() 
 
 @author jan Kowalleck <[email protected]>
 @source https://bitbucket.org/jkowalleck/highcharts-legendextention/
*/
(function(b,a){if(!b){return}var c=b.Chart.prototype,d=b.Legend.prototype;b.extend(c,{legendSetVisibility:function(h){var i=this,k=i.legend,e,g,j,m=i.options.legend,f,l;if(m.enabled==h){return}m.enabled=h;if(!h){d.destroy.call(k);e=k.allItems;if(e){for(g=0,j=e.length;g<j;++g){e[g].legendItem=a}}k.group={}}c.render.call(i);if(!m.floating){f=i.scroller;if(f&&f.render){l=i.xAxis[0].getExtremes();f.render(l.min,l.max)}}},legendHide:function(){this.legendSetVisibility(false)},legendShow:function(){this.legendSetVisibility(true)},legendToggle:function(e){if(typeof e!="boolean"){e=(this.options.legend.enabled^true)}this.legendSetVisibility(e)}})}(Highcharts));


$(function () {
    $('#container').highcharts({
        chart: {
            type: 'bar'
        },
        title: {
            text: 'Historic World Population by Region'
        },
        subtitle: {
            text: 'Source: Wikipedia.org'
        },
        xAxis: {
            categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania'],
            title: {
                text: null
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Population (millions)',
                align: 'high'
            },
            labels: {
                overflow: 'justify'
            }
        },
        tooltip: {
            valueSuffix: ' millions'
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -40,
            y: 100,
            floating: true,
           ...