Search code examples
javascriptjqueryhighchartsdrilldown

Highcharts: Drilldown from map to chart


Please consider Kuwait map:

Kuwait Map

I want when a user clicked on one state (for example - Al Jahrah) then a column chart appears for Al Jahrah series. For example:

Category For: Al Jahrah            Value For: Al Jahrah 
--------------------------------------------------------
Cat1                               10
Cat2                               12
Cat3                               3

I searched via the internet but I only saw country drill down to states. Is it possible?

Thanks


Solution

  • You can create two functions that will be responsible for creating a different type of chart and call them in the right event:

    function mapChart() {
        Highcharts.mapChart('container', {
            chart: {
                map: 'countries/kw/kw-all'
            },
    
            title: {
                text: 'Highmaps basic demo'
            },
    
            subtitle: {
                text: 'Source map: <a href="http://code.highcharts.com/mapdata/countries/kw/kw-all.js">Kuwait</a>'
            },
    
            mapNavigation: {
                enabled: true,
                buttonOptions: {
                    verticalAlign: 'bottom'
                }
            },
    
            colorAxis: {
                min: 0
            },
    
            series: [{
                data: data,
                name: 'Random data',
                point: {
                    events: {
                        click: function() {
                            columnChart(this.name);
                            document.getElementById("back").style.display = "block"
                        }
                    }
                },
                states: {
                    hover: {
                        color: '#BADA55'
                    }
                },
                dataLabels: {
                    enabled: true,
                    format: '{point.name}'
                }
            }]
        });
    
        document.getElementById("back").style.display = "none";
    }
    
    function columnChart(name) {
        var data = [1, 2, 3];
    
        if (name === "Al Jahrah") {
            data = [2, 2, 2];
        }
    
        Highcharts.chart('container', {
            title: {
                text: name
            },
            series: [{
                type: 'column',
                data: data
            }]
        });
    }
    
    mapChart();
    

    Live demo: https://jsfiddle.net/BlackLabel/bnsjgt4z/

    Below you can find another solutions with drilldown from map to chart:

    https://www.highcharts.com/maps/demo/rich-info

    https://www.highcharts.com/maps/demo/data-class-two-ranges