Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added labels to side stacked bar chart

  • Loading branch information...
commit 5688aced6a5bb77020198102953e2b7e2337d4aa 1 parent 9ecdfe5
@BRIMIL01 BRIMIL01 authored BRIMIL01 committed
Showing with 34 additions and 3 deletions.
  1. +17 −3 lib/gruff/side_stacked_bar.rb
  2. +17 −0 test/test_sidestacked_bar.rb
View
20 lib/gruff/side_stacked_bar.rb
@@ -33,7 +33,10 @@ def draw_bars
height = Array.new(@column_count, 0)
length = Array.new(@column_count, @graph_left)
padding = (@bar_width * (1 - @bar_spacing)) / 2
-
+ if @show_labels_for_bar_values
+ label_values = Array.new
+ 0.upto(@column_count-1) {|i| label_values[i] = {:value => 0, :right_x => 0}}
+ end
@norm_data.each_with_index do |data_row, row_index|
data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
@@ -53,7 +56,12 @@ def draw_bars
right_y = left_y + @bar_width * @bar_spacing
length[point_index] += difference
height[point_index] += (data_point * @graph_width - 2)
-
+
+ if @show_labels_for_bar_values
+ label_values[point_index][:value] += @norm_data[row_index][3][point_index]
+ label_values[point_index][:right_x] = right_x
+ end
+
# if a data point is 0 it can result in weird really thing lines
# that shouldn't even be there being drawn on top of the existing
# bar - this is bad
@@ -68,7 +76,13 @@ def draw_bars
end
end
-
+ if @show_labels_for_bar_values
+ label_values.each_with_index do |data, i|
+ val = (@label_formatting || "%.2f") % data[:value]
+ draw_value_label(data[:right_x]+40, (@graph_top + (((i+1) * @bar_width) - (@bar_width / 2)))-12, val.commify, true)
+ end
+ end
+
@d.draw(@base_image)
end
View
17 test/test_sidestacked_bar.rb
@@ -72,6 +72,23 @@ def test_should_space_long_left_labels_appropriately
end
g.write "test/output/side_stacked_bar_long_label.png"
end
+
+ def test_bar_labels
+ g = Gruff::SideStackedBar.new
+ g.title = "Stacked Bar Long Label"
+ g.font = "/Library/Fonts/Verdana.ttf"
+ g.labels = {
+ 0 => 'September',
+ 1 => 'Oct',
+ 2 => 'Nov',
+ 3 => 'Dec',
+ }
+ @datasets.each do |data|
+ g.data(data[0], data[1])
+ end
+ g.show_labels_for_bar_values = true
+ g.write "test/output/side_stacked_bar_labels.png"
+ end
protected
Please sign in to comment.
Something went wrong with that request. Please try again.