Skip to content

Commit 5b76bf5

Browse files
committed
Added dimension calculation for bar plots
1 parent 29e6bbe commit 5b76bf5

9 files changed

Lines changed: 46 additions & 35 deletions

File tree

lib/rubyplot/artist/axes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ def label_string(value, increment)
295295
end
296296

297297
def consolidate_plots
298-
bars = @plots.map { |p| p.is_a?(Rubyplot::Artist::Plot::Bar) }
298+
bars = @plots.grep(Rubyplot::Artist::Plot::Bar)
299299
@plots.delete_if { |p| p.is_a?(Rubyplot::Artist::Plot::Bar) }
300-
@plots << Rubyplot::Artist::Plot::MultiBars.new(self, bars)
300+
@plots << Rubyplot::Artist::Plot::MultiBars.new(self, bar_plots: bars)
301301
end
302302
end # class Axes
303303
end # moudle Artist

lib/rubyplot/artist/axis/x_axis.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def initialize(*)
1818

1919
def draw
2020
@line.draw
21-
@x_ticks.each(&:draw)
21+
@major_ticks.each(&:draw)
2222
@title.draw
2323
end
2424

@@ -31,6 +31,7 @@ def configure_axis_line
3131
end
3232

3333
def populate_major_x_ticks
34+
puts "max_val #{@max_val} #{@major_ticks_count}"
3435
value_distance = (@max_val) / @major_ticks_count.to_f
3536
@major_ticks_count.times do |count|
3637
count += 1

lib/rubyplot/artist/legend_box.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class LegendBox < Base
1515
def initialize(axes, abs_x:, abs_y:)
1616
super(axes.backend, abs_x, abs_y)
1717
@axes = axes
18-
@border_color = "#000000"
18+
@border_color = :black
1919
@legends = []
2020
configure_dimensions
2121
configure_legends

lib/rubyplot/artist/plot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
require_relative 'plot/scatter'
33
require_relative 'plot/line'
44
require_relative 'plot/bar'
5+
require_relative 'plot/multi_bars'

lib/rubyplot/artist/plot/bar.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@ module Plot
44
class Bar < Artist::Plot::Base
55
# Space between the columns.
66
attr_accessor :bar_spacing
7-
# Width of each bar.
7+
# Width of each bar in pixels.
88
attr_accessor :bar_width
9+
# Height of each bar in pixels.
10+
attr_accessor :bar_height
911
# Number between 0 and 1.0 denoting spacing between the bars.
1012
# 0.0 means no spacing at all 1.0 means that each bars' width
1113
# is nearly 0 (so each bar is a simple line with no X dimension).
14+
# Denotes the total left + right side space.
1215
attr_reader :spacing_ratio
16+
# X co-ordinate of the lower left corner of the bar.
17+
attr_accessor :abs_x_left
18+
# Y co-ordinate of the lower left corner of the bar.
19+
attr_accessor :abs_y_left
1320
def initialize(*)
1421
super
1522
@spacing_ratio = 0.1
@@ -24,8 +31,7 @@ def spacing_ratio= sf
2431

2532
# Set Bar plot data.
2633
def data y_values
27-
@data[:y_values] = y_values
28-
set_yrange
34+
super(Array.new(y_values.size) { |i| i}, y_values)
2935
end
3036

3137
# Number of bars in this Bar plot
@@ -34,22 +40,14 @@ def num_bars
3440
end
3541

3642
def draw
37-
super
3843
return unless @axes.geometry.has_data
39-
configure_bars
44+
setup_bar_heights
4045
end
4146

4247
private
4348

44-
# FIXME: handle positive and negative bars.
45-
def configure_bars
46-
x_axis_length = @axes.x_axis.abs_x2 - @axes.x_axis.abs_x1
47-
@bar_width = coords_width / @data[:y_values].to_f
48-
padding = @bar_width * @spacing_ratio / 2
49-
@normalized_data[:y_values].each_with_index do |iy, index|
50-
ix = @normalized_data[:x_values][index]
51-
left_x = ix * x_axis_length
52-
end
49+
def setup_bar_heights
50+
5351
end
5452
end # class Bar
5553
end # module Plot

lib/rubyplot/artist/plot/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize axes
99
@axes = axes
1010
@backend = @axes.backend
1111
@data = {
12-
label: :default,
12+
label: "",
1313
color: :default
1414
}
1515
@normalized_data = {
@@ -33,7 +33,7 @@ def label= label
3333
end
3434

3535
def color= color
36-
@data[:color] = Rubyplot::Color::COLOR_INDEX[color]
36+
@data[:color] = color
3737
end
3838

3939
def data x_values, y_values

lib/rubyplot/artist/plot/multi_bars.rb

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/rubyplot/artist/rectangle.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def initialize(owner,abs_x:,abs_y:,width:,height:,border_color:,fill_color: nil)
77
super(owner.backend, abs_x, abs_y)
88
@height = height
99
@width = width
10-
@border_color = border_color
11-
@fill_color = fill_color
10+
@border_color = Rubyplot::Color::COLOR_INDEX[border_color]
11+
@fill_color = Rubyplot::Color::COLOR_INDEX[fill_color] if fill_color
1212
end
1313

1414
def draw

spec/axes_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275

276276
end
277277

278-
it "makes a simple line plot", focus: true do
278+
it "makes a simple line plot", focus: false do
279279
fig = Rubyplot::Figure.new
280280
axes = fig.add_subplot 0,0
281281
axes.line! do |p|
@@ -650,7 +650,7 @@
650650
# FileUtils.rm_rf SPEC_ROOT + "temp/scatter"
651651
end
652652

653-
it "adds a simple scatter plot.", focus: true do
653+
it "adds a simple scatter plot.", focus: false do
654654
fig = Rubyplot::Figure.new
655655
axes = fig.add_subplot 0,0
656656
axes.scatter!(400) do |p|

0 commit comments

Comments
 (0)