score:8
I had a similar problem so I had to do a little manual work. First, you need a function that will actually 'wrap' the text:
function wordwrap(text, max) {
var regex = new RegExp(".{0,"+max+"}(?:\\s|$)","g");
var lines = [];
var line;
while ((line = regex.exec(text))!="") {lines.push(line);}
return lines
}
Then, you need to appy this function to each text element. In your code I would write it like this:
nodeEnter.append("text")
.attr("x", function(d) { return d.children1 || d._children1 ? -10 : 10; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children1 || d._children1 ? "end" : "start"; })
.style("fill-opacity", 1e-6)
.each(function (d) {
if (d.NickName!=undefined) {
var lines = wordwrap(d.NickName, 15)
for (var i = 0; i < lines.length; i++) {
d3.select(this).append("tspan")
.attr("dy",13)
.attr("x",function(d) {
return d.children1 || d._children1 ? -10 : 10; })
.text(lines[i])
}
}
});
The end result is this:
Of course you should spend some time adjusting the x
position of each text element.
EDIT A simpler way would be to have a wordwrap method as:
function wordwrap2(text) {
var lines=text.split(" ")
return lines
}
and apply it like the following:
nodeEnter.append("text")
.attr("x", function(d) { return d.children1 || d._children1 ? -10 : 10; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children1 || d._children1 ? "end" : "start"; })
.style("fill-opacity", 1e-6)
.each(function (d) {
if (d.NickName!=undefined) {
var lines = wordwrap2(d.NickName)
for (var i = 0; i < lines.length; i++) {
d3.select(this).append("tspan")
.attr("dy",13)
.attr("x",function(d) {
return d.children1 || d._children1 ? -10 : 10; })
.text(lines[i])
}
}
});
Here is a fiddle for this last approach: http://jsfiddle.net/mEyQW/59/
Hope this helps.
score:1
I had a hard time to split a line on comma and place the text pieces in a list format. The following adaptation worked.
function splitoncomma(text) {
var lines=text.split(",") //splits text on comma
return lines
}
label.append("div")
.attr("class", "class_name")
.each(function (d) {
if (textValue(d)!==undefined) {
var lines = splitoncomma(textValue(d))
for (var i = 0; i < lines.length; i++) {
d3.select(this)
.append("text")
.append("tspan")
.style("float","left")// the float:left avoids default inline positioning
.text(lines[i])
}
}
The style float left is required to avoid default inline placement.
Source: stackoverflow.com
Related Query
- D3.js - How can I add a new line to the text in this Collapsible Tree?
- How can I add zooming to the d3 collapsible tree example?
- How can add text to the center of a D3 sunburst diagram
- How to add text only on one side of the tree links
- How can i add another line to make this a multi line chart?
- How can I add text to SVG elements in D3 using the example below
- How can I add title text to this chart in d3?
- How can i change the color per section in this line chart?
- Add hyperlink to node text on a collapsible tree
- New to nvD3 - how can I make my unix timestamps appear as dates on the x-axis?
- I want to add text on the link in D3.js tree graph
- How can I set the each line color or width in SVG path
- How can I remove a line from the 110m TopoJson world map?
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- How can I center the text in a node?
- d3.js How to make all the nodes collapsed in Collapsible indented Tree
- How to add text at the end of Arc
- How to add text in the center of node in force directed graph?
- How can I add labels inside the points in a scatterplot?
- How can I change style class & content of text for ancestors in this d3.js tree?
- How can I color one Y-axis line different from the other grid-lines?
- How can I add images in the nodes of a D3 Sankey diagram, using the rcv and networkD3 packages in R?
- How can we add the space between bars for multiple groups of bars in c3js?
- D3: How can I find the largest length of my text labels?
- How can I add different images instead of text inside d3.js piechart slices
- How can I use D3 tree data in the VEGA Api?
- Unable to add text on link in d3 collapsible tree
- How can I draw a continuous line using d3 without needing to click&drag the mouse, but rather just use the mouseover event?
- How can I make the animation of a circle continous with hovering like this gif in d3.js?
- How to add the fill in between two line in line graph?
More Query from same tag
- n.call is not a function issue when doing a transition
- d3.js Multi-series line chart interactive (tsv into literal data)
- d3js time format
- D3: Resize bar chart according to window width
- What's the easiest way to attach a d3 element to the mouse?
- Changing the data used by a d3 force layout
- D3 Horizontal Bar Chart exact copy
- D3js pie chart click event not work
- Adding a y axis horizonal line to a D3 scatter chart
- d3.js bar chart - show/hide bars on legend click. how to filter correctly?
- Is it possible to skip nodes in D3.js tree?
- Changing Ticks values to text using D3
- Need D3 Map file uStates.js
- D3 AngularJs Force Directed Graph
- How to apply "background: repeating-linear-gradient" style for rect using d3.js
- D3js code when called twice duplicates the graph instead of refreshing
- Need to shift link of child node to parent once parent collapsed in d3 graph
- d3 filling an area with a gradient
- Adding elements to hierarchy for circle packing colours
- Is there a D3 Gauge allowing multiple series/columns?
- D3js filter table
- Including Javascript Library to Wicket Panel
- D3 not displayed within jupyter notebook
- d3js alert leaf name
- Uncheck the Checkbox makes no difference
- How to redraw data after d3.js zoom
- D3 Interactive Legend on Multi Line Chart Issue
- Bar chart: how could I align the data with countries when sorting in D3 v5?
- d3.js graphics not showing in Jupyter notebook output HTML
- D3 V4 Properly placing a bubble in the US Map