score:3
Accepted answer
You can get the following result
with this code
var radiusSeparation = 5;
var texts = vis.selectAll("text")
.data(nodes)
.enter().append("text")
.attr("transform", function(d) {
if (d.depth == 0) return null;
d.deg = 180 / Math.PI * (d.x0 + d.x1) * 0.5;
var translate = d.depth == 1 ? Math.sqrt(d.y1)-radiusSeparation : Math.sqrt(d.y0)+radiusSeparation;
var trans = `rotate(${(d.deg-90).toFixed(2)}) translate(${translate.toFixed(2)},0)`;
if (d.deg > 180) {
trans += ` rotate(180)`;
}
return trans;
})
.text( d => `${d.data.name} (${d.value})` )
.attr("text-anchor", function(d) {
if (d.depth == 0) return "middle";
if (d.depth == 1) return d.deg < 180 ? "end" : "start";
return d.deg < 180 ? "start" : "end";
})
.attr("dominant-baseline", "middle")
use the radius of your arcs to position the text. Use a small separation distance so the text does not touch the arcs
store the
deg
value in the datum so you can use it for the text anchorswitch text-anchor based on deg value
treat depth=0 as special in all cases
vertical align the text to the middle with
dominant-baseline
d3.hierarchy.sum() stores the result in
d.value
, so use this in the text
Source: stackoverflow.com
Related Query
- d3.partition sunbursts: rotating text and other glitches
- D3 rotating globe and text
- Rotating the text in the pie chart of d3.js and alternate filling of colors
- Axis and svg text elements hide each other
- How to get d3 treemap cell text to wrap and not overflow other cells
- D3.js text not coming below each other and color selection
- State of SVG Performance on iOS and other Tablets?
- Add text label to d3 node in Force directed Graph and resize on hover
- D3, SVG and textPath: How to move the text down?
- Group each rect and text in D3
- Where can I get the .geojson file for India and not separate files for each state/territory or any other distinction?
- Rotating text paths in d3.js chord diagram without usual svg:text
- D3 bar chart needs to add arrow and text in particular bar
- With D3, how can I set attribute ("fill", "none") of axis path and line, but not text (without editing stylesheet)
- Transitions and text attributes in a div in D3.js
- Wrap text in a circle with D3 v4 and D3plus
- splitting text for some bold and some not on functional javascript piece
- Trying to link object and text when dragging in v4
- How to add some padding between a path and text on it?
- What is the proper way to both rotate and translate text in d3?
- Trying to find a way to get the row and column names of csv in html/js by using d3 , any other ways?
- Vertical alignment between an SVG rect and SVG text using D3 and javascript
- Add text in rect svg and append it to arc in donut chart
- D3 V4 zoom via mouse and programmatic are zooming independent from each other
- Display text and direct via hyperlink on d3 Map
- Radio buttons in D3: how to align text correctly and select a default?
- Text and Rectangle not align
- D3.js: yAxis text bold and blur
- D3: Rotating labels in the third and fourth quad in sunburst example
- Javascript find a word in text and change color dynamically
More Query from same tag
- d3js : Stacked Horizontal Bar : How to split categories into bars
- dc.js not displaying the y-axis
- Bar colors reverting back to original colors
- d3 transition of realtime graph unsmooth in ie
- D3 Directed graphs
- D3 x scale positioning
- How do I change color of path element when its value is selected from dropdown
- d3 Events not triggering
- right JSON format structure for Streamgraph example D3
- Using D3 with websharper
- D3 animation (transition) does NOT work on Angular app
- Getting access to individual area elements with d3
- D3 Mouse Over Groups Issue
- adjust text in the label of my graphic
- D3.js is it possible to draw circles on each point using nested data?
- Convert raw json to d3 compatible dataset
- D3.JS how to count number of circles of a certain attribute on mouseover
- D3js unable to show gradient area chart
- Trying to display bars on a map with D3, getting Error: <rect> attribute height: Unexpected end of attribute. Expected length, ""
- Scattered plot zooming not working correctly in d3.js
- How to display total count on top of the stacked bar chart
- How to make geoAlbersUSA projection straight(not curved) like geoMercator?
- Force directed graph drawn out of the allocated SVG size
- Click event not working after interation
- find target's target i.e friend of friend in force directed graph
- How to update multiple elements for each data
- Accessing different parts of json in a d3 chart function
- AngularJS ng-select and ng-switch-when issue
- D3 arcs on (inverted) stereographic projection not displaying as required
- d3.js mouse over show details about node by using two different json file