Set background of edge labels

Is there a way to add a background to edge labels? I tried fillcolor but it only colors the arrow, and the edge labels kept having a transparent background.

You can use an “HTML” label (Node Shapes | Graphviz)

graph C {

   y--z [label=<<table bgcolor="orange" border="0" cellspacing="0"><tr><td>table text</td></tr></table>>]
  
}

Giving:

Is there a bug with that? A long edge label overflows the background

digraph g1 {
    a->b [label=<<table bgcolor="orange" border="0" cellspacing="0"><tr><td>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td></tr></table>>];
}

[dot]
digraph g1 {
a->b [label=<<table bgcolor=“orange” border=“0” cellspacing=“0”><tr><td>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td></tr></table>>];
}
[/dot]

Result on Graphviz Visual Editor

Can reproduce with v15.0.0

I’ll see what workaround I can find. I don’t know why the forum builtin render is fine

Looks fine on my Linux system.

File a bug report here: Work items · graphviz / graphviz · GitLab

I’m stuck filing a bug report. The Create Issue button loads forever:

I tried signing up to GitLab but it seems it doesn’t support my region anymore[1]. It said will redirect me to JiHu but the landing page is 404. What the heck

Could someone file the bug report for me?


  1. I remember seeing this in news 2 years ago ↩︎

Sorry about the problem. This has been discussed and explained enough times already. The issue is, “Graphviz generates host-dependent SVG.” It can manifest itself in various ways, e.g. “text label is clipped” “text label overflows into another object” “text label background is incorrect” but they are all the same thing.

Ways we may approach this:

  1. Don’t change anything, just continue to explain/ignore.
  2. Deprecate/remove the native SVG driver in favor of cairopango, which does its own font embedding to avoid this issue.
  3. Program a font embedding feature like that of the cairopango driver into the native SVG driver, unfortunately giving up some of its simplicity and “efficiency.”
  4. Figure out and program a way to display a warning when host-dependent SVG is moved to an incompatible host. Not sure this can be done without Javascript or other executable semantics that may be incompatible with the way the native SVG output is probably being used, so this may not even do any good.
  1. the command dot -Tsvg:cairo ... (as documented in the font FAQ and mentioned by Stephen) will produce output you desire
  2. the reference to fc-list in the font FAQ does not apply to Windows (only Linux & maybe Macos). I will create a documentation Issue (bug report).
  3. the Windows command “settings > personalization > fonts” seems to list installed fonts.
  4. the existing Issue Unwanted space around html font tags with svg output (#1891) · Issues · graphviz / graphviz · GitLab now also links to this page
  5. another possible improvement / approach (in addition to Stephen’s list) is to add SVG’s (relatively new) @face-font feature (see Unwanted space around html font tags with svg output (#1891) · Issues · graphviz / graphviz · GitLab)
  6. font problems are a pain in the butt

[bullet #1 has been edited to be -Tsvg:cairo ... ]

If there’s an issue to be filed, I can happily do this. But I don’t think it would gain us much as you presumably could not get notifications about updates to it there. You’ve got the attention of the main Graphviz developers on the forum here already.

You’re assuming hsambirchaaaaaaaaa generated the graph on one machine and is viewing it on another? I guess that could be the cause, but I don’t see anything in their previous replies that suggests they’re doing this.

You mean dot -Tsvg:cairo …?

You’re right, I have no basis to say the SVG was generated on one machine and viewed in another.

It’s better to say, “Graphviz generates host / renderer dependent SVG.” For example on a given machine, Graphviz might use system fonts while other programs (like Chrome, Microsoft Office, Inkscape, and Adobe Illustrator) might each have their own additional fonts.

Thanks, I have edited my typo to be -Tsvg:cairo ...

As @mattr suggested, -Tsvg (not using cairo) on my Windows system, also produces the bug.

I feel indifferent about it… Since it’s an instance of a known bug pattern, just let me know when it’s going to be fixed so that I can verify. Or ask me here for further information.

Sorry for being absent for a whole month!

I’m viewing on the same machine I generated it on. (Edit: responded)

Sorry, I meant to amended my previous comment about this. Not “host-dependent”, but “renderer-dependent” SVG, where “renderer” includes the internal renderer in graphviz that is executed to get text label sizes.

I don’t see any good solution to this with our native SVG generator.

So does it still belong to a known pattern?

Umm, I believe I can reproduce after fixing a font. I’m not at my computer though, will update a sample asap

We believe this problem exists, no need to spend effort on reproducing it in new ways.

As far as I can tell, there is no way to fix this hole without buying into a lot of complexity that defeats the point of having the native SVG generator. (The complexity would be either coming up with a full font embedding scheme like cairopango has, but why even bother, that already exists? Or assume that the SVG can contain, er, Javascript, that can have functions to get the sizes of things at runtime. Again this seems to defeat the concept of having a “simple,” parsable XML format for graphics. Would require some major hackery in the SVG driver, too.

Because -Tsvg:cairo exists, there’s an available work-around. Does the -Tsvg:cairo option do what you need?

I remember it ignores the fontname (will post a sample when I can, too)

digraph g1 {
    graph [bgcolor=white]
    node [fontname=Consolas]
    edge [fontname="Arial"]
    aAma->b [label=<<table bgcolor="orange" border="0" cellspacing="0"><tr><td>aaaAaaamaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</td></tr></table>>]
    c->d [label=<<table bgcolor="orange" border="0" cellspacing="0"><tr><td>aaaAaaaaamaaaaaaaaaaaaaaaaaaaaaaaa</td></tr></table>>]
    e->f [label=<<table bgcolor="orange" border="0" cellspacing="0"><tr><td>aaaAaaaaamaaaaaaaaaaaaaaaaaaaaaaaa</td></tr></table>>, fontname=Consolas]
}

dot a.gv -Tsvg >a.svg (note: I’ll also upload a PNG later since bug isn’t visible on mobile browser)

dot a.gv -Tsvg:cairo >a-cairo.svg

So my previous claim is wrong, it doesn’t ignore the fontname, but Cairo output is noticably uglier than non-Cairo. Cairo can technically work for me but I’ll still fall back to padding spaces at the end of the label without Cairo

Yes I can reproduce after fixing a font as shown in the non-Cairo generation here

Just speculating, but wonder if would help for the graphviz SVG generator to explicitly find and emit (rebind) font-family using the pango font description. It would need to happen around here in svg_textspan. Also I’m not sure about the case analysis that happens before this (PSFONTS, SVGFONTS, or NATIVEFONTS) and how that should affect the lookup. Something to think about, though.

Here’s the PNG for the non-Cairo generation:

Rendered on Edge:

Rendered on Firefox:

(my OS Windows 11 of course)