【问题标题】:queue.js pass data from local variable not from external filequeue.js 从局部变量而不是外部文件传递数据
【发布时间】:2016-10-25 08:35:11
【问题描述】:

我想使用 json 变量传递数据。 在下面的示例中,json 是从外部 JSON 文件中获取的。 任何人都可以帮助我如何从局部变量传递数据,因为我是 dc.js 的新手

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

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

我试过了

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

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

但没用。

【问题讨论】:

    标签: javascript json d3.js dc.js queue.js


    【解决方案1】:

    如果你有一个局部变量,那么使用 异步调用 来传递它是没有意义的。直接将它作为参数传递:

    var sampleData = [...];//this is your data
    
    makeGraphs(sampleData);//call your function using it as an argument
    

    然后:

    function makeGraphs(data){//this is the parameter
         //use 'data' here
    }
    

    【讨论】:

    • 非常感谢....您的帮助....您能简单地解释一下 queue.js 的真正作用吗?
    • 在 JavaScript 中,当您加载外部文件(如 JSON)时,其后的代码不会等待文件加载,而是立即运行。因此,我们必须将处理文件的代码放在回调函数中。 d3.queue 有一些工具可以处理这种异步代码。因为你有一个变量,所以你不需要它。
    • 是的,但是....我相信回调是为此目的而设计的,仅在加载数据后执行代码是这样吗??...是的...那么什么是需要queue.js ....只是出于好奇,如果我错了,请纠正我,因为我是新人...
    • 还有 dc.js 有什么用?是只在数据量大的情况下使用还是用于创建仪表盘?
    • 你真的不需要使用 d3.queue,它只是更容易处理多个文件和回调。同样,如果您知道如何使用 d3 进行编码,则无需使用 dc。所有这些库(dc、c3、nvd3 等)都是使用 d3 构建的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    相关资源
    最近更新 更多