Gruff: Display label vertical

Hi,
I have stacked Bar graph which has about 20-30 items. I have to name
each of these items. But there names/labels are overlapping. Is there
any option to make the labels display vertical instead of horizontal?
Any other ideas how can I aviod this overlapping?

Please suggest.
Thanks

Ruby R. wrote:

Hi,
I have stacked Bar graph which has about 20-30 items. I have to name
each of these items. But there names/labels are overlapping. Is there
any option to make the labels display vertical instead of horizontal?
Any other ideas how can I aviod this overlapping?

I had the same need and added the code pasted below to my Ruby code
somewhere right after the line “require ‘gruff’”.

The second method below makes the labels vertical, and the
first method below makes the image a bit larger vertically
so the labels fit.

The “@d.rotation = -90” lines are the ones that did
the rotating. Otherwise the function is simply copied
from Gruff itself. Note that the constants in the first
method are hard coded, and of course it’d be better if they
dynamically stretched to fit the labels; but I didn’t see
a way to measure the resulting string to do this.

Also note how nice it is that Ruby lets us re-open an existing
third-part class so if we need to tweak libraries like that we
can do so without even modifying the original library.

==============================================================================
class Gruff::Base
def setup_graph_measurements
# TODO Separate horizontal lines from line number labels so they
can be shown or hidden independently
# TODO Get width of longest left-hand vertical text label and
space left margin accordingly
unless @hide_line_markers
@graph_left = 130.0 # TODO Calculate based on string width of
labels
@graph_right_margin = 80.0 # TODO see previous line
@graph_bottom_margin = 400.0
else
@graph_left = @graph_right_margin = @graph_bottom_margin = 40
end

   @graph_right = @raw_columns - @graph_right_margin

   @graph_width = @raw_columns - @graph_left - @graph_right_margin

   @graph_top = 150.0
   @graph_bottom = @raw_rows - @graph_bottom_margin
   @graph_height = @graph_bottom - @graph_top
 end


 def draw_label(x_offset, index)
   return if @hide_line_markers

   if !@labels[index].nil? && @labels_seen[index].nil?
       #@d.fill = @marker_color
     @d.font = @font if @font
     @d.stroke = 'transparent'
    @d.rotation = 90
    @d.text_align( LeftAlign)
     @d.font_weight = NormalWeight
     @d.pointsize = scale_fontsize(@marker_font_size)
     @d.gravity = NorthWestGravity #CenterGravity
     @d = @d.annotate_scaled(@base_image,
                             1, 1000,
                             x_offset, @raw_rows - 

(@graph_bottom_margin - 30),
@labels[index], @scale)
@d.rotation = -90
@labels_seen[index] = 1
end
end
end

It worked like charm! Thank you so much!

Hi Ron,
First of all thank you for the pointer. I changed the base.rb (I assume
even you changed the base.rb only …right?) with the following code (as
you mentioned) and tried refreshing the graphs but the magic didn’t
happen. I assume I don’t have to do additional steps for the graphs to
take the updated base.rb. Am I missing anything?
Thanks

def setup_graph_measurements
# TODO Separate horizontal lines from line number labels so they
can be shown or hidden independently
# TODO Get width of longest left-hand vertical text label and
space left margin accordingly
unless @hide_line_markers
@graph_left = 130.0 # TODO Calculate based on string width of
labels
@graph_right_margin = 80.0 # TODO see previous line
#@graph_bottom_margin = 80.0 #commented 7/11/06
@graph_bottom_margin = 400.0 #added 7/11/06
else
@graph_left = @graph_right_margin = @graph_bottom_margin = 40
end

  @graph_right = @raw_columns - @graph_right_margin

  @graph_width = @raw_columns - @graph_left - @graph_right_margin

  @graph_top = 150.0
  @graph_bottom = @raw_rows - @graph_bottom_margin
  #setup_graph_height() #commented 7/11/06
   @graph_height = @graph_bottom - @graph_top # added 7/11/06
end

Draws column labels below graph, centered over x_offset

def draw_label(x_offset, index)
  return if @hide_line_markers

  if !@labels[index].nil? && @labels_seen[index].nil?
    #@d.fill = @marker_color #commented 7/11/06
    @d.font = @font if @font
    @d.stroke = 'transparent'
    @d.rotation = 90 #added 7/11/06
    @d.text_align( LeftAlign)
    @d.font_weight = NormalWeight
    @d.pointsize = scale_fontsize(@marker_font_size)
    @d.gravity = NorthWestGravity #CenterGravity below changed 1 to 

1000
@d = @d.annotate_scaled(@base_image,
1, 1000,
x_offset, @raw_rows -
(@graph_bottom_margin - 30),
@labels[index], @scale)
@d.rotation = -90 #added 7/11/06
@labels_seen[index] = 1
end
end

Ruby R. wrote:

Hi Ron,
First of all thank you for the pointer. I changed the base.rb (I assume
even you changed the base.rb only …right?)…
… It worked like charm! Thank you so much!

Actually I didn’t change the file base.rb at all.

I kept the original library as it was; and in a separate
file “gruff_fixes.rb” I redefined the functions I wanted
to behave differently. That way other packages that
expected Gruff to work as it did previously worked as expected.

Only my programs that included the separate ‘fixes’ .rb
get the modified vertical behavior.

IMHO that’s one of the coolest aspects of Ruby - that even
if a program needs to make a library work differently, it
can easily do so by redefining whatever parts of it it needs to.

Ron M wrote:

Ruby R. wrote:

Hi Ron,
First of all thank you for the pointer. I changed the base.rb (I assume
even you changed the base.rb only …right?)…
… It worked like charm! Thank you so much!

Actually I didn’t change the file base.rb at all.

I kept the original library as it was; and in a separate
file “gruff_fixes.rb” I redefined the functions I wanted
to behave differently. That way other packages that
expected Gruff to work as it did previously worked as expected.

Only my programs that included the separate ‘fixes’ .rb
get the modified vertical behavior.

IMHO that’s one of the coolest aspects of Ruby - that even
if a program needs to make a library work differently, it
can easily do so by redefining whatever parts of it it needs to.

Hi Ron,
I was trying to do the same, but the added code does not seem to have
any effect on my rails project (e.g. on my view’s generated graphs)… I
saved the code in a file under the ‘lib’ directory:
lib/gruff_fix_patch.rb

Then, tried to call ‘require’ it in environment.rb (below my other
requires):
require ‘gruff’

require ‘gruff_fix_patch’

But no luck.

It would be great if you could answer this: Exactly where am I supposed
to add/put the code? How do I tell other controllers/views to include
that change?

Thanks in advance…!

Do you know how to modify the scale on Y-axis instead of numbers guessed
for you? I wanted it in increment of 100 but could not figure out where
it is doing that calculation?
Thanks