Ruby R. wrote:
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.
# 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
@graph_left = 130.0 # TODO Calculate based on string width of
@graph_right_margin = 80.0 # TODO see previous line
@graph_bottom_margin = 400.0
@graph_left = @graph_right_margin = @graph_bottom_margin = 40
@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
def draw_label(x_offset, index)
return if @hide_line_markers
if [email protected][index].nil? && @labels_seen[index].nil?
#@d.fill = @marker_color
@d.font = @font if @font
@d.stroke = 'transparent'
@d.rotation = 90
@d.font_weight = NormalWeight
@d.pointsize = scale_fontsize(@marker_font_size)
@d.gravity = NorthWestGravity #CenterGravity
@d = @d.annotate_scaled(@base_image,
x_offset, @raw_rows -
(@graph_bottom_margin - 30),
@d.rotation = -90
@labels_seen[index] = 1