javascript 获取 TypeError: .selectAll(...).enter 不是 d3.js 的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23829605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:38:18  来源:igfitidea点击:

Getting TypeError: .selectAll(...).enter is not a function with d3.js

javascriptd3.js

提问by Thierry

I'm getting an error that I really don't understand. I'm trying to use the pie chart describe here https://gist.github.com/enjalot/1203641and add it as an option in my program.

我收到一个我真的不明白的错误。我正在尝试使用此处描述的饼图https://gist.github.com/enjalot/1203641并将其添加为我的程序中的一个选项。

function chart (div) {


var width = 300,
height = 300, 
radius = 100, 
color = d3.scale.category20c();


div.each(function() {

  var div = d3.select(this);
  var   g = div.select('g');

  var vis,arc,pie,arcs;

  if (g.empty()) {

    vis = div.append("svg:svg")
            .data(group.top(18))
            .classed('barchart', true)
            .attr("width",width + margin.left + margin.right)
            .attr('height', height + margin.top + margin.bottom)
            .append('svg:g')
            .attr('transform', 'translate(' + (margin.left+radius) + ',' + (margin.top+radius) + ')');

    arc = d3.svg.arc() 
            .outerRadius(radius);

    pie = d3.layout.pie()
            .value(function(d) { return d.value; }); 

    arcs = vis.selectAll("g.slice") 
            .enter() 
            .append("svg:g")
            .attr("class", "slice");

    arcs.append("svg:path")
            .attr("fill", function(d, i) { return color(i); } )
            .attr("d", arc); 

    arcs.append("svg:text")
            .attr("transform", function(d) {
              d.innerRadius = 0;
              d.outerRadius = radius;
              return "translate(" + arc.centroid(d) + ")"; 
            })
            .attr("text-anchor", "middle");
            //.text(function(d, i) { return data[i].label; });
  }

When I run my script I get this: TypeError: vis.selectAll(...).enter is not a function In my index.html I have d3.js so it should be able to access the resource.

当我运行我的脚本时,我得到这个: TypeError: vis.selectAll(...).enter is not a function 在我的 index.html 我有 d3.js 所以它应该能够访问资源。

I'm at a loss here so if you guys could help me that would be great ! Thanks a lot and have a nice day !

我在这里不知所措,所以如果你们能帮助我,那就太好了!非常感谢,祝你有美好的一天!

回答by gautam1168

After calling the selectAll()and before calling enter()you must attach data to the objects that will be created by calling data(). For example:

在调用之后selectAll()和调用之前,enter()您必须将数据附加到将通过调用创建的对象data()。例如:

 arcs = vis.selectAll("g.slice") 
            .data(dataset)
            .enter() 
            .append("svg:g")
            .attr("class", "slice");

where dataset is an array of the data, for example:

其中 dataset 是一个数据数组,例如:

dataset = [1,2,3,4]