class CTioga2::Graphics::Types::PlotLocation
Location of an object (especially axes) in a plot, in terms of the side of the plot or the X and Y axis.
Constants
- LocationBaseMargins
A few helper hashes to convert from sides to margins @todo won't work for origins.
- LocationMarginMultiplier
Multiply this by the frame dimension in the correct direction to get the frame margins.
- LocationToTiogaLocation
Conversion between the
base_location
attribute and the real constant used for Tioga- LocationVertical
Horizontal or vertical
- LocationsReorientMargins
Attributes
The position of the object, one of :left, :right, :top, :bottom, :at_y_origin or :at_x_origin.
@todo This will have to be extended to allow possibly arbitrary frame/figure placement.
The shift away from the position given by base_location
.
This will be a Dimension
object.
@todo This is not currently implemented
Public Class Methods
Creates a location from the given text
So far, no real parsing
# File lib/ctioga2/graphics/types/location.rb, line 199 def self.from_text(str) loc = nil case str when /^\s*(left|right|top|bottom|at_y_origin|at_x_origin)\s*$/i loc = $1.downcase.to_sym when /^s*(x|y)0\s*$/i loc = "at_#{$1}_origin".downcase.to_sym end if ! loc raise "Unkown spec for axis location: '#{str}'" end return PlotLocation.new(loc) end
Creates a new PlotLocation
object, either copying the one given as argument or from scratch specifying at least the base location.
# File lib/ctioga2/graphics/types/location.rb, line 92 def initialize(location, shift = nil) if location.respond_to? :shift @base_location = location.base_location @shift = shift || location.shift else @base_location = location @shift = shift end end
Public Instance Methods
# File lib/ctioga2/graphics/types/location.rb, line 174 def do_sub_frame(t, size) margins = frame_margins_for_size(t, size) ## @todo This is should integrate some common class. left = t.convert_frame_to_page_x(margins[0]) right = t.convert_frame_to_page_x(1 - margins[1]) top = t.convert_frame_to_page_y(1 - margins[2]) bottom = t.convert_frame_to_page_y(margins[3]) # Ensure that we don't have coords outside of the page range # because of rounding problems: left = 0.0 if left < 0 bottom = 0.0 if bottom < 0 right = 1.0 if right > 1 top = 1.0 if top > 1 t.context do t.set_frame_sides(left, right, top, bottom) yield end end
Returns the margins argument suitable for sending to set_subframe to paint within the region defined by the given size at the given position.
size is a Dimension
object.
# File lib/ctioga2/graphics/types/location.rb, line 163 def frame_margins_for_size(t, size) margins = Dobjects::Dvector[*LocationBaseMargins[@base_location]] ## @todo handle the case of at Y and at X dim = size.to_frame(t, orientation) add = Dobjects::Dvector[*LocationMarginMultiplier[@base_location]] add.mul!(dim) margins += add return margins end
Returns whether the location is on the given side.
# File lib/ctioga2/graphics/types/location.rb, line 141 def is_side?(which) return @base_location == which end
Extra extension that should be reserved for a label on the given side based on simple heuristics. Value is returned in text height units.
# File lib/ctioga2/graphics/types/location.rb, line 125 def label_extra_space(t) case @base_location when :bottom, :right extra = 0.5 # To account for baseline ? when :top, :left extra = 1 else # We take the safe side ! extra = 1 end if @shift ## @todo Here add the shift end return extra end
Returns the orientation away from the graph
# File lib/ctioga2/graphics/types/location.rb, line 114 def orientation if vertical? return :x else return :y end end
Takes a set of margins, expressed in relative terms, ie
-
close (the margins on the side next to the graph),
-
away (on the other side),
-
aleft (on the left going away from the graph) and
-
aright (on the right going away from the graph)
into a left,right,top,bottom suitable for standards margins calls.
# File lib/ctioga2/graphics/types/location.rb, line 151 def reorient_margins(close, away, aleft, aright) a = [close, away, aleft, aright] return LocationsReorientMargins[@base_location].map do |i| a[i] end end
Returns the tioga location (ie that suitable for sending to show_axis for instance)
# File lib/ctioga2/graphics/types/location.rb, line 104 def tioga_location return LocationToTiogaLocation[@base_location] end
Whether the given location is vertical or horizontal
# File lib/ctioga2/graphics/types/location.rb, line 109 def vertical? return LocationVertical[@base_location] end