Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hyperlink bar graph in highcharts

I have a highcharts that renders data according to data in my database. I am using the type 'bar'. Now i want that when users click on the bar it will redirect to the specific page or forexample another website.I've googled it but couldn't get the answer. Here is the code i am using.

$(function () {
var chart;
$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            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: {
            formatter: function() {
                return ''+
                    this.series.name +': '+ this.y +' millions';
            }
        },
        plotOptions: {
            bar: {
                dataLabels: {
                    enabled: true
                }
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -100,
            y: 100,
            floating: true,
            borderWidth: 1,
            backgroundColor: '#FFFFFF',
            shadow: true
        },
        credits: {
            enabled: false
        },
        series: [{
            type: 'bar',
                    point: {
                      events: {
                        click: function(e) {

                           this.slice();
var clicked = this;
setTimeout(function(){
location.href = clicked.config[2];
                           }, 500)
                           e.preventDefault();
                        }
                     }
                   },
data: [['Com',107,'http://www.google.com']]
        }]
    });
});

});
like image 446
Amir Saeed Avatar asked Oct 04 '12 07:10

Amir Saeed


Video Answer


1 Answers

Here is the url of the documentation on how to do this: http://api.highcharts.com/highcharts#plotOptions.series.point.events.click

Here is a good sample: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/plotoptions/series-point-events-click-url/

The part of your code you want to update is here:

plotOptions: {
        bar: {
            dataLabels: {
                enabled: true
            }
        },
        series: {
            point: {
                events: {
                    click: function(){
                       // do whatever here
                    }
                }
            }
        }

    }
like image 86
Jamiec Avatar answered Oct 13 '22 23:10

Jamiec