为什么这个饼图图例d3.js不起作用?

为什么这个饼图图例d3.js不起作用?,d3.js,D3.js,我做了一个饼图,效果很好: var width = 960, height = 500, radius = Math.min(width, height) / 2; var color = d3.scale.ordinal() .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var percentageFormat = d3.format("%"); var arc

我做了一个饼图,效果很好:

var width = 960,
height = 500,
radius = Math.min(width, height) / 2;

var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var percentageFormat = d3.format("%");

var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);

var pie = d3.layout.pie()
.sort(null)
.value(function(d) {
    return d.values;
});

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");


d3.json("staff.json", function(error, json_data) {

var data = d3.nest()
.key(function(d) {
  return d.sex;
})
.rollup(function(d) {
  return d.length;
}).entries(json_data);

data.forEach(function(d) {
d.percentage = d.values / json_data.length;
});

console.log("data variable", data);
console.log("pie(data)", pie(data));

var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc")
.on('mouseover', function() {
  var current = this;  
  var others = svg.selectAll(".arc").filter(function(el) {
    return this != current
  });
  others.selectAll("path").style('opacity', 0.8);
})
.on('mouseout', function() {
  var current = this;
  d3.select(this)
    .style('opacity', 1);
  var others = svg.selectAll(".arc").filter(function(el) {
    return this != current
  });
  others.selectAll("path").style('opacity', 1);
});


g.append("path")
.attr("d", arc)
.style("fill", function(d, i) {
  return color(i);
});

g.append("text")
.attr("transform", function(d) {
  return "translate(" + arc.centroid(d) + ")";
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) {
  console.log("d is", d);
  return percentageFormat(d.data.percentage);
});
});
这是staff.json文件: [{ “职位”:“程序员”, “姓名”:“Giacomo Gulizzoni”, “年龄”:37岁, “性别”:“男性”, “项目”:“智能工厂” }, { “位置”:“测试仪”, “姓名”:“Marko Botton”, “年龄”:34岁, “性别”:“男性”, “项目”:“智能工厂” }, { “位置”:“测试仪”, “姓名”:“玛丽亚·麦克拉契安”, “年龄”:37岁, “性”:“女性”, “项目”:“智能工厂” }, { “位置”:“测试仪”, “姓名”:“Valerie Liberty”, “年龄”:25岁, “性”:“女性”, “项目”:“智能项目” }, { “职位”:“程序员”, “姓名”:“Guido Jack Gulizzoni”, “年龄”:22岁, “性别”:“男性”

我想添加图例,在一个教程之后,我添加了一些代码。之后,它就不起作用了


你能给我一个提示吗?

这是一个教程:这是我的代码:你能给你的问题添加一些代码吗?如果没有这些,就不可能准确地指出问题所在。让短语更清楚一些。作者仍然需要添加代码并解释“它不再工作”的方式.你想画什么?看起来你想根据一个不存在的“位置”属性来确定百分比…你的意思是性别?年龄?哦,对不起,我想给这个饼图添加图例