Skip to content

Commit 0f10a6f

Browse files
committed
plots multiple bar graphs
1 parent 9d3fb00 commit 0f10a6f

5 files changed

Lines changed: 63 additions & 52 deletions

File tree

lib/rubyplot/artist/axes.rb

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class Axes < Base
4545
attr_reader :x_axis
4646
# Rubyplot::Artist::YAxis object.
4747
attr_reader :y_axis
48+
# Array of X ticks.
49+
attr_reader :x_ticks
50+
# Number of X ticks.
51+
attr_accessor :num_x_ticks
4852

4953
# @param figure [Rubyplot::Figure] Figure object to which this Axes belongs.
5054
def initialize figure
@@ -85,6 +89,8 @@ def initialize figure
8589
calculate_xy_axes_origin
8690
@x_axis = Rubyplot::Artist::XAxis.new(self)
8791
@y_axis = Rubyplot::Artist::YAxis.new(self)
92+
@x_ticks = nil
93+
@num_x_ticks = 5
8894

8995
@legend_box_position = :top
9096
end
@@ -107,12 +113,13 @@ def legend_box_iy
107113

108114
# Write an image to a file by communicating with the backend.
109115
def draw
110-
assign_plot_defaults
116+
assign_default_label_colors
111117
consolidate_plots
112118
gather_plot_data
113119
configure_title
114120
configure_legends
115121
configure_plotting_data
122+
assign_x_ticks
116123
actually_draw
117124
end
118125

@@ -147,7 +154,7 @@ def dot! *args, &block
147154
end
148155

149156
def stacked_bar! *args, &block
150-
add_plot "StackedBar", *args, &block
157+
appdd_plot "StackedBar", *args, &block
151158
end
152159

153160
def write file_name
@@ -176,7 +183,7 @@ def height
176183
end
177184

178185
def x_ticks= x_ticks
179-
@x_axis.x_ticks = x_ticks
186+
@x_ticks = x_ticks
180187
end
181188

182189
def x_title= x_title
@@ -189,11 +196,7 @@ def y_title= y_title
189196

190197
private
191198

192-
def assign_plot_defaults
193-
assign_label_colors
194-
end
195-
196-
def assign_label_colors
199+
def assign_default_label_colors
197200
@plots.each_with_index do |p, i|
198201
if p.color == :default
199202
p.color = @figure.theme_options[:label_colors][
@@ -202,6 +205,26 @@ def assign_label_colors
202205
end
203206
end
204207

208+
def assign_x_ticks
209+
unless @x_ticks
210+
val_distance = @x_range[1] / @num_x_ticks.to_f
211+
@x_ticks = Array.new(@num_x_ticks) { |c| (c*val_distance).to_s }
212+
end
213+
unless @x_ticks.all? { |t| t.is_a?(Rubyplot::Artist::XTick) }
214+
inter_ticks_distance = @x_axis.length / @num_x_ticks
215+
@x_ticks.map!.with_index do |tick_label, i|
216+
Rubyplot::Artist::XTick.new(
217+
self,
218+
abs_x: i * inter_ticks_distance + @x_axis.abs_x1,
219+
abs_y: @x_axis.abs_y1,
220+
label: tick_label,
221+
length: 6,
222+
label_distance: 10
223+
)
224+
end
225+
end
226+
end
227+
205228
def add_plot plot_type, *args, &block
206229
plot = with_backend plot_type, *args
207230
yield(plot) if block_given?
@@ -256,6 +279,7 @@ def configure_plotting_data
256279
# Call the respective draw methods on each of the elements of this Axes.
257280
def actually_draw
258281
@x_axis.draw
282+
@x_ticks.each(&:draw)
259283
@y_axis.draw
260284
@title.draw
261285
@legend_box.draw

lib/rubyplot/artist/axis/x_axis.rb

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
module Rubyplot
44
module Artist
55
class XAxis < Axis::Base
6-
attr_accessor :x_ticks
7-
86
def initialize axes
97
super
108
@abs_x1 = @axes.origin[0]
@@ -17,10 +15,8 @@ def initialize axes
1715
end
1816

1917
def draw
20-
populate_major_x_ticks
2118
configure_title
2219
@line.draw
23-
@x_ticks.each(&:draw)
2420
@title.draw
2521
end
2622

@@ -32,27 +28,6 @@ def configure_axis_line
3228
stroke_width: @stroke_width)
3329
end
3430

