JavaScript D3 Flip label pie chart [duplicate]

Is it possible to show the text not upside down in this case?

http://jsfiddle.net/paulocoelho/Hzsm8/1/

Code:

var cfg = {
    w:400,
    h:400
};

var g = d3.select("#testdiv").append("svg").attr("width", cfg.w).attr("height", cfg.h).append("g")

var arct = d3.svg.arc()
        .innerRadius(cfg.h / 5)
        .outerRadius(cfg.h / 3)
        .startAngle(Math.PI/2)
        .endAngle(Math.PI*1.5);

var path = g.append("svg:path")
    .attr("id","yyy")
    .attr("d", arct)
    .style("fill","blue")
    .attr("transform", "translate("+cfg.w/2+","+cfg.h/6+")");

var text = g.append("text")
            .style("font-size",30)
            .style("fill","#F8F8F8")
            .attr("dy",35)
            .append("textPath")
            .attr("xlink:href","#yyy")
            .attr("startOffset",50)
            .text("some text")
    ;
Answer:1

A great example is Placing Texts on Arcs with D3.js by Nadieh Bremer. A lengthy blog with many images from which the following is an extract:

Flipping the Text on the Bottom Half

You could already feel like it’s finished with that look. But I find those labels along the bottom half, that are upside down, rather hard to read. I’d prefer it if those labels were flipped, so I can read them from left to right again.

To accomplish this, we need to switch the start and end coordinates of the current arc paths along the bottom half so they are drawn from left to right. Furthermore, the sweep-flag has to be set to 0 to get the arc that runs in a counterclockwise fashion from left to right

So for the final act, let’s add a few more lines of code to the .each() statement

//Create the new invisible arcs and flip the direction for those labels on the bottom half
.each(function(d,i) {
    //Search pattern for everything between the start and the first capital L
    var firstArcSection = /(^.+?)L/;    

    //Grab everything up to the first Line statement
    var newArc = firstArcSection.exec( d3.select(this).attr("d") )[1];
    //Replace all the commas so that IE can handle it
    newArc = newArc.replace(/,/g , " ");

    //If the end angle lies beyond a quarter of a circle (90 degrees or pi/2) 
    //flip the end and start position
    if (d.endAngle > 90 * Math.PI/180) {
        var startLoc    = /M(.*?)A/,        //Everything between the capital M and first capital A
            middleLoc   = /A(.*?)0 0 1/,    //Everything between the capital A and 0 0 1
            endLoc      = /0 0 1 (.*?)$/;   //Everything between the 0 0 1 and the end of the string (denoted by $)
        //Flip the direction of the arc by switching the start and end point (and sweep flag)
        var newStart = endLoc.exec( newArc )[1];
        var newEnd = startLoc.exec( newArc )[1];
        var middleSec = middleLoc.exec( newArc )[1];

        //Build up the new arc notation, set the sweep-flag to 0
        newArc = "M" + newStart + "A" + middleSec + "0 0 0 " + newEnd;
    }//if

    //Create a new invisible arc that the text can flow along
    svg.append("path")
        .attr("class", "hiddenDonutArcs")
        .attr("id", "donutArc"+i)
        .attr("d", newArc)
        .style("fill", "none");
});

The only thing that has changed since the previous section is the addition of the if statement. To flip the start and end positions, we can use a few more regular expressions. The current starting x and y location is given by everything in between the capital M and the capital A. The current radius is denoted by everything in between the capital A and the 0 0 1 of the x-axis rotation, large-arc flag and sweep flag. Finally the end location is given by all in between the 0 0 1 and the end of the string (denoted by a $ in regex).

So we save all the pieces in different variables and build/replace up the newArc using the final line in the if statement which has switched the start and end position.

The textPath section needs a small change. For the bottom half arcs, the dy attribute shouldn’t raise the labels above the arc paths, but lower the labels below the arc paths. So we need a small if statement which will result in two different dy values. (To be able to use the d.endAngle in the if statement I replaced the donutData by pie(donutData) in the .data() step. You can still reference the data itself by using d.data instead of just d, which you can see in the .text() line of code.)

//Append the label names on the outside
svg.selectAll(".donutText")
    .data(pie(donutData))
   .enter().append("text")
    .attr("class", "donutText")
    //Move the labels below the arcs for those slices with an end angle greater than 90 degrees
    .attr("dy", function(d,i) { return (d.endAngle > 90 * Math.PI/180 ? 18 : -11); })
   .append("textPath")
    .attr("startOffset","50%")
    .style("text-anchor","middle")
    .attr("xlink:href",function(d,i){return "#donutArc"+i;})
    .text(function(d){return d.data.name;});
Answer:2

I have this object Object { "87291": "valid", "1873681927": "valid", "nwq89j8jw0qj9": "valid", "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid", } what i want to do is remove object based on key ...

I have this object Object { "87291": "valid", "1873681927": "valid", "nwq89j8jw0qj9": "valid", "oVFYfWIUOsONE6JyMGYAbnsPMAr1": "valid", } what i want to do is remove object based on key ...

  1. javascript object array find
  2. javascript object array sort
  3. javascript object array filter
  4. javascript object array contains
  5. javascript object array to string
  6. javascript object array get value by key
  7. javascript object array length
  8. javascript object array iterator
  9. javascript object array as key
  10. javascript object array indexof
  11. javascript object array foreach
  12. javascript object array example
  13. javascript object array map
  14. javascript object array notation
  15. javascript object array to json
  16. javascript object array remove duplicates
  17. javascript object array search
  18. javascript object array find index
  19. javascript object array add item
  20. javascript object array to csv

Is there an efficient way to toggle the Boolean value of multiple variables at once? My variables are lines that I want to show/hide depending on userValue. myList = [lineA, lineB, lineC, lineD] My ...

Is there an efficient way to toggle the Boolean value of multiple variables at once? My variables are lines that I want to show/hide depending on userValue. myList = [lineA, lineB, lineC, lineD] My ...

I am using Webshim to standardise datepickers cross-browser. That's great, but unfortunately it also replaces other form elements, which I am not interested in. Is there a way I can limit webshim/...

I am using Webshim to standardise datepickers cross-browser. That's great, but unfortunately it also replaces other form elements, which I am not interested in. Is there a way I can limit webshim/...

I am building a REST API in NodeJS. I am building the server-side pagination, sorting and filtering. I have a simple implementation of the filter method. This works if the item does not contain any ...

I am building a REST API in NodeJS. I am building the server-side pagination, sorting and filtering. I have a simple implementation of the filter method. This works if the item does not contain any ...

  1. javascript filter array of objects
  2. javascript filter array of objects by value
  3. javascript filter array of objects by key value
  4. javascript filter array by value
  5. javascript filter array by another array
  6. javascript filter array based on another array
  7. javascript filter array of objects by property
  8. javascript filter array by index
  9. javascript filter array of arrays
  10. javascript filter array of strings
  11. javascript filter array example
  12. javascript filter array multiple conditions
  13. javascript filter array unique
  14. javascript filter array into two arrays
  15. javascript filter array of objects by another array
  16. javascript filter array duplicates
  17. javascript filter array of objects unique
  18. javascript filter array of objects by multiple values
  19. javascript filter array by multiple values
  20. javascript filter array of objects by date