简体   繁体   English

未捕获的类型错误:d3.pack(...).sort 不是函数

[英]Uncaught TypeError: d3.pack(...).sort is not a function

I am new to Javascript and D3.js and I am currently trying to build a bubble chart in d3.js using an example provided here https://jrue.github.io/coding/2014/exercises/basicbubblepackchart/ and modifying it according to my assignment.我是 Javascript 和 D3.js 的新手,我目前正在尝试使用此处提供的示例在 d3.js 中构建气泡图https://jrue.github.io/coding/2014/exercises/basicbubblepackchart/并根据修改它到我的任务。 I know that the example above was written in a previous version of d3, and I am using version v4 where syntax has slightly changed,however I am getting a following error when running a program:我知道上面的例子是用以前版本的 d3 编写的,我使用的是 v4 版本,其中语法略有变化,但是在运行程序时出现以下错误:

Uncaught TypeError: d3.pack(...).sort is not a function未捕获的类型错误:d3.pack(...).sort 不是函数

var diameter = 500, //max size of the bubbles
color    = d3.scaleOrdinal(d3.schemeCategory10); //color category

var bubble = d3.pack()
.sort()
.size([diameter, diameter])
.padding(1.5);

var svg = d3.select("body")
.append("svg")
.attr("width", diameter)
.attr("height", diameter)
.attr("class", "bubble");

d3.csv("teams.csv", function(error, data){

data = data.map(function(d){ d.value = +d["Amount"]; return d; });

var nodes = bubble.nodes({children:data}).filter(function(d) { return !d.children; });

//setup the chart
var bubbles = svg.append("g")
    .attr("transform", "translate(0,0)")
    .selectAll(".bubble")
    .data(nodes)
    .enter();

bubbles.append("circle")
    .attr("r", function(d){ return d.r; })
    .attr("cx", function(d){ return d.x; })
    .attr("cy", function(d){ return d.y; })
    .style("fill", function(d) { return color(d.value); });

bubbles.append("text")
    .attr("x", function(d){ return d.x; })
    .attr("y", function(d){ return d.y + 5; })
    .attr("text-anchor", "middle")
    .text(function(d){ return d["Team"]; })
    .style({
        "fill":"black", 
        "font-family":"Helvetica Neue, Helvetica, Arial, san-serif",
        "font-size": "12px"
    });

What is the problem here?这里有什么问题?

Be sure to use the same version of d3 as the author in the example (it looks like you're good by the syntax).请务必使用与示例中的作者相同版本的 d3(看起来您对语法很好)。 Also, it's 'd3.layout.pack().sort(null)' :)此外,它是 'd3.layout.pack().sort(null)' :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM