Skip to content

Commit

Permalink
Add support for HTML content in node labels
Browse files Browse the repository at this point in the history
This patch comes from @robopeter in #19.
  • Loading branch information
cpettitt committed Oct 15, 2012
1 parent 71a9193 commit abc0b4e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h2>Graph Definition</h2>
<textarea id="input" rows="5" cols="80" style="display: block" onKeyUp="tryDraw();">
/* Example */
digraph {
A [label="A Big Source!", fontSize=25];
A [label="<div>A Big <span style='color:red;'>HTML</span> Source!</div>", fontSize=25];
C [fontColor=red];
E [label="A blue sink!", fontName=arial, fill="#aaccff"];
A -&gt; B -&gt; C [color=green, strokeWidth=3];
Expand Down
8 changes: 4 additions & 4 deletions src/pre-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ dagre.preLayout = function(g) {
// The font size to use for the node's label
defaultInt(attrs, "fontSize", 14);

var text = createTextNode(u);
svg.appendChild(text);
var svgNode = createSVGNode(u);
svg.appendChild(svgNode);

var bbox = text.getBBox();
var bbox = svgNode.getBBox();
attrs.width = Math.max(attrs.width, bbox.width);
attrs.height = Math.max(attrs.height, bbox.height);
svg.removeChild(text);
svg.removeChild(svgNode);
});

g.edges().forEach(function(e) {
Expand Down
10 changes: 8 additions & 2 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ dagre.render = function(g, svg) {
"stroke: " + u.attrs.color].join("; "));
group.appendChild(rect);

var text = createTextNode(u);
group.appendChild(text);
var svgNode = createSVGNode(u);
if(svgNode.nodeName == "foreignObject"){
svgNode.setAttribute("x", -(u.attrs.marginX + u.attrs.width / 2 + u.attrs.strokeWidth / 2));
svgNode.setAttribute("y", -(u.attrs.marginY + u.attrs.height / 2 + u.attrs.strokeWidth / 2));
svgNode.setAttribute("width", u.attrs.width + 2 * u.attrs.marginX + u.attrs.strokeWidth);
svgNode.setAttribute("height", u.attrs.height + 2 * u.attrs.marginY + u.attrs.strokeWidth);
}
group.appendChild(svgNode);
});
}

Expand Down
26 changes: 26 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ function createSVGElement(tag) {
* Performs some of the common rendering that is used by both preLayout and
* render.
*/
function createSVGNode(node, x){
if(node.attrs.label[0] == '<'){
return createHTMLNode(node, x);
}else{
return createTextNode(node, x);
}
}

function createHTMLNode(node, x){
var fo = createSVGElement("foreignObject");
var div = document.createElementNS("http://www.w3.org/1999/xhtml", "div");
div.innerHTML = node.attrs.label;
div.setAttribute("id", "temporaryDiv");
var body = document.getElementsByTagName('body')[0];
body.appendChild(div);
var td = document.getElementById("temporaryDiv");
td.setAttribute("style", "width:10;float:left;");
fo.setAttribute("width", td.clientWidth);
fo.setAttribute("height", td.clientHeight);
body.removeChild(td);
div.setAttribute("id", "");

fo.appendChild(div);
return fo;
}

function createTextNode(node, x) {
var fontSize = node.attrs.fontSize;
var text = createSVGElement("text");
Expand Down

0 comments on commit abc0b4e

Please sign in to comment.