lib/lilygraph.rb in lilygraph-0.5.0 vs lib/lilygraph.rb in lilygraph-0.5.1
- old
+ new
@@ -18,11 +18,11 @@
:height => '100%',
:width => '100%',
:indent => 2,
:padding => 14,
:legend => :right,
- :bar_text => true,
+ :bar_text => :number,
:type => :bar,
:viewbox => {
:width => 800,
:height => 600
},
@@ -222,13 +222,15 @@
bar_width = (width / Float(data.size)).round
x = (@options[:margin][:left] + (dx * data_index)).round
# Text
- if @options[:bar_text]
+ if @options[:bar_text] != :none
last_bar_height = false
data.each_with_index do |number, number_index|
+ percent_total = data.inject(0) { |total, percent_number| total + percent_number }
+
if number > 0
height = ((dy / division) * number).round
bar_x = (x + ((dx - width) / 2.0) + (number_index * bar_width)).round
text_x = (bar_x + (bar_width / 2.0)).round
@@ -241,10 +243,15 @@
last_bar_height = false
else
last_bar_height = height
end
- xml.text number, :x => text_x, :y => text_y, 'text-anchor' => 'middle'
+ label = case @options[:bar_text]
+ when :number then number
+ when :percent then (100.0 * Float(number) / Float(percent_total)).round.to_s + "%"
+ end
+
+ xml.text label, :x => text_x, :y => text_y, 'text-anchor' => 'middle'
else
last_bar_height = false
end
end
end