Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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
D3.js d3追加并输入问题_D3.js_Svg - Fatal编程技术网

D3.js d3追加并输入问题

D3.js d3追加并输入问题,d3.js,svg,D3.js,Svg,所以我在学习一个非常简单的d3教程时遇到了一个问题。实际上,我试图通过将数据绑定到当前SVG圆圈元素,然后调用enter.append来添加SVG圆圈元素,如下所示: var svg =d3.select("body").selectAll("svg"); var circle = svg.selectAll("circle"); var w=window.innerWidth; console.log(circle); circle.data([500,57,112,200,600,1000]

所以我在学习一个非常简单的d3教程时遇到了一个问题。实际上,我试图通过将数据绑定到当前SVG圆圈元素,然后调用enter.append来添加SVG圆圈元素,如下所示:

var svg =d3.select("body").selectAll("svg");
var circle = svg.selectAll("circle");
var w=window.innerWidth;
console.log(circle);
circle.data([500,57,112,200,600,1000]);


circle.enter().append("circle")
    .attr("cy",function(d) {
        return (d)
        })
    .attr("cx", function(i){
        return (i*100)
    })
    .attr("r", function(d) {
        return Math.sqrt(d);
        })
这似乎会添加3个新的圆圈元素(因为我已经创建了3个)。但是,我没有添加这3个新的圆圈元素,而是遇到了以下错误:

未捕获类型错误:对象[Object SVGCircleElement]、[objectSVGCircleElement]、[Object SVGCircleElement]没有方法“enter”

我对段落做了基本相同的事情,似乎效果不错:

var p =d3.select("body").selectAll("p")
.data([4, 8, 15, 16, 23, 42])
.text(function(d) { return "I'm number " + d + "!"; });

 //Enter    
 p.enter().append("p")
.text(function(d) { return "I'm number " + d + "!"; })
.style("color", function(d, i) {
 return i % 2 ? "#000" : "#eee";
});
但是,只要我尝试向其中添加SVG元素,就会继续出现相同的错误

看起来应该只是语法错误之类的,但我已经对代码进行了5万亿次的检查,什么都找不到

非常感谢您的帮助,感谢您抽出宝贵的时间


Isaac

您想调用
enter
查看
圆圈的结果。数据
而不是
圆圈
本身

var circle = svg.selectAll("circle");
circle.data([500,57,112,200,600,1000]).enter().append("circle");

p
示例中,通过将
数据的返回存储在
p
变量中,您正确地做到了这一点。而在您的
圆圈
示例中,您正在
圆圈
变量中存储返回的
d3.select.selectAll

非常感谢!这消除了我的错误,而且非常合理。你已经救了我两次了,奥尔森先生@我想没问题。如果这个问题没有其他问题,请接受答案。对不起,花了这么长时间,我试着接受,但一个小时都不让我分心,忘记了。我道歉!