Browse files

Made it possible to use data labels for left bar labels in side bar g…

…raphs
  • Loading branch information...
1 parent 4160d66 commit 9ecdfe51699eb27029812c1d2f8d731f9a081fbe @BRIMIL01 BRIMIL01 committed with BRIMIL01 Mar 7, 2011
Showing with 17 additions and 6 deletions.
  1. +3 −0 lib/gruff/base.rb
  2. +14 −6 lib/gruff/side_bar.rb
View
3 lib/gruff/base.rb
@@ -193,6 +193,9 @@ class Base
# Default is "%.2f"
attr_accessor :label_formatting
+ # With Side Bars use the data label for the marker value to the left of the bar
+ # Default is false
+ attr_accessor :use_data_label
# If one numerical argument is given, the graph is drawn at 4/3 ratio
# according to the given width (800 results in 800x600, 400 gives 400x300,
# etc.).
View
20 lib/gruff/side_bar.rb
@@ -57,10 +57,16 @@ def draw_bars
@d = @d.rectangle(left_x, left_y, right_x, right_y)
# Calculate center based on bar_width and current row
- label_center = @graph_top + (@bars_width * point_index + @bars_width / 2)
- draw_label(label_center, point_index)
+
+ if @use_data_label
+ label_center = @graph_top + (@bar_width * (row_index+point_index) + @bar_width / 2)
+ draw_label(label_center, row_index, @norm_data[row_index][DATA_LABEL_INDEX])
+ else
+ label_center = @graph_top + (@bars_width * point_index + @bars_width / 2)
+ draw_label(label_center, point_index)
+ end
if @show_labels_for_bar_values
- val = (@label_formatting || "%.2f") % @norm_data[row_index][3][point_index]
+ val = (@label_formatting || "%.2f") % @norm_data[row_index][3][point_index]
draw_value_label(right_x+40, (@graph_top + (((row_index+point_index+1) * @bar_width) - (@bar_width / 2)))-12, val.commify, true)
end
end
@@ -80,7 +86,8 @@ def draw_line_markers
# Draw horizontal line markers and annotate with numbers
@d = @d.stroke(@marker_color)
@d = @d.stroke_width 1
- number_of_lines = 5
+ number_of_lines = @marker_count || 5
+ number_of_lines = 1 if number_of_lines == 0
# TODO Round maximum marker value to a round number like 100, 0.1, 0.5, etc.
increment = significant(@spread.to_f / number_of_lines)
@@ -111,8 +118,9 @@ def draw_line_markers
##
# Draw on the Y axis instead of the X
- def draw_label(y_offset, index)
+ def draw_label(y_offset, index, label=nil)
if !@labels[index].nil? && @labels_seen[index].nil?
+ lbl = (@use_data_label) ? label : @labels[index]
@d.fill = @font_color
@d.font = @font if @font
@d.stroke = 'transparent'
@@ -122,7 +130,7 @@ def draw_label(y_offset, index)
@d = @d.annotate_scaled(@base_image,
1, 1,
-@graph_left + LABEL_MARGIN * 2.0, y_offset,
- @labels[index], @scale)
+ lbl, @scale)
@labels_seen[index] = 1
end
end

0 comments on commit 9ecdfe5

Please sign in to comment.