Skip to content

Commit 9d3fb00

Browse files
committed
configuring proper tick labels for bar plots
1 parent 56247f7 commit 9d3fb00

7 files changed

Lines changed: 59 additions & 55 deletions

File tree

lib/rubyplot/artist/axes.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ class Axes < Base
1212

1313
# FIXME: most of the below accessors should just be name= methods which
1414
# will access the required Artist and set the variable in there directly.
15-
# Title of the X axis
16-
attr_accessor :x_title
17-
# Title of the Y axis.
18-
attr_accessor :y_title
1915
# Range of X axis.
2016
attr_accessor :x_range
2117
# Range of Y axis.
@@ -60,8 +56,7 @@ def initialize figure
6056
@y_axis_margin = 40.0
6157
@x_range = [nil, nil]
6258
@y_range = [nil, nil]
63-
64-
@origin = [nil, nil]
59+
6560
@title = ""
6661
@title_shift = 0
6762
@title_margin = TITLE_MARGIN
@@ -86,8 +81,10 @@ def initialize figure
8681
@legends = []
8782
@lines = []
8883
@texts = []
89-
@x_axis = nil
90-
@y_axis = nil
84+
@origin = [nil, nil]
85+
calculate_xy_axes_origin
86+
@x_axis = Rubyplot::Artist::XAxis.new(self)
87+
@y_axis = Rubyplot::Artist::YAxis.new(self)
9188

9289
@legend_box_position = :top
9390
end
@@ -114,8 +111,6 @@ def draw
114111
consolidate_plots
115112
gather_plot_data
116113
configure_title
117-
calculate_xy_axes_origin
118-
configure_xy_axes
119114
configure_legends
120115
configure_plotting_data
121116
actually_draw
@@ -180,10 +175,18 @@ def height
180175
(1 - (@figure.top_spacing + @figure.bottom_spacing)) * @figure.height
181176
end
182177

183-
def x_ticks= ticks_hash
184-
@x_ticks = ticks_hash
178+
def x_ticks= x_ticks
179+
@x_axis.x_ticks = x_ticks
185180
end
186181

182+
def x_title= x_title
183+
@x_axis.title = x_title
184+
end
185+
186+
def y_title= y_title
187+
@y_axis.title = y_title
188+
end
189+
187190
private
188191

189192
def assign_plot_defaults
@@ -236,14 +239,6 @@ def calculate_xy_axes_origin
236239
@origin[1] = abs_y + height - @y_axis_margin
237240
end
238241

