Circle path grouping but also has hover over information Nobel prize data shows which catagories over the years have won script type text javascript src https d3js org d3 v7 min js script style circle fill indianred opacity 0 3 stroke white text font family Helvetica Neue Helvetica sans serif fill black font size 13px text anchor middle font weight 900 style svg viewBox 10 10 400 400 g g svg script collection of data sets ref https github com jdorfman awesome json datasets async let pro await fetch https api nobelprize org v1 prize json let json await pro json console log json prizes 0 number of unique categories for let key in json prizes 0 console log key put the data into a format for visualizing the categories let data name laureates children for let i 0 i json prizes length i if data children filter e e name json prizes i category length 0 data children push name json prizes i category laureates children data children filter e e name json prizes i category 0 children push name laureates JSON stringify json prizes i laureates value 1 console log data var colorScale d3 scaleLinear domain 1 3 5 6 range 2c7bb6 ffffbf d7191c interpolate d3 interpolateHcl var packLayout d3 pack size 300 300 var rootNode d3 hierarchy data rootNode sum function d return d value packLayout rootNode var nodes d3 select svg g selectAll g data rootNode descendants join g attr transform function d return translate d x d y nodes append circle attr r d return d r attr fill d i return colorScale i nodes filter function d return d depth 1 append text attr class test style class test attr dx 0 attr opacity 1 text function d i return d data name nodes filter function d return d depth 2 append svg title text function d i return d data laureates Helper function Move region svg to front should be defined before called d3 selection prototype moveToFront function return this each function this parentNode appendChild this d3 selection prototype moveToFront2 function return this each function this parentNode appendChild this var parent this parentNode parent parentNode appendChild parent var currNode d3 select this Fix to move title hover over text to the very front d3 selection prototype moveToFront3 function return this each function d3 select svg node appendChild this nodes selectAll test moveToFront append text attr dx 10 text function d return ddd d3 selectAll text filter function d return d depth 1 moveToFront2 d3 selectAll title filter function d return d depth 2 moveToFront3 var region2 nodes filter function d return d depth 2 append title text dd add text node label and move to front var region nodes filter function d return d depth 1 moveToFront append text attr dx 0 text function d return d data name add title node label and move to front nodes filter function d return d depth 2 moveToFront2 append title text function d return d data laureates script Donut with Arc Labels script type text javascript src https d3js org d3 v7 min js script Nice fancy text for the names link href https fonts googleapis com css family Pacifico rel stylesheet type text css style body font family Pacifico sans serif fill 333333 font size 16px text align center style svg viewBox 40 40 500 400 g g svg script async let pro await fetch https api nobelprize org v1 prize json let json await pro json get the catagory data from the prize json and put it into an array var donutData for let i 0 i json prizes length i if donutData filter e e name json prizes i category length 0 donutData push name json prizes i category value 0 donutData filter e e name json prizes i category 0 value 1 setup var screenWidth window innerWidth var margin left 20 top 20 right 20 bottom 20 var width Math min screenWidth 500 margin left margin right var height Math min screenWidth 500 margin top margin bottom var svg d3 select svg attr width width margin left margin right attr height height margin top margin bottom append g attr class wrapper attr transform translate width 2 margin left height 2 margin top data and scaling note D3 v4 it is no longer named d3 scale linear Use d3 scaleLinear instead Create a color scale var colorScale d3 scaleLinear domain 1 3 5 6 range 2c7bb6 ffffbf d7191c interpolate d3 interpolateHcl Create an arc function var arc d3 arc innerRadius width 0 75 2 outerRadius width 0 75 2 30 Turn the pie chart 90 degrees counter clockwise so it starts at the left var pie d3 pie startAngle 90 Math PI 180 endAngle 90 Math PI 180 2 Math PI value function d return d value padAngle 01 sort null Create the donut slices and also the invisible arcs for the text svg selectAll donutArcs data pie donutData enter append path attr class donutArcs attr d arc style fill function d i if i 7 return CCCCCC Other else return colorScale i 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 comma s 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 first capital M and first capital A middleLoc A 0 0 1 Everything between the first capital A and 0 0 1 endLoc 0 0 1 Everything between the first 0 0 1 and the end of the string denoted by Flip the direction of the arc by switching the start en end point and sweep flag of those elements that are below the horizontal line 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 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 console log finished script
svg viewBox 40 40 500 400 g g svg script async let pro await fetch https api nobelprize org v1 prize json let json await pro json get the catagory data from the prize json and put it into an array var donutData for let i 0 i json prizes length i if donutData filter e e name json prizes i category length 0 donutData push name json prizes i category value 0 donutData filter e e name json prizes i category 0 value 1 setup var screenWidth window innerWidth var margin left 20 top 20 right 20 bottom 20 var width Math min screenWidth 500 margin left margin right var height Math min screenWidth 500 margin top margin bottom var svg d3 select svg attr width width margin left margin right attr height height margin top margin bottom append g attr class wrapper attr transform translate width 2 margin left height 2 margin top data and scaling note D3 v4 it is no longer named d3 scale linear Use d3 scaleLinear instead Create a color scale var colorScale d3 scaleLinear domain 1 3 5 6 range 2c7bb6 ffffbf d7191c interpolate d3 interpolateHcl Create an arc function var arc d3 arc innerRadius width 0 75 2 outerRadius width 0 75 2 30 Turn the pie chart 90 degrees counter clockwise so it starts at the left var pie d3 pie startAngle 90 Math PI 180 endAngle 90 Math PI 180 2 Math PI value function d return d value padAngle 01 sort null Create the donut slices and also the invisible arcs for the text svg selectAll donutArcs data pie donutData enter append path attr class donutArcs attr d arc style fill function d i if i 7 return CCCCCC Other else return colorScale i 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 comma s 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 first capital M and first capital A middleLoc A 0 0 1 Everything between the first capital A and 0 0 1 endLoc 0 0 1 Everything between the first 0 0 1 and the end of the string denoted by Flip the direction of the arc by switching the start en end point and sweep flag of those elements that are below the horizontal line 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 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 console log finished script