Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3 Appending Text to a SVG Rectangle

I'm looking to append html onto a rectangle in D3 to give me a multiple line tooltip. The bottom part is how I'm adding a rectangle which may be part of the problem. The top is the code that should work in my world.

 newRect.().html(" <textArea font-family=Verdana font-size=20 fill=blue > Test " + "</br>" + "Test2 </textArea>"); 

Which does insert a text field into the SVG, it just doesn't display:
HTML:

<rect id="rectLabel" x="490" y="674" width="130" height="160" fill="red">     <textarea fill="blue" font-size="20" font-family="Verdana"> Test </br>Test2 </textarea> </rect> 

I have a mouse over function which runs the following:

    newRect = svg.append("rect")     .attr("x", xCor)     .attr("y", yCor)     .attr("width", 130)     .attr("height", 160)     .attr("fill", "red")     .attr("id", "rectLabel"); 

I think I should be doing this but it doesn't work. It just removes the g.node that I'm trying to append to.

    newRect = $(this).enter().append("rect")     .attr("x", xCor)     .attr("y", yCor)     .attr("width", 130)     .attr("height", 160)     .attr("fill", "red")     .attr("id", "rectLabel"); 

Question: Why doesn't my text appear? Ive tried .html, .textArea. I want a multiple line label so I don't think .text will work correct? Also, how should I be appending the rectangle?

like image 787
gbam Avatar asked Dec 17 '13 20:12

gbam


People also ask

How do I add text to SVG rectangle?

If you want to display text inside a rect element you should put them both in a group with the text element coming after the rect element ( so it appears on top ). The group fits to its content not the other way. I think the elements are still relative to the parent svg.

How do I add text to SVG?

SVG treats a text element in much the same way that it treats graphical and shape elements. You position text with x and y coordinates and use fill and stroke options to color text the same way you would with a <circle> or <rect> element.

Can you append SVG on SVG?

The answer is yes. For whatever reason you want to nest those SVGs (you probably don't need it), just use the append method as you would with any other element.

What is selectAll in d3?

selectAll() function in D3. js is used to select all the element that matches the specified selector string. Syntax: d3.selectAll("element") Parameters: This function accepts single parameter HTML tag as a parameter. Return Value: This function returns the selected elements.


2 Answers

A rect can't contain a text element. Instead transform a g element with the location of text and rectangle, then append both the rectangle and the text to it:

var bar = chart.selectAll("g")     .data(data)   .enter().append("g")     .attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });  bar.append("rect")     .attr("width", x)     .attr("height", barHeight - 1);  bar.append("text")     .attr("x", function(d) { return x(d) - 3; })     .attr("y", barHeight / 2)     .attr("dy", ".35em")     .text(function(d) { return d; }); 

http://bl.ocks.org/mbostock/7341714

Multi-line labels are also a little tricky, you might want to check out this wrap function.

like image 121
Adam Pearce Avatar answered Oct 05 '22 00:10

Adam Pearce


Have you tried the SVG text element?

.append("text").text(function(d, i) { return d[whichevernode];}) 

rect element doesn't permit text element inside of it. It only allows descriptive elements (<desc>, <metadata>, <title>) and animation elements (<animate>, <animatecolor>, <animatemotion>, <animatetransform>, <mpath>, <set>)

Append the text element as a sibling and work on positioning.

UPDATE

Using g grouping, how about something like this? fiddle

You can certainly move the logic to a CSS class you can append to, remove from the group (this.parentNode)

like image 38
cbayram Avatar answered Oct 05 '22 00:10

cbayram