Skip to content

Instantly share code, notes, and snippets.

@dbuezas
Last active November 21, 2024 02:11
Show Gist options
  • Save dbuezas/9306799 to your computer and use it in GitHub Desktop.
Save dbuezas/9306799 to your computer and use it in GitHub Desktop.
Pie charts labels

This variation of a donut chart demonstrates how to add labels with lines. Clicking on the button changes the displayed data. Check Pie Chart with Labels and Missing Data to see how to handle transitions with missing/new data.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
width: 960px;
height: 500px;
position: relative;
}
svg {
width: 100%;
height: 100%;
}
path.slice{
stroke-width:2px;
}
polyline{
opacity: .3;
stroke: black;
stroke-width: 2px;
fill: none;
}
</style>
<body>
<button class="randomize">randomize</button>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var svg = d3.select("body")
.append("svg")
.append("g")
svg.append("g")
.attr("class", "slices");
svg.append("g")
.attr("class", "labels");
svg.append("g")
.attr("class", "lines");
var width = 960,
height = 450,
radius = Math.min(width, height) / 2;
var pie = d3.layout.pie()
.sort(null)
.value(function(d) {
return d.value;
});
var arc = d3.svg.arc()
.outerRadius(radius * 0.8)
.innerRadius(radius * 0.4);
var outerArc = d3.svg.arc()
.innerRadius(radius * 0.9)
.outerRadius(radius * 0.9);
svg.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var key = function(d){ return d.data.label; };
var color = d3.scale.ordinal()
.domain(["Lorem ipsum", "dolor sit", "amet", "consectetur", "adipisicing", "elit", "sed", "do", "eiusmod", "tempor", "incididunt"])
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
function randomData (){
var labels = color.domain();
return labels.map(function(label){
return { label: label, value: Math.random() }
});
}
change(randomData());
d3.select(".randomize")
.on("click", function(){
change(randomData());
});
function change(data) {
/* ------- PIE SLICES -------*/
var slice = svg.select(".slices").selectAll("path.slice")
.data(pie(data), key);
slice.enter()
.insert("path")
.style("fill", function(d) { return color(d.data.label); })
.attr("class", "slice");
slice
.transition().duration(1000)
.attrTween("d", function(d) {
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
return arc(interpolate(t));
};
})
slice.exit()
.remove();
/* ------- TEXT LABELS -------*/
var text = svg.select(".labels").selectAll("text")
.data(pie(data), key);
text.enter()
.append("text")
.attr("dy", ".35em")
.text(function(d) {
return d.data.label;
});
function midAngle(d){
return d.startAngle + (d.endAngle - d.startAngle)/2;
}
text.transition().duration(1000)
.attrTween("transform", function(d) {
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
var d2 = interpolate(t);
var pos = outerArc.centroid(d2);
pos[0] = radius * (midAngle(d2) < Math.PI ? 1 : -1);
return "translate("+ pos +")";
};
})
.styleTween("text-anchor", function(d){
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
var d2 = interpolate(t);
return midAngle(d2) < Math.PI ? "start":"end";
};
});
text.exit()
.remove();
/* ------- SLICE TO TEXT POLYLINES -------*/
var polyline = svg.select(".lines").selectAll("polyline")
.data(pie(data), key);
polyline.enter()
.append("polyline");
polyline.transition().duration(1000)
.attrTween("points", function(d){
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
var d2 = interpolate(t);
var pos = outerArc.centroid(d2);
pos[0] = radius * 0.95 * (midAngle(d2) < Math.PI ? 1 : -1);
return [arc.centroid(d2), outerArc.centroid(d2), pos];
};
});
polyline.exit()
.remove();
};
</script>
</body>
@Bjarkemonsted
Copy link

Hello David,
Excellent transitions! I managed to get external data, and refresh on click, but I have a problem making it work in "Chrome" and "Firefox" browsers, the view windows is only in the upper 1/3 left corner, not showing the full graph. I tried adding .attr to the d3.select, but that didnt work. Any suggestions would be appreciated :)

@nunoooo
Copy link

nunoooo commented Dec 4, 2015

hello,
Amazing graph cheers for this! But how to prevent that the labels overlap? i tried several solutions from stack overflow but none of the seem to work properly.
Good work cheers

@cristianfierro
Copy link

Any news about the overlapping issue?

@yolo-ling
Copy link

hello!Ask a question!How to deal with the problem of overlapping text?

@kiranb555
Copy link

yes, the same overlapping problem. do any have a solution in 2019?

@mast4461
Copy link

mast4461 commented Jun 4, 2021

Nice!

I made a port to Observable, with credit and links to you and this bl.ock and gist.
https://observablehq.com/@mast4461/d3-donut-chart-labels

@dbuezas
Copy link
Author

dbuezas commented Jun 4, 2021

yes, the same overlapping problem. do any have a solution in 2019?

IIRC, what I did at the time was to keep track of the y axis of each label and hide the the ones that overlapped. You could pre-sort them by % to make sure the important slices get priority. The code lives in some client page so I can't share that (I don't even have it anymore)

I made a port to Observable, with credit and links to you and this bl.ock and gist.

Cool! Amazing to hear there's interest on this after 7 years :)

@dipenparmar12
Copy link

Thanks David Buezas for amazing gist I love it.

@dipenparmar12
Copy link

dipenparmar12 commented Feb 15, 2023

Is there any way to make it responsive (Mobile, Tablet and Desktop)
i attempted to do it, but i am not succeed, Labels are overlapping with each other, is there any approach to handle this sicario?
Thanks everyone.

image

@dbuezas
Copy link
Author

dbuezas commented Feb 16, 2023

To make it responsive, you'll need to add a resize observer, change the values of width and height and then run the whole code.
Regarding overlapping, see a couple of posts above.

@Andrei-Fogoros
Copy link

Hello @dbuezas,

What is the license for the code above? Or if you copied it from somewhere, can you give me the source?

Thanks in advance! :)

@dbuezas
Copy link
Author

dbuezas commented Mar 23, 2023

I authored the code.

Let's make it MIT

Copyright David Buezas, 2023

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

@dbuezas
Copy link
Author

dbuezas commented Mar 23, 2023

Do you mind sharing the project in which you'll use it? Im curious :)

@Andrei-Fogoros
Copy link

Thank you for your message and for your willingness to license your code under the MIT License.

Unfortunately, I cannot share with you the project as it is the property of a client who has used a significant portion of the code written by you in one of their files.

Thank you for your understanding and please let me know if you have any further questions. :)

Best regards,
Andrei

@dbuezas
Copy link
Author

dbuezas commented Mar 24, 2023

Oh, I see, a client project. Well happy to hear it helped somebody :)

@JosephStarobinets
Copy link

Hi @dbuezas,

Thank you very much for the code.

I made some small changes to make this code work in version 7:

var pie = d3.pie() //deleted layout.
var arc = d3.arc() //svg.
var outerArc = d3.arc() //.svg
var color = d3.scaleOrdinal()//.scale.ordinal()

Everything works fine except for one thing: when I first open the page (and the 'change' function is called for the first time), the graph doesn't display, and all the text collapses to the center. Do you have any thoughts on why this might be happening?
image

@dbuezas
Copy link
Author

dbuezas commented Oct 26, 2023

It looks like the arc radius starts as zero. I suggest you make your changes one by one and find which one is problematic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment