How to use a callback to load json data

2,064 views
Skip to first unread message

Daniel Carpintero Abril

unread,
Jul 14, 2014, 11:14:21 AM7/14/14
to c3...@googlegroups.com
Hi, I'm trying to load data from a web service using JSON, the scrip I test is this one.

var chart = c3.generate({
        bindto: '#chart',
        size: {
            width: 400
        },
        data: {
            json: function () {
                $.ajax({....}); //jquery ajax callback
            },
            mimeType: 'json',
            keys: {
                value: ['data1', 'data2']
            }
        }
    });

Thanx for all

Daniel Carpintero

Karsten Jeschkies

unread,
Jul 15, 2014, 5:19:30 AM7/15/14
to c3...@googlegroups.com
Hi,

could you use just the URL as described here (http://c3js.org/samples/data_url.html)? Or do you need some special handling?

Daniel Carpintero Abril

unread,
Jul 15, 2014, 7:18:30 AM7/15/14
to c3...@googlegroups.com
Thanks but url is not working for me, I need a Post method to call a web service with parameters, but finaly I found a solution by myself.

var parameters = {
parval: "valor"
};

$.ajax({
url: "../WebServices/WSname.asmx/GetMethod",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(parameters),
success: function (data) {
var datos = jQuery.parseJSON(data.d);
var testJson = $.map(datos.rows, function (item) {
return {
data1: item.value1,
data2: item.value2
}
});

var chart = c3.generate({
bindto: '#chart',
size: {
width: 400
},
data: {

json: testJson,


mimeType: 'json',
keys: {
value: ['data1', 'data2']

},
names: {
data1: 'Value 1',
data2: 'Value 2'
}
}
});

}
});

alic...@gmail.com

unread,
Dec 3, 2014, 8:08:48 AM12/3/14
to c3...@googlegroups.com
Hi Daniel,

I am trying to adopt your logic to pull data off from asmx webservice.
My service is returning following:

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">[{"name":"Actual","month1":0.82,"month2":0.83,"month3":0.8,"month4":0.79,"month5":0.78,"month6":0.78},{"name":"Pipeline","month1":0.83,"month2":0.84,"month3":0.81,"month4":0.8,"month5":0.8,"month6":0.79},{"name":"Factored","month1":0.87,"month2":0.9,"month3":0.87,"month4":0.85,"month5":0.85,"month6":0.84}]</string>

When it gets to parse the data, I think it isn't working.
jQuery.parseJSON(data.d)

When I try to alert the datos.rows it gives me undefined.

Please review and let me know where I am going wrong.
OR, if you could send me a sample app - that would be great.

Thanks

Ali

Daniel Carpintero Abril

unread,
Dec 5, 2014, 12:25:28 PM12/5/14
to c3...@googlegroups.com, alic...@gmail.com
try this, Basically it is the same but you do not need to use "row", the important thing is mapped correctly the json

var parameters = {
    parval: "value"
 };

$.ajax({
        url: "../WebServices/WSname.asmx/GetMethod",
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(parameters),
        success: function (data) {

                var myData = jQuery.parseJSON(data.d);
                var testJson = $.map(myData, function (item) {
                    return {
                        data1: item.name,
                        data2: item.month1


                    }
                });

                var chart = c3.generate({
                    bindto: '#chart',
                    size: {
                        width: 400
                    },
                    data: {
                        json: testJson,

                        mimeType: 'json',
                        keys: {
                            value: ['data1', 'data2']
                        },
                        names: {
                            data1: 'Value 1',
                            data2: 'Value 2'
                        }
                    }
                });

        }
    });


alic...@gmail.com

unread,
Dec 7, 2014, 3:17:37 AM12/7/14
to c3...@googlegroups.com, alic...@gmail.com
Thank you so much Daniel - you're a star :)

It was all about data formatting!

jifa...@gmail.com

unread,
Feb 24, 2016, 6:34:13 AM2/24/16
to c3js, alic...@gmail.com
On Sunday, December 7, 2014 at 1:47:37 PM UTC+5:30, alic...@gmail.com wrote:
> Thank you so much Daniel - you're a star :)
>
> It was all about data formatting!
json explain please
Reply all
Reply to author
Forward
0 new messages