@@ -23,10 +23,14 @@ class Axes < Base
23
23
attr_reader :y_axis
24
24
# Array of X ticks.
25
25
attr_reader :x_ticks
26
+ # Array of Y ticks.
27
+ attr_reader :y_ticks
26
28
# Array denoting co-ordinates in pixels of the origin of X and Y axes.
27
29
attr_reader :origin
28
30
# Number of X ticks.
29
31
attr_accessor :num_x_ticks
32
+ # Number of Y ticks.
33
+ attr_accessor :num_y_ticks
30
34
# Position of the legend box.
31
35
attr_accessor :legend_box_position
32
36
# Set true if title is to be hidden.
@@ -78,7 +82,9 @@ def initialize(figure)
78
82
@x_axis = Rubyplot ::Artist ::XAxis . new ( self )
79
83
@y_axis = Rubyplot ::Artist ::YAxis . new ( self )
80
84
@x_ticks = nil
85
+ @y_ticks = nil
81
86
@num_x_ticks = 5
87
+ @num_y_ticks = 4
82
88
@legend_box_position = :top
83
89
end
84
90
@@ -107,6 +113,7 @@ def draw
107
113
configure_title
108
114
configure_legends
109
115
assign_x_ticks
116
+ assign_y_ticks
110
117
actually_draw
111
118
end
112
119
@@ -179,6 +186,10 @@ def x_ticks= x_ticks
179
186
@x_ticks = x_ticks
180
187
end
181
188
189
+ def y_ticks = y_ticks
190
+ @y_ticks = y_ticks
191
+ end
192
+
182
193
def x_title = x_title
183
194
@x_axis . title = x_title
184
195
end
@@ -221,6 +232,26 @@ def assign_x_ticks
221
232
end
222
233
end
223
234
235
+ def assign_y_ticks
236
+ unless @y_ticks
237
+ val_distance = ( @y_range [ 1 ] - @y_range [ 0 ] ) . abs / @num_y_ticks . to_f
238
+ @y_ticks = ( @y_range [ 0 ] ..@y_range [ 1 ] ) . step ( val_distance ) . map { |i | i }
239
+ end
240
+ unless @y_ticks . all? { |t | t . is_a? ( Rubyplot ::Artist ::YTick ) }
241
+ inter_ticks_distance = @y_axis . length / ( @num_y_ticks - 1 )
242
+ @y_ticks . map! . with_index do |tick_label , i |
243
+ Rubyplot ::Artist ::YTick . new (
244
+ self ,
245
+ abs_x : @origin [ 0 ] ,
246
+ abs_y : @y_axis . abs_y1 - ( i * inter_ticks_distance ) ,
247
+ label : Rubyplot ::Utils . format_label ( tick_label ) ,
248
+ length : 6 ,
249
+ label_distance : 50
250
+ )
251
+ end
252
+ end
253
+ end
254
+
224
255
def add_plot plot_type , *args , &block
225
256
plot = with_backend plot_type , *args
226
257
yield ( plot ) if block_given?
@@ -271,6 +302,7 @@ def normalize_plotting_data
271
302
def actually_draw
272
303
@x_axis . draw
273
304
@x_ticks . each ( &:draw )
305
+ @y_ticks . each ( &:draw )
274
306
@y_axis . draw
275
307
@texts . each ( &:draw )
276
308
@legend_box . draw
0 commit comments