Class Gruff::StackedBar
In: lib/gruff/stacked_bar.rb
Parent: Gruff::Base
StackedMixin StackedBar SideStackedBar StackedArea AccumulatorBar Base Scene Pie Area PhotoBar Bullet Spider SideBar Net Bar Line Dot Pie Observable Group SideBar StandardError IncorrectNumberOfDatasetsException Magick Bar Layer BarConversion lib/gruff/stacked_area.rb lib/gruff/scene.rb lib/gruff/spider.rb lib/gruff/pie.rb lib/gruff/bullet.rb lib/gruff/area.rb lib/gruff/net.rb lib/gruff/bar_conversion.rb lib/gruff/bar.rb lib/gruff/side_bar.rb lib/gruff/line.rb lib/gruff/stacked_bar.rb lib/gruff/dot.rb lib/gruff/side_stacked_bar.rb lib/gruff/photo_bar.rb lib/gruff/base.rb lib/gruff/accumulator_bar.rb lib/gruff/mini/bar.rb lib/gruff/mini/side_bar.rb lib/gruff/mini/pie.rb Legend Mini Deprecated Gruff dot/m_12_0.png

Methods

draw  

Included Modules

StackedMixin

Public Instance methods

Draws a bar graph, but multiple sets are stacked on top of each other.

[Source]

    # File lib/gruff/stacked_bar.rb, line 9
 9:     def draw
10:       get_maximum_by_stack
11:       super
12:       return unless @has_data
13: 
14:       # Setup spacing.
15:       #
16:       # Columns sit stacked.
17:       @bar_spacing ||= 0.9
18:       @bar_width = @graph_width / @column_count.to_f
19:       padding = (@bar_width * (1 - @bar_spacing)) / 2
20:     
21:       @d = @d.stroke_opacity 0.0
22:       
23:       height = Array.new(@column_count, 0)
24:     
25:       @norm_data.each_with_index do |data_row, row_index|      
26:         data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
27:           @d = @d.fill data_row[DATA_COLOR_INDEX]
28:           
29:           # Calculate center based on bar_width and current row
30:           label_center = @graph_left + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
31:           draw_label(label_center, point_index)
32: 
33:           next if (data_point == 0)
34:           # Use incremented x and scaled y
35:           left_x = @graph_left + (@bar_width * point_index) + padding
36:           left_y = @graph_top + (@graph_height -
37:                                  data_point * @graph_height - 
38:                                  height[point_index]) + 1
39:           right_x = left_x + @bar_width * @bar_spacing
40:           right_y = @graph_top + @graph_height - height[point_index] - 1
41:           
42:           # update the total height of the current stacked bar
43:           height[point_index] += (data_point * @graph_height ) 
44:           
45:           @d = @d.rectangle(left_x, left_y, right_x, right_y)
46:           
47:         end
48: 
49:       end
50:     
51:       @d.draw(@base_image)    
52:     end

[Validate]