@@ -2,32 +2,41 @@ module Rubyplot
22 module Artist
33 module Plot
44 # Class for holding multiple Bar plot objects.
5+ # Terminoligies used:
6+ #
7+ # * A 'bar' is a single bar of a single bar plot.
8+ # * A 'slot' is a box within which multiple bars can be plotted.
9+ # * 'padding' is the total whitespace on the left and right of a slot.
510 class MultiBars < Artist ::Plot ::Base
611 # The max. width that each bar can occupy.
712 attr_reader :max_bar_width
813
9- def initialize ( *, bar_plots :)
10- super
14+ def initialize ( *args , bar_plots :)
15+ super ( args [ 0 ] )
1116 @bar_plots = bar_plots
1217 end
1318
1419 def draw
1520 configure_plot_geometry_data
16- configure_x_ticks
17- broadcast_to_bar_plots
21+ #configure_x_ticks
1822 @bar_plots . each ( &:draw )
1923 end
2024
2125 private
2226
2327 def configure_plot_geometry_data
24- max_bars = @bar_plots . map { |bar | bar . num_bars } . max
25- @max_bar_width = ( @axes . x_axis . abs_x2 - @axes . x_axis . abs_x1 ) . abs
26- @bar_plots . each do |bar |
27- set_bar_coords bar
28+ @num_max_slots = @bar_plots . map { |bar | bar . num_bars } . max
29+ @max_slot_width = ( @axes . x_axis . abs_x2 - @axes . x_axis . abs_x1 ) . abs / @num_max_slots
30+ # FIXME: figure out a way to specify inter-box space somehow.
31+ @spacing_ratio = @bar_plots [ 0 ] . spacing_ratio
32+ @padding = @spacing_ratio * @max_slot_width
33+ @max_bars_width = @max_slot_width - @padding
34+ @bars_per_slot = @bar_plots . size
35+ @bar_plots . each_with_index do |bar , index |
36+ set_bar_dims bar , index
2837 end
2938 end
30-
39+
3140 def configure_x_ticks
3241 if @axes . x_ticks # user supplied ticks
3342
@@ -36,8 +45,10 @@ def configure_x_ticks
3645 end
3746 end
3847
39- def set_bar_coords bar_plot
40-
48+ def set_bar_dims bar_plot , index
49+ bar_plot . bar_width = @max_bars_width / @bars_per_slot
50+ bar_plot . abs_x_left = @padding / 2 + index * bar_plot . bar_width
51+ bar_plot . abs_y_left = @axes . x_axis . abs_y1
4152 end
4253 end # class MultiBars
4354 end # module Plot
0 commit comments