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.



94
95
96
97
98
99
100
101
102
# File 'lib/ctioga2/graphics/types/location.rb', line 94

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.



81
82
83
# File 'lib/ctioga2/graphics/types/location.rb', line 81

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.



88
89
90
# File 'lib/ctioga2/graphics/types/location.rb', line 88

def shift
  @shift
end

Class Method Details

.from_text(str) ⇒ Object

Creates a location from the given text

So far, no real parsing



201
202
203
204
# File 'lib/ctioga2/graphics/types/location.rb', line 201

def self.from_text(str)
  str.gsub!(/-/,"_")
  return PlotLocation.new(str.to_sym)
end

Instance Method Details

#do_sub_frame(t, size) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ctioga2/graphics/types/location.rb', line 176

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.



165
166
167
168
169
170
171
172
173
174
# File 'lib/ctioga2/graphics/types/location.rb', line 165

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)


143
144
145
# File 'lib/ctioga2/graphics/types/location.rb', line 143

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.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ctioga2/graphics/types/location.rb', line 127

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



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

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.



153
154
155
156
157
158
# File 'lib/ctioga2/graphics/types/location.rb', line 153

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)



106
107
108
# File 'lib/ctioga2/graphics/types/location.rb', line 106

def tioga_location
  return LocationToTiogaLocation[@base_location]
end

#vertical?Boolean

Whether the given location is vertical or horizontal

Returns:

  • (Boolean)


111
112
113
# File 'lib/ctioga2/graphics/types/location.rb', line 111

def vertical?
  return LocationVertical[@base_location]
end