更新带有标签的饼图(d3)

alexc101

我只是试图附加mbostocks饼图示例的标签,但由于某种原因我无法使其正常工作。

实际上,我只是想结合他的两个示例(pie updatepie labels),所以我试图实现此代码。

var g = svg.selectAll(".arc")
      .data(pie(data))
    .enter().append("g")
      .attr("class", "arc");

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

  g.append("text")
      .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
      .attr("dy", ".35em")
      .text(function(d) { return d.data.apples; });
});

在他的饼图更新示例中放入该变量;

var path = svg.datum(data).selectAll("path")
      .data(pie)
    .enter().append("path")
      .attr("fill", function(d, i) { return color(i); })
      .attr("d", arc)
      .each(function(d) { this._current = d; }); // store the initial angles

这是我尝试的重点,希望有人可以解决我遇到的问题。

http://plnkr.co/edit/e05kKjB8KWGqRteh8OdS?p=preview


为我的尝试编写代码;

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

var color = d3.scale.category20();

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

var arc = d3.svg.arc()
    .innerRadius(radius - 100)
    .outerRadius(radius - 20);

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

d3.tsv("data.tsv", type, function(error, data) {
  if (error) throw error;

/*  var path = svg.datum(data).selectAll("path")
      .data(pie)
    .enter().append("path")
      .attr("fill", function(d, i) { return color(i); })
      .attr("d", arc)
      .each(function(d) { this._current = d; }); // store the initial angles*/


  var g = svg.datum(data).selectAll(".arc")
      .data(pie)
    .enter().append("g")
      .attr("class", "arc")

  g.append("path")
      .attr("d", arc)
      .style("fill", function(d) { return color(d.data.apples); })
            .each(function(d) { this._current = d; }); // store the initial angles


  g.append("text")
      .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
      .attr("dy", ".35em")
      .text(function(d) { return d.data.apples; });

  d3.selectAll("input")
      .on("change", change);

  var timeout = setTimeout(function() {
    d3.select("input[value=\"oranges\"]").property("checked", true).each(change);
  });

  function change() {
    var value = this.value;
    clearTimeout(timeout);
    pie.value(function(d) { return d[value]; }); // change the value function
    path = g.data(pie); // compute the new angles
    path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
  }
});

function type(d) {
  d.apples = +d.apples;
  d.oranges = +d.oranges;
  return d;
}

// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
  var i = d3.interpolate(this._current, a);
  this._current = i(0);
  return function(t) {
    return arc(i(t));
  };
}
标记

当您从的集合移至包含path的集合时,您并未更新转换以在路径上进行操作。它正在尝试将过渡应用于更正的代码:gpathtextg

function change() {
  var value = this.value;
  pie.value(function(d) { return d[value]; }); 
  g = g.data(pie); 
  g.select("path") //<-- operate on the paths in the g
    .transition()
    .duration(750)
    .attrTween("d", arcTween);
}

运行代码:

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

var color = d3.scale.category20();

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

var arc = d3.svg.arc()
  .innerRadius(radius - 100)
  .outerRadius(radius - 20);

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

var data = [{
  "apples": 53245,
  "oranges": 200
}, {
  "apples": 28479,
  "oranges": 200
}, {
  "apples": 19697,
  "oranges": 200
}, {
  "apples": 24037,
  "oranges": 200
}, {
  "apples": 40245,
  "oranges": 200
}];

var g = svg.datum(data).selectAll(".arc")
  .data(pie)
  .enter().append("g")
  .attr("class", "arc");

var path = g.append("path")
  .attr("d", arc)
  .style("fill", function(d) {
    return color(d.data.apples);
  })
  .each(function(d) {
    this._current = d;
  }); // store the initial angles

var text = g.append("text")
  .attr("transform", function(d) {
    return "translate(" + arc.centroid(d) + ")";
  })
  .attr("dy", ".35em")
  .text(function(d) {
    return d.data.apples;
  });

d3.selectAll("input")
  .on("change", change);

function change() {
  var value = this.value;
  pie.value(function(d) {
    return d[value];
  }); // change the value function
  g = g.data(pie); // compute the new angles
  g.select("path")
    .transition()
    .duration(750)
    .attrTween("d", arcTween); // redraw the arcs
  g.select("text")
    .style("opacity", 0)
    .attr("transform", function(d) {
      return "translate(" + arc.centroid(d) + ")";
    })
    .text(function(d) {
      return d.data[value];
    })
    .transition()
    .duration(1000)
    .style("opacity", 1);
}

function type(d) {
  d.apples = +d.apples;
  d.oranges = +d.oranges;
  return d;
}

// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
  var i = d3.interpolate(this._current, a);
  this._current = i(0);
  return function(t) {
    return arc(i(t));
  };
}
<!DOCTYPE html>
<html>

  <head>
    <script data-require="jquery@*" data-semver="2.2.0" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script data-require="d3@*" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
    <link rel="stylesheet" href="style.css" />
  </head>

  <body>
    <form>
  <label><input type="radio" name="dataset" value="apples" checked> Apples</label>
  <label><input type="radio" name="dataset" value="oranges"> Oranges</label>
</form>
    <script src="script.js"></script>

  </body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何更新D3饼图

来自分类Dev

D3多个饼图更新

来自分类Dev

带有工具提示中链接的d3饼图

来自分类Dev

带有角度/水平标签的d3.js饼图

来自分类Dev

饼图在更新时会不断调整自身-D3

来自分类Dev

D3饼图未正确更新

来自分类Dev

带有 d3.v4 的嵌套饼图

来自分类Dev

d3饼图排序过渡

来自分类Dev

带有弯曲标签的d3甜甜圈图

来自分类Dev

D3多个饼图标签

来自分类Dev

如何在D3中为饼图添加标签颜色?

来自分类Dev

d3饼图/甜甜圈图中的标签过渡

来自分类Dev

在D3饼图之外添加标签不起作用

来自分类Dev

d3饼图和甜甜圈图

来自分类Dev

d3饼图和甜甜圈图

来自分类Dev

d3.js 更新饼图

来自分类Dev

使用新的data.json更新d3饼图

来自分类Dev

如何在D3的一页中更新多个饼图?

来自分类Dev

有没有办法在d3中向饼图添加突出显示?

来自分类Dev

绘制带有图例和Pie标签的饼图

来自分类Dev

如何绘制带有文本标签的饼图?

来自分类Dev

如何绘制带有文本标签的饼图?

来自分类Dev

如何在D3饼图上同时更新文本标签的内容和位置

来自分类Dev

d3 +创建具有不同格式的饼图

来自分类Dev

D3带有React Hook的面积图

来自分类Dev

如果有足够的空间,D3将圆弧标签放在饼图中

来自分类Dev

如果有足够的空间,D3将圆弧标签放在饼图中

来自分类Dev

来自本地json变量的d3饼图

来自分类Dev

d3如何补间饼图的内半径