系列和类别数据不会动态变化

I have these two fiddles - first and second.

in the first one I have written -

var chart = $('#container').highcharts();
chart.xAxis[0].setCategories(['J', 'F', 'M', 'A', 'N']);

this code within first if block and in the second i have written this in a different if block.

My doubt is that why only in the second one the categories are changed on selecting DATA SET B

please guide.

Add this -

$("#list").on('change', function(){
      var selVal = $("#list").val();
    if(selVal == "B")
    {
    var chart = $('#container').highcharts();
    chart.xAxis[0].setCategories(['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D']);
    }
});

here is the fiddle - fiddle

you can remove the button completely and do the following :

var options = {
    chart: {
        renderTo: 'container',
        defaultSeriesType: 'column'
    },
            xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May']
        },
    series: [{name: 'A', data: [1,2,3,2,1]}]
};
var chart = new Highcharts.Chart(options);

$("#list").on('change', function(){
    var options = {
    chart: {
        renderTo: 'container',
        defaultSeriesType: 'column'
    },
            xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May']
        },
    series: [{name: 'A', data: [1,2,3,2,1]}]
};
var chart = new Highcharts.Chart(options);
    //alert('f')
    var selVal = $("#list").val();
    if(selVal == "A" || selVal == '')
    {
        options.series = [{name: 'A', data: [1,2,3,2,1]}]
    }
    else if(selVal == "B")
    {
        options.series = [{name: 'B', data: [3,2,1,2,3]}]

         chart.xAxis[0].setCategories(['r', 'F', 'M', 'A', 'M']);
    }
    else if(selVal == "C")
    {
        options.series = [{name: 'C', data: [5,4,8,7,6]}]
    } 
    else
    {
        options.series = [{name: 'D', data: [4,7,9,6,2]}]
    }  
    var chart = new Highcharts.Chart(options);    
});