Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:22:30+00:00 2026-06-18T15:22:30+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:22:31+00:00Added an answer on June 18, 2026 at 3:22 pm

    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; });
    
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.