Question

Iam using Highchart for our project. I created an drilldown chart using AJAX. But now i like to make an button that changes the type of the chart, but the charts stays in its current drilldown and does not need to be reloaded.

function createBarDiagram(div,first,second,type,ytitle,title){

    Highcharts.setOptions({lang: {drillUpText: '◁ Terug naar {series.name}'}});

    if(type == 'pie'){
        var options1 = {                        
            tooltip: {pointFormat: 'Percentage: <b>{point.percentage:.1f}%</b><br>'},       
            xAxis: {categories: true}
        };
    }

    if(type == 'column'){
        var options1 = {
            tooltip: {
                pointFormat: 'Totaal: <b>{point.total}</b><br>'+
                                   'Geel: <b>{point.1}</b><br>'+
                                   'Grijs: <b>{point.2}</b><br>',
                followPointer:true
                                   },
            xAxis: {
                categories: true,
                labels : { rotation: -90}
            }
        };  
    }

    var options = {
        yAxis: {title: {text: ytitle}},     
        chart: {height: 400,
            events: {
                drilldown: function(event) {

                    if (!event.seriesOptions) {

                        // save drilldown status
                        drilldown.push(event.point.name);
                        depth++;                        

                        // show public that the chart is loading by AJAX request
                        chart.showLoading('Laden ...');

                        // array for drilldown data
                        var drilldowndata = new Array;

                        var beginmaand = getStartOrEindmaand('start');
                        var eindmaand = getStartOrEindmaand('eind');
                        var jaar = $('select[name=jaar] option:selected').val();                    
                        var geel = $('input[name="geel"]').prop('checked');                     
                        var grijs = $('input[name="grijs"]').prop('checked');   

                        // get data based on drilldown
                        $.ajax({
                            type: "POST", 
                            async:false,
                            data:{drilldown:drilldown,type:drilldown[0],beginmaand:beginmaand,eindmaand:eindmaand,jaar:jaar,start:start,end:end,geel:geel,grijs:grijs},
                            url:base_url+'wagenparkoverzicht/getdrilldowndata/',
                            success: function(data){

                                // save the data in global array variabele
                                drilldowndata =  JSON.parse(data);                      

                            }
                        });

                        chart.hideLoading();
                        chart.addSeriesAsDrilldown(event.point, drilldowndata); 

                        // show breadcrump
                        var breadcrump = makeBreadCrump();
                        $('#'+div+'_breadcrump').html('('+breadcrump+')');                      
                    }
                },
                drillup: function(event) {
                    depth--;
                    drilldown.splice(depth,1);                                  
                }
            }},
        title: {text: title},
        //drilldown: {series: second},
        drilldown: {
            series: []
        },
        legend: {enabled: false},
        credits: {enabled: false},
        plotOptions: {
            series: {
                dataLabels: {enabled: true},
                shadow: false,
                cursor: 'pointer',
                point: {
                    events: {
                        click: function() { getDetailData(this);}
                    }
                }               
            },
            pie: {size: '70%'}
        },
        series: [{
            name: 'overzicht',
            colorByPoint: true,
            data: first
        }]  
    };  

    options = jQuery.extend(options, options1);
    options.chart.renderTo = div;
    options.chart.type = type;
    var chart = new Highcharts.Chart(options);
    return chart;

}

--- EDIT ---

I have found some example thats works i little bit in this fiddle (http://jsfiddle.net/tccpT/). When using this, the chart changes well from column to pie, but when i click deeper and than click back the charts give an error or shows two charts. 1 column and the pie..?

function changeType(series, newType) {
    var dataArray = [],
        points = series.data;

    series.chart.addSeries({
        type: newType,
        name: series.name,
        color: series.color,
        data: series.options.data
    }, false);

    series.remove();
}

$('#toolbar button').click(function(){
    charttype = $(this).attr('type');
    var series = first.series[0],
      newType = charttype;
    changeType(series, newType);
});
Was it helpful?

Solution

Use series.update(), like this: http://jsfiddle.net/VEaLz/

$("#button").click(function() {
        $("#container").highcharts().series[0].update({ type: 'column' });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top