@@ -56,6 +56,10 @@ class Axes < Base
5656 attr_accessor :y_axis_margin
5757 # Position of the legend box.
5858 attr_accessor :legend_box_position
59+ # Rubyplot::Artist::XAxis object.
60+ attr_reader :x_axis
61+ # Rubyplot::Artist::YAxis object.
62+ attr_reader :y_axis
5963
6064 # @param figure [Rubyplot::Figure] Figure object to which this Axes belongs.
6165 def initialize figure
@@ -124,9 +128,8 @@ def draw
124128 calculate_xy_axes_origin
125129 configure_xy_axes
126130 configure_legends
127- # configure_plotting_data
131+ configure_plotting_data
128132 actually_draw
129- # @plots.each(&:draw)
130133 end
131134
132135 def scatter! *args , &block
@@ -212,103 +215,6 @@ def prepare_legend
212215 @legends = @plots . map ( &:create_legend )
213216 @legends . each { |l | l . draw }
214217 end
215-
216- # Calculates size of drawable area and generates normalized data.
217- #
218- # * line markers
219- # * legend
220- # * title
221- # * labels
222- # * X/Y offsets
223- def setup_drawing
224- calculate_spread
225- normalize # FIXME: maybe doesnt need to go here.
226- setup_graph_measurements
227- end
228-
229- # Calculate spread of the data.
230- def calculate_spread
231- @y_spread = @y_range [ 1 ] . to_f - @y_range [ 0 ] . to_f
232- unless @x_range [ 0 ] . nil? && @x_range [ 1 ] . nil?
233- @x_spread = @x_range [ 1 ] . to_f - @x_range [ 0 ] . to_f
234- @x_spread = @x_spread > 0 ? @x_spread : 1
235- end
236- end
237-
238- # Normalize data with values scaled between 0-100.
239- def normalize
240- @plots . each do |p |
241- p . normalize @x_spread , @y_spread
242- end
243- end
244-
245- ##
246- # Calculates size of drawable area, general font dimensions, etc.
247- # This is the most crucial part of the code and is based on geometry.
248- # It calcuates the measurments in pixels to figure out the positioning
249- # gap pixels of Legends, Labels and Titles from the picture edge.
250- def setup_graph_measurements
251- @marker_caps_height = @backend . caps_height @font , @marker_font_size
252- @title_caps_height = @geometry . hide_title || @title . nil? ? 0 :
253- @backend . caps_height ( @font , @title_font_size ) *
254- @title . lines . to_a . size
255- @legend_caps_height = @backend . caps_height @font , @legend_font_size
256-
257- # For now, the labels feature only focuses on the dot graph so it
258- # makes sense to only have this as an attribute for this kind of
259- # graph and not for others.
260- if @geometry . has_left_labels
261- text = @y_ticks . values . inject ( '' ) { |value , memo |
262- value . to_s . length > memo . to_s . length ? value : memo
263- }
264- longest_left_label_width = @backend . string_width (
265- @marker_font_size , text ) * 1.25
266- else
267- longest_left_label_width = @backend . string_width (
268- @font , @marker_font_size ,
269- label_string ( @y_range [ 1 ] . to_f , @geometry . increment ) )
270- end
271-
272- # Shift graph if left line numbers are hidden
273- line_number_width = @geometry . hide_line_numbers && !@geometry . has_left_labels ?
274- 0.0 : ( longest_left_label_width + LABEL_MARGIN * 2 )
275- # Pixel offset from the left edge of the plot
276- @graph_left = @geometry . left_margin +
277- line_number_width +
278- ( @geometry . y_axis_label . nil? ? 0.0 : @marker_caps_height + LABEL_MARGIN * 2 )
279- # Make space for half the width of the rightmost column label.
280- # last_label = @x_ticks.keys.max.to_i
281- # extra_room_for_long_label = last_label >= (@geometry.column_count - 1) &&
282- # @geometry.center_labels_over_point ?
283- # @backend.string_width(
284- # @font,
285- # @marker_font_size,
286- # @x_ticks[last_label]) / 2.0 : 0
287- extra_room_for_long_label = 0
288- # Margins
289- @graph_right_margin = @geometry . right_margin + extra_room_for_long_label
290- @graph_bottom_margin = @geometry . bottom_margin + @marker_caps_height + LABEL_MARGIN
291-
292- @graph_right = @geometry . raw_columns - @graph_right_margin
293- @graph_width = @geometry . raw_columns - @graph_left - @graph_right_margin
294-
295- # When @hide title, leave a title_margin space for aesthetics.
296- @graph_top = @geometry . legend_at_bottom ?
297- @geometry . top_margin :
298- ( @geometry . top_margin +
299- ( @geometry . hide_title ?
300- @title_margin :
301- @title_caps_height + @title_margin ) +
302- ( @legend_caps_height + @legend_margin ) )
303-
304- x_axis_label_height = @geometry . x_axis_label . nil? ? 0.0 :
305- @marker_caps_height + LABEL_MARGIN
306-
307- # The actual height of the graph inside the whole image in pixels.
308- @graph_bottom = @raw_rows - @graph_bottom_margin -
309- x_axis_label_height - @geometry . label_stagger_height
310- @graph_height = @graph_bottom - @graph_top
311- end
312218
313219 # Figure out the co-ordinates of the title text w.r.t Axes.
314220 def configure_title
@@ -343,12 +249,21 @@ def configure_legends
343249 self , abs_x : legend_box_ix , abs_y : legend_box_iy )
344250 end
345251
252+ # Make adjustments to the data that will be plotted. Maps the data
253+ # contained in the plot to actual pixel values.
254+ def configure_plotting_data
255+ @plots . each do |plot |
256+ plot . normalize
257+ end
258+ end
259+
346260 # Call the respective draw methods on each of the elements of this Axes.
347261 def actually_draw
348262 @x_axis . draw
349263 @y_axis . draw
350264 @title . draw
351265 @legend_box . draw
266+ @plots . each ( &:draw )
352267 end
353268
354269 # Return a formatted string representing a number value that should be
0 commit comments