【问题标题】:How to add image at the end of Arc如何在弧的末尾添加图像
【发布时间】:2020-09-09 06:24:21
【问题描述】:

我正在使用 d3 js 我必须在弧的末尾显示图像我如何才能实现以下是我的示例

var total_codes = 8;
var remaining_codes = 4;
var issued = total_codes - remaining_codes;
var coloursArray = ["#128ED2", "#dadada"];
var dataset = {
  privileges: [issued, remaining_codes]
};

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

var color = d3.scale.ordinal()
  .range(coloursArray);

var pie = d3.layout.pie()
  .sort(null);

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

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

var path = svg.selectAll("path")
  .data(pie(dataset.privileges))
  .enter().append("path")
  .attr("fill", function(d, i) {
    return color(i);
  })
  .attr("d", arc);
path.transition().duration(750);

var point = path.node().getPointAtLength(path.node().getTotalLength() / 2);

svg.append("image")
  .attr("cx", point.x)
  .attr("cy", point.y)
  .attr({
    "xlink:href": "http://run.plnkr.co/preview/ckf41wu0g00082c6g6bzer2cc/images/pacman_active_icon.png", //nothing visible
    width: 35,
    height: 36
  });

svg.append("text")
  .attr("dy", ".0em")
  .style("text-anchor", "middle")
  .attr("class", "inside")
  .html(function() {
    return "<tspan x='0' dy='0em'>External</tspan><tspan x='0' dy='1.2em'>Privileges</tspan>";
  }); // Add your code here
<script src="https://d3js.org/d3.v3.min.js"></script>
<div id="donut"></div>

【问题讨论】:

  • 也许是一个相关的答案here?您可以从路径属性中获取位置,然后确定图像的位置。提供代码以便我们提供帮助。
  • 抱歉回复晚了,您可以查看此链接中的示例plnkr.co/edit/jD41S59ZttQPj58p吃豆人图像需要旋转,图像需要显示弧的末端。

标签: d3.js automatic-ref-counting


【解决方案1】:

这有点乏味,但以下工作。

你取pieData 的第一个元素,它表示蓝色弧线。然后使用三角法计算将 pacman 放置在正确位置的偏移量。最后,首先平移它,使其围绕中心旋转,然后旋转所需的量。

我把它从中心放在radius - 15,因为那是30像素宽弧的中间。

var total_codes = 8;
var remaining_codes = 5;
var issued = total_codes - remaining_codes;
var coloursArray = ["#128ED2", "#dadada"];
var dataset = {
  privileges: [issued, remaining_codes]
};

var width = 160,
  height = 160,
  radius = Math.min(width, height) / 2,
  iconSize = 48;

var color = d3.scale.ordinal()
  .range(coloursArray);

var pie = d3.layout.pie()
  .sort(null);

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

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

var pieData = pie(dataset.privileges);
var path = svg.selectAll("path")
  .data(pieData)
  .enter().append("path")
  .attr("fill", function(d, i) {
    return color(i);
  })
  .attr("d", arc);
path.transition().duration(750);

svg
  .append('g')
  .attr('class', 'pacmancontainer')
  .style('transform', function() {
    // the radius of the center of the arc, also the hypothenuse of a triangle
    var meanRadius = radius - 15;
    var angleRadians = pieData[0].endAngle - Math.PI / 2;
    var xOffset = Math.cos(angleRadians) * meanRadius;
    var yOffset = Math.sin(angleRadians) * meanRadius;
    return " translate(" + xOffset + "px, " + yOffset + "px)";
  })
  .append("image")
  .attr({
    "xlink:href": "http://run.plnkr.co/preview/ckf41wu0g00082c6g6bzer2cc/images/pacman_active_icon.png", //nothing visible
    width: iconSize,
    height: iconSize
  })
  // Make sure the Pacman rotates around its center
  .style('transform-origin', (iconSize / 2) + 'px ' + (iconSize / 2) + 'px')
  .style('transform', function() {
    var angleDegrees = pieData[0].endAngle / (2 * Math.PI) * 360;
    return "translate(-" + (iconSize / 2) + "px, -" + (iconSize / 2) + "px) rotate(" + angleDegrees + "deg)";
  });

svg.append("text")
  .attr("dy", ".0em")
  .style("text-anchor", "middle")
  .attr("class", "inside")
  .html(function() {
    return "<tspan x='0' dy='0em'>External</tspan><tspan x='0' dy='1.2em'>Privileges</tspan>";
  }); // Add your code here
<script src="https://d3js.org/d3.v3.min.js"></script>
<div id="donut"></div>

【讨论】:

  • 非常感谢 Ruben Helsloot 工作正常,您节省了我的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 2017-06-15
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多