Skip to content

Commit 722ada5

Browse files
committed
added bar plot class
1 parent efe870b commit 722ada5

5 files changed

Lines changed: 38 additions & 3 deletions

File tree

lib/rubyplot/artist/axes.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ def scatter! *args, &block
132132
end
133133

134134
def bar! *args, &block
135-
add_plot "Bar", *args, &block
135+
plot = Rubyplot::Artist::Plot::Bar.new self
136+
yield(plot) if block_given?
137+
@plots << plot
136138
end
137139

138140
def line! *args, &block

lib/rubyplot/artist/plot.rb

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

lib/rubyplot/artist/plot/bar.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module Rubyplot
2+
module Artist
3+
module Plot
4+
class Bar < Artist::Plot::Base
5+
# Space between the columns.
6+
attr_accessor :bar_spacing
7+
# Number between 0 and 1.0 denoting spacing between the bars.
8+
# 0.0 means no spacing at all 1.0 means that each bars' width
9+
# is nearly 0 (so each bar is a simple line with no X dimension).
10+
attr_reader :spacing_factor
11+
def initialize(*)
12+
super
13+
@spacing_factor = 0.9
14+
end
15+
16+
# Set the spacing factor for this bar plot.
17+
def spacing_factor= sf
18+
raise ValueError, '@spacing_factor must be between 0.00 and 1.00' unless
19+
(sf >= 0) && (sf <= 1)
20+
@spacing_factor = sf
21+
end
22+
23+
def draw
24+
super
25+
return unless @axes.geometry.has_data
26+
end
27+
end # class Bar
28+
end # module Plot
29+
end # module Artist
30+
end # module Rubyplot
31+

lib/rubyplot/figure.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def initialize
2222
@backend = Rubyplot::Backend::MagickWrapper.new
2323
@width = DEFAULT_TARGET_WIDTH
2424
@height = @width * 0.75
25+
@x = 0
26+
@y = 0
2527
add_subplots @nrows, @ncols
2628
end
2729

spec/axes_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@
449449
# FileUtils.rm_rf SPEC_ROOT + "temp/bar"
450450
end
451451

452-
it "adds a simple bar plot" do
452+
it "adds a simple bar plot", focus: true do
453453
fig = Rubyplot::Figure.new
454454
axes = fig.add_subplot 0,0
455455
axes.bar!(600) do |p|
@@ -524,7 +524,6 @@
524524
end
525525

526526
skip "adds multiple bar plots for wide graph" do
527-
528527
fig = Rubyplot::Figure.new
529528
axes = fig.add_subplot 0,0
530529
data.each do |name, nums|

0 commit comments

Comments
 (0)