Sunday, June 7, 2009

Gruff vertical labels

As I was working on the project which has pdf reporting functionality, which also contains charts and other related data.
So need to convert those charts to printer friendly images, which was possible through gruff graphs for ruby.
But X-axis labels were so long that those can not stay on one line.
So I tried to make it vertical with help of post by mrtibs vertical labels in gruff which really helped.
But problem with that was labels should go at the bottom of the chart and not at the top.
So instead of
y_offset = @graph_bottom + LABEL_MARGIN

we used
y_offset = ((@graph_height / 2) + (@graph_top * 3) - 5 )

which worked for me a bit but not fully functional.


  1. Hi,
    Any solution on this for the line graphs? I was trying different approaches, but none works. My detailed post is here:

  2. Hi Arif,

    The solution above mentioned was working for me say 75%. Even still I am looking for better solution. No luck so far. This approach was followed by my colleague while working on the same project, but it failed. Did you tried to modify the gem as explained above?

    You will need to go in the gruff gem folder:base.rb line no.:856 where you will find

    def draw_label(x_offset, index)
    return if @hide_line_markers

    if !@labels[index].nil? && @labels_seen[index].nil?
    y_offset = ((@graph_height / 2) + (@graph_top * 3) - 5 )

    @d.fill = @font_color
    @d.font = @font if @font
    @d.font_weight = NormalWeight
    @d.pointsize = scale_fontsize(@marker_font_size)
    @d.gravity = NorthGravity
    @d.rotation = -90
    @d = @d.annotate_scaled(@base_image,
    1.0, 1.0,
    x_offset, y_offset,
    @labels[index], @scale)
    @d.rotation = 90
    @labels_seen[index] = 1
    debug { @d.line 0.0, y_offset, @raw_columns, y_offset }

    I hope this will help you somewhat.