0

I am plotting the population information using bar graph in highchart. I fetched the data using Ajax call and store them in a array

 $(document).ready(function(){
        var url = 'zone.php';
        var zone_name = [];
        var male_pop = [];
        var female_pop = [];
        $.getJSON(url, function(data) {
            $.each(data, function(index, data) {
                zone_name.push(data.name);
                male_pop.push(parseInt(data.pop_male));
                female_pop.push(parseInt(data.pop_female));
            });
        });

When I use these data to plot the graph, the graph doesn't show up.

$('#chart').highcharts({
            chart: {
                type: 'bar'
            },
            title: {
                text: 'Popolution of Nepal zone wise'
            },
            subtitle: {
                text: 'Source: cbs.gov.np'
            },
            xAxis: {
                categories: zone_name,
                title: {
                    text: null
                }
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Population',
                    align: 'high'
                },
                labels: {
                    overflow: 'justify'
                }
            },
            tooltip: {
                valueSuffix: ''
            },
            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: [{
                name: 'Male Population',
                data: male_pop
            }, {
                name: 'Female Population',
                data: female_pop
            }, {
                name: 'Total Population',
                data:  male_pop
            }]
        });
        });

The error is in series part. The assignment data: male_pop is not working. How can I overcome this error?

EDIT: output of male_pop array on console is

0: 676960
1: 1122885
2: 990638
3: 1401822
4: 1939350
5: 1500452
6: 706243
7: 1344568
8: 241786
9: 195827
10: 831283
11: 679340
12: 754277
13: 463610
1
  • Console.log the male_pop array and tell us what you've got ?
    – adeneo
    Commented Jul 18, 2013 at 12:59

1 Answer 1

2

I guess the problem is due to asyn nature of $.getJSON, when you call: $('#chart').highcharts({ right after that, the response from the server does not arrive yet. Try changing it to:

$.getJSON(url, function(data) {
            $.each(data, function(index, data) {
                zone_name.push(data.name);
                male_pop.push(parseInt(data.pop_male));
                female_pop.push(parseInt(data.pop_female));
            });

        //    $('#chart').highcharts({
        });
1
  • @Bibek Subedi: you're wellcome, happy coding
    – Khanh TO
    Commented Jul 18, 2013 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.