239-
# Figure out co-ordinatees of the XAxis and YAxis
240-
def configure_xy_axes
241-
@x_axis = Rubyplot::Artist::XAxis.new(
242-
self, @x_title, @x_range[0], @x_range[1])
243-
@y_axis = Rubyplot::Artist::YAxis.new(
244-
self, @y_title, @y_range[0], @y_range[1])
245-
end
246-
247242
# Figure out co-ordinates of the legends
248243
def configure_legends
249244
@legend_box = Rubyplot::Artist::LegendBox.new(
@@ -310,6 +305,7 @@ def consolidate_plots
310305
end
311306
end
312307

308+
# FIXME: replace x_range and y_range with XAxis::max/min_value and YAxis::max/min_value.
313309
def gather_plot_data
314310
set_xrange
315311
set_yrange
@@ -320,13 +316,17 @@ def set_xrange
320316
@x_range[0] = @plots.map { |p| p.x_min }.min
321317
@x_range[1] = @plots.map { |p| p.x_max }.max
322318
end
319+
@x_axis.min_val = @x_range[0]
320+
@x_axis.max_val = @x_range[1]
323321
end
324322

325323
def set_yrange
326324
if @y_range[0].nil? && @y_range[1].nil?
327325
@y_range[0] = @plots.map { |p| p.y_min }.min
328326
@y_range[1] = @plots.map { |p| p.y_max }.max
329327
end
328+
@y_axis.min_val = @y_range[0]
329+
@y_axis.max_val = @y_range[1]
330330
end
331331
end # class Axes
332332
end # moudle Artist

lib/rubyplot/artist/axis/base.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ class Base
55
# Length in pixels of the arrow after the last major tick.
66
FINISH_ARROW_LENGTH = 10.0
77

8-
attr_reader :label, :ticks, :major_ticks_count, :min_val, :max_val, :title
8+
attr_reader :label, :ticks, :major_ticks_count
99
attr_reader :abs_x1, :abs_x2, :abs_y1, :abs_y2, :backend, :length
1010
attr_reader :stroke_width, :major_ticks
11+
attr_accessor :title, :min_val, :max_val
1112

12-
def initialize axes, title, min_val, max_val
13+
def initialize axes
1314
@axes = axes
14-
@title = title
15-
@min_val = min_val
16-
@max_val = max_val
15+
@title = ""
16+
@min_val = nil
17+
@max_val = nil
1718
@stroke_width = 1.0
1819
@backend = @axes.backend
19-
@major_ticks = []
20+
@major_ticks_count = 5
21+
@x_ticks = nil
2022
end
2123
end # class Base
2224
end # class Axis

lib/rubyplot/artist/axis/x_axis.rb

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
module Rubyplot
44
module Artist
55
class XAxis < Axis::Base
6-
def initialize(*)
6+
attr_accessor :x_ticks
7+
8+
def initialize axes
79
super
810
@abs_x1 = @axes.origin[0]
911
@abs_y1 = @axes.origin[1]
1012
@abs_x2 = @axes.abs_x + @axes.width - @axes.y_axis_margin
1113
@abs_y2 = @axes.origin[1]
12-
@major_ticks_count = 5
1314
@major_ticks_distance = (@abs_x2 - @abs_x1) / @major_ticks_count
1415
@length = (@abs_x2 - @abs_x1).abs
1516
configure_axis_line
16-
populate_major_x_ticks
17-
configure_title
1817
end
1918

2019
def draw
20+
populate_major_x_ticks
21+
configure_title
2122
@line.draw
22-
@major_ticks.each(&:draw)
23+
@x_ticks.each(&:draw)
2324
@title.draw
2425
end
2526

@@ -30,19 +31,25 @@ def configure_axis_line
3031
self, abs_x1: @abs_x1, abs_y1: @abs_y1, abs_x2: @abs_x2, abs_y2: @abs_y2,
3132
stroke_width: @stroke_width)
3233
end
33-
34+
35+
# FIXME: refactor the tick population logic.
3436
def populate_major_x_ticks
3537
value_distance = (@max_val) / @major_ticks_count.to_f
36-
@major_ticks_count.times do |count|
37-
count += 1
38-
@major_ticks << Rubyplot::Artist::XTick.new(
39-
@axes,
40-
abs_x: count * @major_ticks_distance + @abs_x1,
41-
abs_y: @abs_y1,
42-
label: (count * value_distance),
43-
length: 6,
44-
label_distance: 10
45-
)
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
4653
end
4754
end
4855

lib/rubyplot/artist/plot/multi_bars.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def normalize
2626

2727
def draw
2828
configure_plot_geometry_data
29-
#configure_x_ticks
29+
configure_default_x_ticks if @axes.x_axis.x_ticks.nil?
3030
@bar_plots.each(&:draw)
3131
end
3232

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

48-
def configure_x_ticks
49-
if @axes.x_ticks # user supplied ticks
50-
51-
else # default ticks
52-
53-
end
48+
def configure_default_x_ticks
49+
labels = @axes.x_ticks || Array.new(@num_max_slots) { |i| i.to_s }
5450
end
5551

5652
def set_bar_dims bar_plot, index

lib/rubyplot/artist/tick/base.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Rubyplot
22
module Artist
33
class Tick
4-
class Base
4+
class Base < Artist::Base
55
# Distance between tick mark and number.
66
attr_reader :label_distance
77
# Label of this Tick
@@ -18,15 +18,13 @@ class Base
1818
# @param tick_opacity [Float] Number describing the opacity of the tick drawn. 0-1.0.
1919
def initialize(owner,abs_x:,abs_y:,length:,label:,label_distance:,
2020
tick_opacity: 0.0,tick_width: 1.0)
21+
super(owner.backend, abs_x, abs_y)
2122
@owner = owner
22-
@abs_x = abs_x
23-
@abs_y = abs_y
2423
@length = length
25-
@label_text = Rubyplot::Utils.format_label label
24+
@label_text = label #Rubyplot::Utils.format_label label
2625
@label_distance = label_distance
2726
@tick_opacity = tick_opacity
2827
@tick_width = tick_width
29-
@backend = @owner.backend
3028
end
3129
end # class Base
3230
end # class Tick

lib/rubyplot/artist/tick/x_tick.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ def draw
1919
stroke_width: @tick_width)
2020
@label.draw
2121
end
22-
end
22+
end # class XTick
2323
end # class Artist
2424
end # module Rubyplot

spec/axes_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@
457457
p.label = "data"
458458
p.color = :yellow
459459
end
460+
axes.x_ticks = ["five", "twelve", "nine", "six", "seven"]
460461
axes.title = "Random bar numbers"
461462

462463
file = "/#{Rubyplot.backend}_simple_bar.png"

0 commit comments

Comments
 (0)