Class: Rubyplot::StackedBar

Inherits:
Bar show all
Defined in:
lib/rubyplot/scripting/stacked_bar/data.rb,
lib/rubyplot/scripting/stacked_bar/draw.rb,
lib/rubyplot/scripting/stacked_bar/init.rb

Constant Summary collapse

DATA_VALUES_INDEX =
Rubyplot::Artist::DATA_VALUES_INDEX

Constants inherited from Artist

Artist::DATA_COLOR_INDEX, Artist::DATA_LABEL_INDEX, Artist::DATA_VALUES_X_INDEX, Artist::DEFAULT_MARGIN, Artist::DEFAULT_TARGET_WIDTH, Artist::LABEL_MARGIN, Artist::LEGEND_MARGIN, Artist::THOUSAND_SEPARATOR

Instance Attribute Summary

Attributes inherited from Bar

#bar_spacing, #graph_height, #graph_top, #minimum_value, #mode, #spread, #zero

Attributes inherited from Artist

#base_image, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_numbers, #labels, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_shadow_color, #maximum_value, #minimum_value, #title, #title_margin, #x_axis_label, #y_axis_label

Instance Method Summary collapse

Methods inherited from Bar

#draw_bars, #get_left_y_right_y_scaled, #spacing_factor=

Methods inherited from Artist

#artist_draw, #calculate_caps_height, #calculate_spread, #calculate_width, #center, #clip_value_if_greater_than, #construct_colors_array, #data, #draw_axis_labels, #draw_label, #draw_legend, #draw_line_markers!, #draw_title, #get_colors_array, #initialize_variables, #normalize, #render_gradiated_background, #reset_themes, #scale, #scale_fontsize, #set_colors_array, #setup_drawing, #setup_graph_measurements, #significant, #sort_norm_data, #sum, #theme=, #write

Constructor Details

#initializeStackedBar

A stacked bar graph (or stacked bar chart) is a chart that uses bars to show comparisons between categories of data, but with ability to break down and compare parts of a whole. Each bar in the chart represents a whole, and segments in the bar represent different parts or categories of that whole.

Example

plot = Rubyplot::StackedBar.new(400)

]

plot.title = 'Stacked Bar' plot.labels =

0 => '10',
1 => '15',
2 => '20',
3 => '30'

end plot.write('stacked_bar.png')


30
31
32
33
# File 'lib/rubyplot/scripting/stacked_bar/init.rb', line 30

def initialize(*)
  super
  @geometry = Rubyplot::StackedBarGeometry.new
end

Instance Method Details

#drawObject

Draws a bar graph, but multiple sets are stacked on top of each other. Modified drawing feature of the bar graph with multiple bar graphs stacked on top of each other.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rubyplot/scripting/stacked_bar/draw.rb', line 15

def draw
  max_stack_height
  artist_draw

  set_spacings
  @d = @d.stroke_opacity 0.0
  height = Array.new(@geometry.column_count, 0)

  @geometry.norm_data.each_with_index do |data_row, _row_index|
    data_row[DATA_VALUES_INDEX].each_with_index do |data_point, point_index|
      @d = @d.fill @plot_colors[_row_index]

      # Calculate center based on bar_width and current row
      label_center = @graph_left + (@bar_width * point_index) + (@bar_width * @bar_spacing / 2.0)
      draw_label(label_center, 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] - @segment_spacing

      # update the total height of the current stacked bar
      height[point_index] += (data_point * @graph_height)

      # Draw the rectangle
      @d = @d.rectangle(left_x, left_y, right_x, right_y)
    end
  end

  @d.draw(@base_image)
end

#max_stack_heightObject


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubyplot/scripting/stacked_bar/data.rb', line 3

def max_stack_height
  # Get the sum of values in each stack to the get the stack height
  max_hash = {}
  @data.each do |data_set|
    data_set[DATA_VALUES_INDEX].each_with_index do |data_point, i|
      max_hash[i] = 0.0 unless max_hash[i]
      max_hash[i] += data_point.to_f
    end
  end

  @geometry.maximum_value = 0
  max_hash.keys.each do |key|
    @geometry.maximum_value = max_hash[key] if max_hash[key] > @geometry.maximum_value
  end
  @geometry.minimum_value = 0
end

#set_spacingsObject


2
3
4
5
6
7
8
9
10
# File 'lib/rubyplot/scripting/stacked_bar/draw.rb', line 2

def set_spacings
  # Setup spacing.
  #
  # Columns sit stacked.
  @bar_spacing ||= 0.9
  @segment_spacing ||= 1
  @bar_width = @graph_width / @geometry.column_count.to_f
  @padding = (@bar_width * (1 - @bar_spacing)) / 2
end