3

I am new to this topic and I am trying to build a simple half pie/half donut chart with D3.js v4. The result should be something like the chart in the following example: http://bl.ocks.org/dbuezas/9306799.

My problem is the placement of the labels and the lines to these.

Here is my current code:

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v4.min.js"></script>
<script>

var data = [
    {name: "ABC", val: 11975},
    {name: "DEF", val: 5871},
    {name: "GEH", val: 8916}
];

var margin = {
    top: 40,
    right: 40,
    bottom: 40,
    left: 40
};

var width = 800 - margin.right - margin.left;
var height = 400 - margin.top - margin.bottom;
var radius = 300;

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

svg.append('g')
    .attr('class', 'slices');
svg.append('g')
    .attr('class', 'labels');
svg.append('g')
    .attr('class', 'lines');

var color = d3.scaleOrdinal(d3.schemeCategory20);

var pie = d3.pie()
    .sort(null)
    .value(function (d) {
        return d.val;
    })
    .startAngle(-90 * (Math.PI / 180))
    .endAngle(90 * (Math.PI / 180));

// donut chart arc
var arc = d3.arc()
    .innerRadius(radius - 100)
    .outerRadius(radius - 50);

var slice = svg.select('.slices')
    .selectAll('path.slice')
    .data(pie(data));

slice.enter()
    .append('path')
    .attr('d', arc)
    .attr('fill', function (d) {
        return color(d.data.name);
    })
    .attr('class', 'slice');

var sliceDots = svg.select('.slices')
    .selectAll('circle')
    .data(pie(data));

sliceDots.enter()
    .append('circle')
    .attr('class', 'dot')
    .attr('cx', function (d) {
        return arc.centroid(d)[0];
    })
    .attr('cy', function (d) {
        return arc.centroid(d)[1];
    })
    .attr('r', 2)
    .attr('fill', 'black');

// label arc
var labelArc = d3.arc()
    .innerRadius(radius * 0.9)
    .outerRadius(radius * 0.9);

var labels = svg.select('.labels')
    .selectAll('text')
    .data(pie(data));

labels.enter()
    .append('text')
    .attr('dy', '.35em')
    .attr('text-anchor', 'middle')
    .attr('class', 'labels')
    .text(function (d) {
        return d.data.name;
    })
    .attr('transform', function (d) {
        return 'translate(' + labelArc.centroid(d)[0] + ',' + labelArc.centroid(d)[1] + ')';
    });

// lines
var polyline = svg.select('.lines')
    .selectAll('polyline')
    .data(pie(data));

polyline.enter()
    .append('polyline')
    .attr('stroke-width', '2px')
    .attr('stroke', 'black')
    .attr('opacity', '0.4')
    .attr('points', function (d) {
        return [arc.centroid(d), labelArc.centroid(d)];
    });
 </script>
 </body>

Is there a simple solution for this?

  • I think you should change the **transform** attribute for displaying the labels. – Ha Hoang Mar 22 '17 at 04:42
  • Where are you trying to position the labels? Over the path or outside the path? Also, do you want them to be rotated? – ph0enix Mar 22 '17 at 13:21
  • I am trying to positition the labels over the path and the labels shouldn't be rotated. Maybe is it easier to work with a second arc-object that has a wider radius. –  Mar 22 '17 at 15:02

1 Answers1

1

I believe using a centroid was a good approach, so I just added getTextWidth (from here: Calculate text width with JavaScript) method to determine the length of value label and then move it to the left.

.attr("transform", function (d) {
    var textWidth = getTextWidth(d.data.val.toString(), "Arial");
    return "translate(" + (arc.centroid(d)[0] - textWidth) + "," + arc.centroid(d)[1] + ")"; 
})

Here is a JSFiddle: https://jsfiddle.net/w6d4hcb9/2/

Community
  • 1
  • 1
ph0enix
  • 763
  • 2
  • 8
  • 23
  • 1
    Thank you, but i want the labels outside the donut. I changed my previous solution and added a second arc for the labels. You are moving the position always to the left, but i think it has to depend on the angle: Labels on the left to the left and on the right to right. Here is a JSFiddle of my current Version: https://jsfiddle.net/Lnj00so0/ –  Mar 23 '17 at 10:26