35-
# FIXME: refactor the tick population logic.
36-
def populate_major_x_ticks
37-
value_distance = (@max_val) / @major_ticks_count.to_f
38-
if !@x_ticks
39-
@x_ticks = Array.new(@major_ticks_count) { |c| (c*value_distance).to_s }
40-
end
41-
@x_ticks = @x_ticks[0...@major_ticks_count]
42-
unless @x_ticks.all? { |x| x.is_a?(XTick) }
43-
@x_ticks.map!.with_index do |tick_label, i|
44-
Rubyplot::Artist::XTick.new(
45-
@axes,
46-
abs_x: i * @major_ticks_distance + @abs_x1,
47-
abs_y: @abs_y1,
48-
label: tick_label,
49-
length: 6,
50-
label_distance: 10
51-
)
52-
end
53-
end
54-
end
55-
5631
def configure_title
5732
@title = Rubyplot::Artist::Text.new(
5833
@title,

lib/rubyplot/artist/axis/y_axis.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ def initialize(*)
1111
@length = (@abs_y1 - @abs_y2).abs
1212
configure_axis_line
1313
configure_title
14-
populate_major_y_ticks
1514
end
1615

1716
def draw
1817
@title.draw
1918
@line.draw
20-
#@y_ticks.each(&:draw)
2119
end
2220

2321
private
@@ -43,10 +41,6 @@ def configure_title
4341
pointsize: @axes.marker_font_size
4442
)
4543
end
46-
47-
def populate_major_y_ticks
48-
49-
end
5044
end # class YAxis
5145
end # class Artist
5246
end # module Rubyplot

lib/rubyplot/artist/legend_box.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def initialize(axes, abs_x:, abs_y:)
2323
end
2424

2525
def draw
26-
@bounding_box.draw
27-
@legends.each(&:draw)
26+
unless @legends.empty?
27+
@bounding_box.draw
28+
@legends.each(&:draw)
29+
end
2830
end
2931

3032
def top_margin
@@ -70,14 +72,16 @@ def configure_dimensions
7072

7173
def configure_legends
7274
@axes.plots.each_with_index do |plot, count|
73-
@legends << Rubyplot::Artist::Legend.new(
74-
self,
75-
@axes,
76-
text: plot.label,
77-
color: plot.color,
78-
abs_x: @abs_x + left_margin,
79-
abs_y: @abs_y + count * per_legend_height + top_margin
80-
)
75+
if plot.label != ""
76+
@legends << Rubyplot::Artist::Legend.new(
77+
self,
78+
@axes,
79+
text: plot.label,
80+
color: plot.color,
81+
abs_x: @abs_x + left_margin,
82+
abs_y: @abs_y + count * per_legend_height + top_margin
83+
)
84+
end
8185
end
8286
end
8387
end # class LegendBox

lib/rubyplot/artist/plot/multi_bars.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ def initialize(*args, bar_plots:)
1818
@y_min = @bar_plots.map(&:y_min).min
1919
@x_max = @bar_plots.map(&:x_max).max
2020
@y_max = @bar_plots.map(&:y_max).max
21+
configure_plot_geometry_data
22+
configure_x_ticks
2123
end
2224

2325
def normalize
2426
@bar_plots.each(&:normalize)
2527
end
2628

2729
def draw
28-
configure_plot_geometry_data
29-
configure_default_x_ticks if @axes.x_axis.x_ticks.nil?
3030
@bar_plots.each(&:draw)
3131
end
3232

@@ -45,8 +45,22 @@ def configure_plot_geometry_data
4545
end
4646
end
4747

48-
def configure_default_x_ticks
48+
def configure_x_ticks
49+
@axes.num_x_ticks = @num_max_slots
4950
labels = @axes.x_ticks || Array.new(@num_max_slots) { |i| i.to_s }
51+
if labels.size != @axes.num_x_ticks
52+
labels = labels[0...@axes.num_x_ticks]
53+
end
54+
@axes.x_ticks = labels.map.with_index do |label, i|
55+
Rubyplot::Artist::XTick.new(
56+
@axes,
57+
abs_x: @axes.abs_x + @axes.y_axis_margin + i * @max_slot_width + @max_slot_width / 2,
58+
abs_y: @axes.x_axis.abs_y1,
59+
label: label,
60+
length: 6,
61+
label_distance: 10
62+
)
63+
end
5064
end
5165

5266
def set_bar_dims bar_plot, index

0 commit comments

Comments
 (0)