Class: CTioga2::Graphics::Styles::AxisStyle

Inherits:
BasicStyle
  • Object
show all
Includes:
Tioga::FigureConstants
Defined in:
lib/ctioga2/graphics/styles/axes.rb

Overview

The style of an axis or an egde of the plot. Unlike tioga, ctioga2 does not make any difference.

Direct Known Subclasses

MapAxisStyle

Constant Summary collapse

@@text_size_index =
TODO:

Add a ‘slave’ attribute: axes would be created as

slaves to another one until:

  • a curve is added to it

  • a transform is set

  • ticks are set

0

Constants inherited from BasicStyle

BasicStyle::AllStyles, BasicStyle::OldAttrAccessor

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicStyle

alias_for, aliases, attr_accessor, attribute_type, attribute_types, attributes, convert_string_hash, defined_aliases, deprecated_attribute, from_hash, inherited, #instance_variable_defined?, normalize_hash, normalize_in, normalize_out, options_hash, #set_from_hash, sub_style, sub_styles, #to_hash, typed_attribute, #update_from_other

Constructor Details

#initialize(location = nil, decoration = nil, label = nil) ⇒ AxisStyle

Creates a new AxisStyle object at the given location with the given style.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ctioga2/graphics/styles/axes.rb', line 88

def initialize(location = nil, decoration = nil, label = nil)
  @location = Types::PlotLocation.new(location)
  @decoration = decoration

  @tick_label_style = FullTextStyle.new
  @tick_label_style.scale = Types::Dimension.new(:dy, 0.8)

  @axis_label = TextLabel.new(label)
  @log = false
  @ticks_side = {}
  @ticks = AxisTicks.new

  @index = @@text_size_index
  @@text_size_index += 1
end

Class Method Details

.current_axis_style(plotmaker, spec) ⇒ Object

Returns the AxisStyle object corresponding to the named axis in the current plot.



259
260
261
262
# File 'lib/ctioga2/graphics/styles/axes.rb', line 259

def self.current_axis_style(plotmaker, spec)
  return PlotStyle.current_plot_style(plotmaker).
    get_axis_style(spec)
end

Instance Method Details

#draw_axis(t, watcher = nil) ⇒ Object

Draws the axis within the current plot. Boundaries are the current plot boundaries. Also draw the #axis_label, if there is one.

todo

  • the offset mechanism, to place the axis away from the place where it should be…

  • non-linear axes (or linear, for that matter, but with a transformation)

watcher is a TextSizeWatcher object.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ctioga2/graphics/styles/axes.rb', line 115

def draw_axis(t, watcher = nil)
  spec = get_axis_specification(t)

  info = t.axis_information(spec)

  # Merge in the specs
  spec.merge!(@ticks.ticks_specs(t, info, @transform))
  
  # Add tick label style:
  spec.merge!(@tick_label_style.hash_for_tioga(t))

  # Direct copy of attributes 
  for key in %w(stroke_color major_tick_length major_tick_width
minor_tick_length minor_tick_width)
    val = self.send(key.to_sym)
    if val
      spec[key] = val
    end
  end

  spec.update(@ticks_side)

  # We don't allow Tioga to draw tick labels by itself
  type = spec['type']
  spec['type'] = case spec['type']
                 when Tioga::FigureConstants::AXIS_WITH_MAJOR_TICKS_AND_NUMERIC_LABELS
                   Tioga::FigureConstants::AXIS_WITH_MAJOR_TICKS_ONLY
                 when Tioga::FigureConstants::AXIS_WITH_TICKS_AND_NUMERIC_LABELS
                   Tioga::FigureConstants::AXIS_WITH_TICKS_ONLY
                 else
                   spec['type']
                 end
  t.show_axis(spec)
  # Now, we draw axis ticks
  if (type == Tioga::FigureConstants::AXIS_WITH_MAJOR_TICKS_AND_NUMERIC_LABELS) || (type == Tioga::FigureConstants::AXIS_WITH_TICKS_AND_NUMERIC_LABELS)

    fnc = info['vertical'] ? :convert_figure_to_frame_y : :convert_figure_to_frame_x
    stl = @tick_label_style.dup
    
    
    stl.shift ||= Types::Dimension.new(:dy, info['shift'])


    # @todo integrate to the 
    shift_def = ( 
                 @location.base_location == :bottom ||
                 @location.base_location == :at_y_origin ||
                 @location.base_location == :right 
                 ) ? 0.3 : 0.4
    stl.shift = Types::Dimension.new(:dy, stl.shift.to_dy(t) + shift_def)

    stl.valign ||= ( 
                    @location.base_location == :bottom ||
                    @location.base_location == :at_y_origin ||
                    @location.base_location == :right 
                    ) ? Tioga::FigureConstants::ALIGNED_AT_TOP : Tioga::FigureConstants::ALIGNED_AT_BOTTOM
    
    spec['labels'].size.times do |i|
      pos = spec['major_ticks'][i]
      label = spec['labels'][i]
      pos = t.send(fnc, pos)
      
      next unless label


      nm = "axis-tick#{@index}-#{i}"

      stl.draw_text(t, label, spec['location'], 
                    [:pos, pos], nm)
      if watcher
        watcher.watch(nm)
      end
    end
  end

  @axis_label.loc = @location
  default = vertical? ? 'ylabel' : 'xlabel'
  nm = "axis-label#{@index}"

  # stl = @axis_label.dup
  # # Default to aligning the label where it counts.
  # stl.valign ||= ( 
  #                 @location.base_location == :bottom ||
  #                 @location.base_location == :at_y_origin ||
  #                 @location.base_location == :right 
  #                 ) ? Tioga::FigureConstants::ALIGNED_AT_MIDHEIGHT : Tioga::FigureConstants::ALIGNED_AT_BOTTOM

  @axis_label.draw(t, default, nm)
  if watcher
    watcher.watch(nm)
  end
