lib/gruffy/side_stacked_bar.rb in gruffy-1.1.0 vs lib/gruffy/side_stacked_bar.rb in gruffy-1.2.0
- old
+ new
@@ -25,29 +25,57 @@
@bar_width = @graph_height / @column_count.to_f
@d = @d.stroke_opacity 0.0
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|
+
+ difference_pixels = []
+ @norm_data.each do |data_row|
+ temps = []
data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
+ temp1 = @graph_left + (@graph_width - data_point * @graph_width - height[point_index])
+ temp2 = @graph_left + @graph_width - height[point_index]
+ temps << (temp2 - temp1)
+ end
+ difference_pixels << temps
+ end
+
+ difference_count = 1
+ begin
+ temps = difference_pixels.first
+ difference_pixels.shift
+ difference_pixels.each {|t| temps = temps.zip(t) }
+ difference_count = temps.first.flatten.length
+ temps = temps.map {|r| r.flatten.sum }
+ difference_pixels = temps
+ rescue
+ end
+
+ @norm_data.each_with_index do |data_row, row_index|
+ data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
## using the original calcs from the stacked bar chart to get the difference between
## part of the bart chart we wish to stack.
+ pixel_missing = ((@graph_width - difference_pixels[point_index].to_s.to_f) / difference_count)
+
temp1 = @graph_left + (@graph_width - data_point * @graph_width - height[point_index])
temp2 = @graph_left + @graph_width - height[point_index]
- difference = temp2 - temp1
+ difference = temp2 - temp1 + pixel_missing
@d = @d.fill data_row[DATA_COLOR_INDEX]
- left_x = length[point_index] #+ 1
- left_y = @graph_top + (@bar_width * point_index) + padding
+ left_x = length[point_index]
+ left_y = @graph_top + (@bar_width * point_index) + padding
+
right_x = left_x + difference
- right_y = left_y + @bar_width * @bar_spacing
+ 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]
@@ -66,9 +94,10 @@
label_center = @graph_top + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
draw_label(label_center, point_index)
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