Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从json读取数据时未分配变量_Javascript_D3.js_Assign - Fatal编程技术网

Javascript 从json读取数据时未分配变量

Javascript 从json读取数据时未分配变量,javascript,d3.js,assign,Javascript,D3.js,Assign,我使用以下代码使用D3从json读取数据,并将其传递给另一个函数 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Simple venn.js example</title> </head> <style> div { max-width: 300px; height: 400p

我使用以下代码使用D3从json读取数据,并将其传递给另一个函数

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Simple venn.js example</title>
</head>
<style>
div {
    max-width: 300px;
    height: 400px;
    margin: 0 0 0 0;
    padding: 0 0 0 0;
}
</style>
<body>
    <div id="weighted_example"></div>
</body>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="../venn.js"></script>


<script>
    var sets= [{}];
    d3.json( "json", function( data ) 
    {  
       sets=data;        // reading successfully
    });

    alert(sets);    // but i need to use alert to pass the value to function div.datum(sets) otherwise sets value is null.
    var div =d3.select("#weighted_example")
    div.datum(sets).call(venn.VennDiagram())
</script>
</html>

简单的venn.js示例
div{
最大宽度:300px;
高度:400px;
保证金:0;
填充:0;
}
变量集=[{}];
d3.json(“json”),函数(数据)
{  
set=data;//读取成功
});
警报(组);//但我需要使用alert将值传递给函数div.datum(sets),否则sets值为null。
var div=d3。选择(“加权示例”)
div.datum(set).call(venn.VennDiagram())
json文件:

[{“集合”:[0],“大小”:1958},{“集合”:[1],“大小”:1856},{“集合”:[2],“大小”:1297},{“集合”:[0,1],“大小”:220},{“集合”:[2,0],“大小”:123},{“集合”:[2,1],“大小”:139}]
从逻辑上讲,这似乎不现实。当我在d3.json()函数中使用alert时,它会显示正确的值,因此设置值已经被分配,但如果我不使用alert for set,为什么会传递空值。如何修复此类错误?

d3.json
是一个异步函数,因此当您到达
div.datum
行时,无法确定集合是否准备就绪

您看到它与alert一起工作的原因是alert将停止javascript流,因此集合将具有属性值

您应该将数据代码放在
d3.json
代码中