end

#draw_background_lines(t) ⇒ Object

Draw the axis background lines:



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/ctioga2/graphics/styles/axes.rb', line 229

def draw_background_lines(t)
  if @background_lines
    # First, getting major ticks location from tioga
    info = t.axis_information(get_axis_specification(t))
    
    tick_info = @ticks.ticks_specs(t, info, @transform)

    if info['vertical']
      x0 = t.bounds_left
      x1 = t.bounds_right
    else
      y0 = t.bounds_bottom
      y1 = t.bounds_top
    end
    t.context do
      @background_lines.set_stroke_style(t)
      values = tick_info['major_ticks']
      for val in values
        if info['vertical']
          t.stroke_line(x0, val, x1, val)
        else
          t.stroke_line(val, y0, val, y1)
        end
      end
    end
  end
end

#extension(t, style = nil) ⇒ Object

Returns the extension of the axis (including tick labels and labels if applicable) perpendicular to itself, in units of text height (at scale = current text scale when drawing axes).

style is a PlotStyle object containing the style information for the target plot.

todo handle offset axes when that is implemented.



294
295
296
# File 'lib/ctioga2/graphics/styles/axes.rb', line 294

def extension(t, style = nil)
  return labels_only_extension(t, style)
end

#labels_only_extension(t, style = nil) ⇒ Object

Returns the part of the #extension only due to the labels (ticks and standard label).

For now, it returns the same value as #extension, but that might change



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ctioga2/graphics/styles/axes.rb', line 269

def labels_only_extension(t, style = nil)
  ticks_shift, ticks_scale = *get_ticks_parameters(t)
  default =  vertical? ? 'ylabel' : 'xlabel'
  le = @axis_label.label_extension(t, default, @location)

  case @decoration
  when AXIS_WITH_MAJOR_TICKS_AND_NUMERIC_LABELS,
    AXIS_WITH_TICKS_AND_NUMERIC_LABELS
    te = ticks_shift * ticks_scale
  else
    te = 0
  end
  return Dobjects::Dvector[le,te].max * 
    (style ? style.text_scale || 1 : 1)
end

#set_bounds_for_axis(t, range = nil) ⇒ Object

Sets the current boundaries of the t object to the range SimpleRange object for the direction handled by the AxisStyle, without touching the rest.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ctioga2/graphics/styles/axes.rb', line 211

def set_bounds_for_axis(t, range = nil)
  if ! range
    return
  end
  l,r,top,b = t.bounds_left, t.bounds_right, 
  t.bounds_top, t.bounds_bottom

  if self.vertical?
    b = range.first
    top = range.last
  else
    l = range.first
    r = range.last
  end
  t.set_bounds([l,r,top,b])
end