@@ -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
0 commit comments