2

I want to pass the data using a json variable. In the below example the json is fetched from an external JSON file. can any one help me how to pass the data from a local variable as i am new to dc.js

queue()
    .defer(d3.json, "sampledata.json") // sampledata.json  is an external json file
    .await(makeGraphs);

function makeGraphs() {
   //function which proceses the data
}

i tried this

var sampledata = [ ....];
queue().defer(d3.json, "sampledata.json") // sampledata.json  is an external json file
        .await(makeGraphs);

    function makeGraphs() {
       //function which proceses the data
    }

but did not work.

Rakesh
  • 57
  • 7

1 Answers1

2

If you have a local variable it makes no sense using an asynchronous call to pass it. Just pass it straight away as an argument:

var sampleData = [...];//this is your data

makeGraphs(sampleData);//call your function using it as an argument

And then:

function makeGraphs(data){//this is the parameter
     //use 'data' here
}
Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
  • Thanks a lot....for your help....can you explain in simple terms what does queue.js really do? – Rakesh Oct 25 '16 at 09:04
  • In JavaScript, when you load an external file (like an JSON), the code that comes after it doesn't wait for the file to be loaded, it runs immediately. So, we have to put the code that deals with the file in a callback function. `d3.queue` has some tools for dealing with such asynchronous code. As you have a variable, you don't need it. – Gerardo Furtado Oct 25 '16 at 09:10
  • yes but....i believe that callbacks are designed for this purpose only that to execute code once the data is loaded is this right??...is yes ...then what is the need for queue.js....just asking for curiosity and please correct me if i a wrong as i am a new bie... – Rakesh Oct 25 '16 at 09:30
  • and also what is the use of dc.js? is it used only when their is a large amount of data or to create a dashboard? – Rakesh Oct 25 '16 at 09:46
  • You really don't need to use d3.queue, it just makes easier dealing with several files and callbacks. The same way, you don't need to use dc if you know how to code with d3. All those libraries (dc, c3, nvd3 etc) are built using d3. – Gerardo Furtado Oct 25 '16 at 10:38