user1481317
user1481317

Reputation:

Highchart doesn't show the graph : error adding javascript array in series

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

Upvotes: 0

Views: 675

Answers (1)

Khanh TO
Khanh TO

Reputation: 48972

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({
        });

Upvotes: 2

Related Questions