Class: CTioga2::Graphics::Types::PlotLocation

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/graphics/types/location.rb

Overview

Location of an object (especially axes) in a plot, in terms of the side of the plot or the X and Y axis.

Constant Summary collapse

LocationToTiogaLocation =

Conversion between the #base_location attribute and the real constant used for Tioga

{
  :left => Tioga::FigureConstants::LEFT,
  :right => Tioga::FigureConstants::RIGHT,
  :bottom => Tioga::FigureConstants::BOTTOM,
  :top => Tioga::FigureConstants::TOP,
  :at_x_origin => Tioga::FigureConstants::AT_X_ORIGIN,
  :at_y_origin => Tioga::FigureConstants::AT_Y_ORIGIN
}
LocationVertical =

Horizontal or vertical

{
  :left => true,
  :right => true,
  :bottom => false,
  :top => false,
  :at_x_origin => true,
  :at_y_origin => false
}
LocationBaseMargins =
TODO:

won’t work for origins.

A few helper hashes to convert from sides to margins

{
  :left => [0,1,0,0],
  :right => [1,0,0,0],
  :bottom => [0,0,1,0],
  :top => [0,0,0,1]
}
LocationMarginMultiplier =

Multiply this by the frame dimension in the correct direction to get the frame margins.

{
  :left => [-1,0,0,0],
  :right => [0,-1,0,0],
  :bottom => [0,0,0,-1],
  :top => [0,0,-1,0]
}
LocationsReorientMargins =
{
  :left => [1,0,3,2],
  :right => [0,1,2,3],
  :top => [2,3,1,0],
  :bottom => [3,2,0,1]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, shift = nil) ⇒ PlotLocation

Creates a new PlotLocation object, either copying the one given as argument or from scratch specifying at least the base location.



92
93
94
95
96
97
98
99
100
# 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

Instance Attribute Details

#base_locationObject

TODO:

This will have to be extended to allow possibly

The position of the object, one of :left, :right, :top, :bottom, :at_y_origin or :at_x_origin.

arbitrary frame/figure placement.



79
80
81
# File 'lib/ctioga2/graphics/types/location.rb', line 79

def base_location
  @base_location
end

#shiftObject

TODO:

This is not currently implemented

The shift away from the position given by #base_location.

This will be a Dimension object.



86
87
88
# File 'lib/ctioga2/graphics/types/location.rb', line 86

def shift
  @shift
end

Class Method Details

.from_text(str) ⇒ Object

Creates a location from the given text

So far, no real parsing



199
200
201
202
203
204
205
206
207
208
209
210
211
# 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

Instance Method Details

#do_sub_frame(t, size) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# 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

#frame_margins_for_size(t, size) ⇒ Object

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.



163
164
165
166
167
168
169
170
171
172
# 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

#is_side?(which) ⇒ Boolean

Returns whether the location is on the given side.

Returns:

  • (Boolean)


141
142
143
# File 'lib/ctioga2/graphics/types/location.rb', line 141

def is_side?(which)
  return @base_location == which
end

#label_extra_space(t) ⇒ Object

Extra extension that should be reserved for a label on the given side based on simple heuristics. Value is returned in text height units.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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

#orientationObject

Returns the orientation away from the graph



114
115
116
117
118
119
120
# File 'lib/ctioga2/graphics/types/location.rb', line 114

def orientation
  if vertical?
    return :x
  else
    return :y
  end
end

#reorient_margins(close, away, aleft, aright) ⇒ Object

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.



151
152
153
154
155
156
# 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

#tioga_locationObject

Returns the tioga location (ie that suitable for sending to show_axis for instance)



104
105
106
# File 'lib/ctioga2/graphics/types/location.rb', line 104

def tioga_location
  return LocationToTiogaLocation[@base_location]
end

#vertical?Boolean

Whether the given location is vertical or horizontal

Returns:

  • (Boolean)


109
110
111
# File 'lib/ctioga2/graphics/types/location.rb', line 109

def vertical?
  return LocationVertical[@base_location]
end