Search code examples
javascriptjquerychartshighchartsdrilldown

Highcharts - Dynamically change chart type with on radio buttons click


I am trying to change my working highcharts into different type of chart using 4 radio buttons: column, bar, pie, line.

Here is my working chart:

$(function () {    


// Create the chart

var options = {
    chart: {
       events: {
            drilldown: function (e) {
                if (!e.seriesOptions) {

                    var chart = this,

                        drilldowns = <?php echo json_encode($array) ?>,
                        series = drilldowns[e.point.name];

                    // Show the loading label
                    chart.showLoading('Loading ...');

                    setTimeout(function () {
                        chart.hideLoading();
                        chart.addSeriesAsDrilldown(e.point, series);
                    }, 1000); 
                }

            }
        },
        plotBorderWidth: 0
    },

    title: {
        text: 'Chart Title',
    },
    //
    subtitle: {
            text: 'Subtitle'
    },
    //
    xAxis: {
            type: 'category',
    },
    //
    yAxis: {

            title: {
                margin: 10,
                text: 'No. of user'
            },      
    },
    //
    legend: {
        enabled: true,
    },
    //
    plotOptions: {
        series: {
            pointPadding: 0.2,
            borderWidth: 0,
            dataLabels: {
                enabled: true
            }
        },
        pie: {
            plotBorderWidth: 0,
            allowPointSelect: true,
            cursor: 'pointer',
            size: '100%',
            dataLabels: {
                enabled: true,
                format: '{point.name}: <b>{point.y}</b>'
            }
        }
    },
    //
     series: [{
        name: 'Case',
        colorByPoint: true,
        data:data_array,
    }],
    //
    drilldown: {
        series: []
    }
};

// Column chart
options.chart.renderTo = 'container';
options.chart.type = 'column';
var chart1 = new Highcharts.Chart(options);

});

As you can see a drilldown column type will be the default type of the chart and i want to add radio button where user can choose type of chart. Then I want to disable "LINE" radio button when the chart is in main series state and enable it when in drilldown series state.

Here are my radio buttons:

  <input type="radio" name="mychart" class="mychart" id= "column" value="column" onclick= "chartfunc()">Column
<input type="radio" name="mychart" class="mychart" id= "bar" value="bar" onclick= "chartfunc()">Bar
<input type="radio" name="mychart" class="mychart" id= "pie" value="pie" onclick= "chartfunc()">Pie
<input type="radio" name="mychart" class="mychart" id= "line" value="line" onclick= "chartfunc()">Line

the i add this script:

function chartfunc()
{
var column = document.getElementById('column');
var bar = document.getElementById('bar');
var pie = document.getElementById('pie');
var line = document.getElementById('line');

if(column.checked)
    {
        options.chart.renderTo = 'container';
        options.chart.type = 'column';
        var chart1 = new Highcharts.Chart(options);
    }
else if(bar.checked)
    {
        options.chart.renderTo = 'container';
        options.chart.type = 'bar';
        var chart1 = new Highcharts.Chart(options);
    }
else if(pie.checked)
    {
        options.chart.renderTo = 'container';
        options.chart.type = 'pie';
        var chart1 = new Highcharts.Chart(options);
    }
else
    {
        options.chart.renderTo = 'container';
        options.chart.type = 'line';
        var chart1 = new Highcharts.Chart(options);
    }

}

Is it possible with this script?

P.S. As you can see also the default title of chart is 'Chart Title', I want also to dynamically change it using textbox.. =)


Solution

  • DEMO

    code:

    $(function () {    
    
    
    // Create the chart
    
    var options = {
        chart: {
           events: {
                drilldown: function (e) {
                    if (!e.seriesOptions) {
    
                        var chart = this;
    
    
    
                        // Show the loading label
                        chart.showLoading('Loading ...');
    
                        setTimeout(function () {
                            chart.hideLoading();
                            chart.addSeriesAsDrilldown(e.point, series);
                        }, 1000); 
                    }
    
                }
            },
            plotBorderWidth: 0
        },
    
        title: {
            text: 'Chart Title',
        },
        //
        subtitle: {
                text: 'Subtitle'
        },
        //
        xAxis: {
                type: 'category',
        },
        //
        yAxis: {
    
                title: {
                    margin: 10,
                    text: 'No. of user'
                },      
        },
        //
        legend: {
            enabled: true,
        },
        //
        plotOptions: {
            series: {
                pointPadding: 0.2,
                borderWidth: 0,
                dataLabels: {
                    enabled: true
                }
            },
            pie: {
                plotBorderWidth: 0,
                allowPointSelect: true,
                cursor: 'pointer',
                size: '100%',
                dataLabels: {
                    enabled: true,
                    format: '{point.name}: <b>{point.y}</b>'
                }
            }
        },
        //
         series: [{
            name: 'Case',
            colorByPoint: true,
            data: [3, 2, 1, 3, 4]
        }],
        //
        drilldown: {
            series: []
        }
    };
    
    // Column chart
    options.chart.renderTo = 'container';
    options.chart.type = 'column';
    var chart1 = new Highcharts.Chart(options);
    
    chartfunc = function()
    {
    var column = document.getElementById('column');
    var bar = document.getElementById('bar');
    var pie = document.getElementById('pie');
    var line = document.getElementById('line');
    
    
    if(column.checked)
        {
    
            options.chart.renderTo = 'container';
            options.chart.type = 'column';
            var chart1 = new Highcharts.Chart(options);
        }
    else if(bar.checked)
        {
            options.chart.renderTo = 'container';
            options.chart.type = 'bar';
            var chart1 = new Highcharts.Chart(options);
        }
    else if(pie.checked)
        {
            options.chart.renderTo = 'container';
            options.chart.type = 'pie';
            var chart1 = new Highcharts.Chart(options);
        }
    else
        {
            options.chart.renderTo = 'container';
            options.chart.type = 'line';
            var chart1 = new Highcharts.Chart(options);
        }
    
    }
    
    $('#change_chart_title').click(function(){
    var new_title = $('#chart_title').val();
    var chart = $('#container').highcharts();
    chart.setTitle({ text: new_title });
    
    alert('Chart title changed to '+new_title+' !');
    
    });
        });