lib/gruff/stacked_bar.rb
# Draws a bar graph, but multiple sets are stacked on top of each other.
Draws a bar graph, but multiple sets are stacked on top of each other.
Draws a bar graph but multiple sets are stacked on top of each other .
def draw_graph
     Columns sit stacked.
    bar_width = @graph_width / column_count.to_f
    padding = (bar_width * (1 - @bar_spacing)) / 2
    height = Array.new(column_count, 0)
    stack_bar_value_label = Gruff::BarValueLabel::StackedBar.new
    store.norm_data.each_with_index do |data_row, row_index|
      data_row.points.each_with_index do |data_point, point_index|
        next if data_point == 0
         Use incremented x and scaled y
        left_x = @graph_left + (bar_width * point_index) + padding
        left_y = @graph_top + (@graph_height -
                               data_point * @graph_height -
                               height[point_index]) + @segment_spacing
        right_x = left_x + bar_width * @bar_spacing
        right_y = @graph_top + @graph_height - height[point_index]
         update the total height of the current stacked bar
        height[point_index] += (data_point * @graph_height)
        rect_renderer = Gruff::Renderer::Rectangle.new(renderer, color: data_row.color)
        rect_renderer.render(left_x, left_y, right_x, right_y)
         Calculate center based on bar_width and current row
        label_center = left_x + bar_width * @bar_spacing / 2.0
        draw_label(label_center, point_index)
        bar_value_label = Gruff::BarValueLabel::Bar.new([left_x, left_y, right_x, right_y], store.data[row_index].points[point_index])
        stack_bar_value_label.add(bar_value_label, point_index)
      end
    end
    if @show_labels_for_bar_values
      stack_bar_value_label.prepare_rendering(@label_formatting, bar_width) do |x, y, text|
        draw_value_label(x, y, text)
      end
    end
  end
def draw_graph bar_width = @graph_width / column_count . to_f padding = ( bar_width * ( 1 - @bar_spacing ) ) / 2 height = Array . new ( column_count , 0 ) stack_bar_value_label = Gruff :: BarValueLabel :: StackedBar . new store . norm_data . each_with_index do | data_row , row_index | data_row . points . each_with_index do | data_point , point_index | next if data_point == 0 left_x = @graph_left + ( bar_width * point_index ) + padding left_y = @graph_top + ( @graph_height - data_point * @graph_height - height [ point_index ] ) + @segment_spacing right_x = left_x + bar_width * @bar_spacing right_y = @graph_top + @graph_height - height [ point_index ] height [ point_index ] += ( data_point * @graph_height ) rect_renderer = Gruff :: Renderer :: Rectangle . new ( renderer , color : data_row . color ) rect_renderer . render ( left_x , left_y , right_x , right_y ) label_center = left_x + bar_width * @bar_spacing / 2.0 draw_label ( label_center , point_index ) bar_value_label = Gruff :: BarValueLabel :: Bar . new ( [ left_x , left_y , right_x , right_y ] , store . data [ row_index ] . points [ point_index ] ) stack_bar_value_label . add ( bar_value_label , point_index ) end end if @show_labels_for_bar_values stack_bar_value_label . prepare_rendering ( @label_formatting , bar_width ) do | x , y , text | draw_value_label ( x , y , text ) end end end
Draws a bar graph, but multiple sets are stacked on top of each other.
Draws a bar graph but multiple sets are stacked on top of each other .
# Setup spacing. # # Columns sit stacked. # Use incremented x and scaled y # update the total height of the current stacked bar # Calculate center based on bar_width and current row