Sunday, 18 August 2013

How to load data using XHR in D3

How to load data using XHR in D3

I am experimenting with D3 on my localhost. I would like to load data to
be visualized from an external source on the Internet.
I am looking for instruction/examples of how to how to load data using XHR
in D3 (there is documentation here, but it does not suffice for me, I am
looking for code snippets https://github.com/mbostock/d3/wiki/Requests)
I have tried the following and it does not work for me:
d3.xhr("http://example.org/json-test.json", function(data){
alert(data); //no data is returned
});
Thanks

2 comments:

  1. d3.json( url, function( error, data ) {
    if ( error ) alert('loading failed');
    alert(data);
    } );

    Callback arguments:
    1 - error if any or null
    2 - data if any

    ReplyDelete
  2. d3.xhr( '/audio/mp3' )
        .responseType('arraybuffer')
        .get( function( error, data ) {
            if ( error ) alert('error');
            loaded_array_buffer = data.response;
    } );

    ReplyDelete