jquery - d3js饼图的响应线标签

标签 jquery css html d3.js

目前我正在研究 d3js 饼图。

我希望我的饼图具有响应线标签。 任何人都可以指导我如何实现它。

这是我试过的。但是当值较小时,标签会重叠,如下面的链接所示。

Fiddle

 var dataset = {
  apples: [53245, 28479, 19697, 537, 245],
};

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

var color = d3.scale.category20();

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

var piedata = pie(dataset.apples);

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

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

var path = svg.selectAll("path")
    .data(piedata)
  .enter().append("path")
    .attr("fill", function(d, i) { return color(i); })
    .attr("d", arc);

svg.selectAll("text").data(piedata)
    .enter()
    .append("text")
    .attr("text-anchor", "middle")
    .attr("x", function(d) {
        var a = d.startAngle + (d.endAngle - d.startAngle)/2 - Math.PI/2;
        d.cx = Math.cos(a) * (radius - 75);
        return d.x = Math.cos(a) * (radius - 20);
    })
    .attr("y", function(d) {
        var a = d.startAngle + (d.endAngle - d.startAngle)/2 - Math.PI/2;
        d.cy = Math.sin(a) * (radius - 75);
        return d.y = Math.sin(a) * (radius - 20);
    })
    .text(function(d) { return d.value; })
    .each(function(d) {
        var bbox = this.getBBox();
        d.sx = d.x - bbox.width/2 - 2;
        d.ox = d.x + bbox.width/2 + 2;
        d.sy = d.oy = d.y + 5;
    });

svg.append("defs").append("marker")
    .attr("id", "circ")
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("refX", 3)
    .attr("refY", 3)
    .append("circle")
    .attr("cx", 3)
    .attr("cy", 3)
    .attr("r", 3);

svg.selectAll("path.pointer").data(piedata).enter()
    .append("path")
    .attr("class", "pointer")
    .style("fill", "none")
    .style("stroke", "black")
    .attr("marker-end", "url(#circ)")
    .attr("d", function(d) {
        if(d.cx > d.ox) {
            return "M" + d.sx + "," + d.sy + "L" + d.ox + "," + d.oy + " " + d.cx + "," + d.cy;
        } else {
            return "M" + d.ox + "," + d.oy + "L" + d.sx + "," + d.sy + " " + d.cx + "," + d.cy;
        }
    });

最佳答案

您可以使用 CSS3 旋转标签。

transform: rotate(90deg);

或者只使用这个库: http://d3pie.org/

关于jquery - d3js饼图的响应线标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32199009/

相关文章:

jquery - 如何使用标记创建范围 slider 并更改填充范围的颜色?

javascript - Bootstrap 响应 : Show multiple div in collapse mode

javascript - 单击事件删除表单中的隐藏值

html - Canvas 顶部 <div> 中的文本 - bootstrap

javascript - 如何阻止 Chrome 和 Firefox 按字母顺序按键对 sessionStorage 进行排序?

javascript - 如何让 jQuery 在每次悬停时识别鼠标悬停在图像上一次

javascript - 如何为文本之间的某些字符添加文本样式

javascript - Socket.io & Redis - 适合 io 游戏的架构?

java - 更改从 servlet 类返回的 div 类位置

javascript - 更改可见性属性