[rrd-users] legend: getting everything aligned and perky

Daniele Sluijters d.sluijters at betterbe.com
Wed Jul 20 10:13:05 CEST 2011


I figured out a few things:

1) RRDtool just can't/won't align stuff decently like you want it because it doesn't have the options to do so. I'm really missing something like a table-keyword similar to HTML's in order to create tabular legends.

2) \t, \l, \n, \j are useful but it's a thing about getting stuff right and getting stuff wrong. Basically, shuffle through the options until you get the result you need.

3) The only way to really align stuff is with spaces. If a string is always of an equal length, thanks to for example additionally appended spaces at the end of a label, stuff will line out.

In the end I'm left with the following code:
args_ext.append("COMMENT:                <b>Average</b>")
args_ext.append("COMMENT:       <b>Max</b>\\n")
args_ext.append("LINE1:sc%i%s:%s" % (2*i+1000, COLORS_MULTI[2*i], pair.descr.split("/")[0].lower().ljust(8)+"|"))
args_ext.append("GPRINT:sc%i:AVERAGE:%%8.2lf %%S  |" % (2*i+1000,))
args_ext.append("GPRINT:sc%i:MAX:%%8.2lf %%S  |\\l" % (2*i+1000,))
args_ext.append("LINE2:sc%i%s:%s" % (2*i+1+1000, COLORS_MULTI[2*i+1], pair.descr.split("/")[1].lower().strip().ljust(8)+"|"))
args_ext.append("GPRINT:sc%i:AVERAGE:%%8.2lf %%S  |" % (2*i+1+1000,))
args_ext.append("GPRINT:sc%i:MAX:%%8.2lf %%S  |\\l" % (2*i+1+1000,))

Now because I'm using python's string formatting I need to escape the % by doubling it so it isn't seen by python as something meaningful, same goes for the backslashes (\). I'm also generating the sc-numbers on the fly, don't bother with it, I know what's happening and that doesn't affect anything. 

The crux lies in ljustify(8). It basically makes sure the label is 8 chars long with spaces appended to the right of it. Because we also tell RRDtool that the font for the legend is a monospace-font everything now is exactly the same size so we can start using spaces to align. Coupled with the %8.2lf I also always know that the output will be 8 numbers followed by the decimal separator followed by another two numbers. If the first part doesn't happen to have 8 numbers spaces will be prepended to it so  xxxx.xx and yyy.yy align equally to the right. The additional pipe at the end is just for clarity in our case, it creates a column-feel.

By bolding the first two comments and spacing them correctly you can then create the look and feel of table headings but that only works for RRDtool 1.3+ which can use pango.

This in effect results in the following code being passed to RRDtool:
COMMENT:                <b>Average</b>
COMMENT:       <b>Max</b>\n
LINE1:sc1000#014194:used    |
GPRINT:sc1000:AVERAGE:%8.2lf %S  |
GPRINT:sc1000:MAX:%8.2lf %S  |\l
LINE2:sc1001#f49800:total   |
GPRINT:sc1001:AVERAGE:%8.2lf %S  |
GPRINT:sc1001:MAX:%8.2lf %S  |\l
LINE1:sc1002#C93A5E:free    |
GPRINT:sc1002:AVERAGE:%8.2lf %S  |
GPRINT:sc1002:MAX:%8.2lf %S  |\l
LINE2:sc1003#2C8C29:max     |
GPRINT:sc1003:AVERAGE:%8.2lf %S  |
GPRINT:sc1003:MAX:%8.2lf %S  |\l

As you can see, the amount of spaces between 'max' and the pipe at the end is just a bit more than for example 'free' until the pipe.

In the end I get a graph looking like this:

That's reasonably decent and good enough for now. I really wish there was a less intricate way of doing this. Now I still need to find a color-scheme / bunch of settings that give the output a bit more of a post-millenium look and feel.

Daniele Sluijters
e: d.sluijters at betterbe.com
w: http://www.betterbe.com

On Jul 20, 2011, at 08:57 , Simon Hobson wrote:

> Ulf Zimmermann wrote:
>> You can use \t in the GPRINT to align.
> It's been a while now, but from memory it didn't work for me because 
> ... it tabs from the end of the previous element on the line, not to 
> an absolute position. I think I was trying to align some values where 
> one line had the colour blob from the legend, and another didn't. 
> Since the tab runs from the end of the space occupied by the colour 
> blob, there is no way to align the other line with it at all. 
> Similarly where some lines don't include all the values that others 
> do - the tab runs from the end of the previous element, and so you 
> still end up trying to match the length of a printed field with 
> spaces.
> It also spread stuff out terribly so that my tables just didn't fit 
> into the box.
> Now if someone has a working example that can do something like this :
> * Some label       xxx.xx    xxx.xx    xxx.xx
>   Another label    yyy.yy              yyy.yy
> and have it all line up then I'd love to see it !
> -- 
> Simon Hobson
> Visit http://www.magpiesnestpublishing.co.uk/ for books by acclaimed
> author Gladys Hobson. Novels - poetry - short stories - ideal as
> Christmas stocking fillers. Some available as e-books.
> _______________________________________________
> rrd-users mailing list
> rrd-users at lists.oetiker.ch
> https://lists.oetiker.ch/cgi-bin/listinfo/rrd-users

More information about the rrd-users mailing list