Zane Claes
Zane Claes

Reputation: 14954

Preventing Text-Clipping in D3 (Javascript Charting)

I'm drawing a pie chart in D3, but having trouble with the text clipping itself:

enter image description here

Here's my draw function:

    pie: function(config)
    {
        var width = config.width || 840,
            height = config.height || 520,
            radius = Math.min(width, height) / 2;

        var color = this._color = d3.scale.ordinal().range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
        var arc = d3.svg.arc().outerRadius(radius - 10).innerRadius(0);
        var pie = d3.layout.pie().sort(null).value(function(d) { return d.value; });

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

          var g = svg.selectAll(".arc").data(pie(config.data)).enter().append("g").attr("class", "arc");
          g.append("path").attr("d", arc).style("fill", function(d) { return color(d.data.name); });

          g.append("text")
              .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
              .attr("dy", ".35em")
              .style("text-anchor", "middle")
              .text(function(d) { return d.data.name; });
        return $('#'+(config.id || 'chart'));
    },

Is there an easy way to prevent such text clipping?

Upvotes: 4

Views: 4308

Answers (1)

musically_ut
musically_ut

Reputation: 34288

Update: See the answer to D3 put arc labels in a Pie Chart if there is enough space for a more comprehensive answer.


If by avoiding clipping you mean that the <text> elements should not be occluded by the ensuing arc, then this can be achieved by making the <text> elements occur after the .arc elements in the DOM. One way of doing it is shown here: http://jsfiddle.net/tu3Pk/2/

Here, I have created a fresh g.arc-labels element which contains the labels and appears in the DOM after g.arcs.

pie: function (config) {

    // ...

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

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

    // Creating a new g for labels
    var gLabel = svg.selectAll('.arc-label')
                  .data(pie(config.data))
                .enter()
                  .append('g')
                  .attr('class', 'arc-label');

    gLabel.append("text")
        .attr("transform", function(d) { 
              return "translate(" + arc.centroid(d) + ")"; 
        })
        .attr("dy", ".35em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.data.name; });

    // ...
}

However, this does not help in legibility much. To make the labels more legible, you might want to take a look at the question: Preventing overlap of text in D3 pie chart in which case, the solution would be on these lines: http://jsfiddle.net/tu3Pk/3/

// Get the angle on the arc and then rotate by -90 degrees
function getAngle(d) {
    var ang = (180 / Math.PI * (d.startAngle + d.endAngle) / 2 - 90);
    return (ang > 180) ? 180 - ang : ang;
};

// ...

pie: function (config) {

    // ...

    gLabel.append("text")
        .attr("transform", function(d) { 
              return "translate(" + arc.centroid(d) + ") " +
                     "rotate(" + getAngle(d) + ")";
        })
        .attr("dy", ".35em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.data.name; });

    // ...
}

Upvotes: 6

Related Questions