Class: CTioga::Label

Inherits:
Object
  • Object
show all
Includes:
Log, Tioga::FigureConstants
Defined in:
lib/CTioga/axes.rb

Overview

A class that describes the various attributes of a label, either for the X or Y axis, or even the plot title.

Direct Known Subclasses

TickLabels

Constant Summary collapse

Attributes =

The various attributes

[:alignment, :angle, :color, :justification,
:position, :scale, :shift, :side, :visible]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#identify, #init_logger, #logger, #logger_options, #spawn

Constructor Details

#initialize(which, label = nil, other_values = {}) ⇒ Label

Creates a label specification for the which axis. other_values is a hash that can be used to provide default values for the various Attributes.

which can be :xlabel, :ylabel or :title



378
379
380
381
382
383
384
# File 'lib/CTioga/axes.rb', line 378

def initialize(which, label = nil, other_values = {})
  @which = which
  @label = label
  for key,val in other_values
    self.send("#{key}=", val)
  end
end

Instance Attribute Details

#labelObject

The actual text to be displayed



371
372
373
# File 'lib/CTioga/axes.rb', line 371

def label
  @label
end

#whichObject

Which axis ?



368
369
370
# File 'lib/CTioga/axes.rb', line 368

def which
  @which
end

Instance Method Details

#extension(t, scale = 1) ⇒ Object

Guesses the extension of the label on the side of the plot. The output is only garanteed when called with t exactly in the context that will be used for the plot. An external scale factor can be given in the case we have more information about the actual overall scale. Of course, just multiplying the result by it does give the same thing.

The value returned is an array left,right, top, bottom containing only zeroes but for the place where the axis does extend.



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/CTioga/axes.rb', line 412

def extension(t, scale = 1)
  ret_val = [0,0,0,0]
  if @label
    ext = (@scale || tioga_value(t,:scale)) * 
      (1 + (@shift || tioga_value(t,:shift))) * 
    t.default_text_scale * t.default_font_size * scale
    # Set to 0 if label doesn't extend.
    ext = 0 if ext < 0
    case (@side || tioga_value(t,:side)) 
    when LEFT
      ret_val[0] = ext
    when BOTTOM
      ret_val[3] = ext
    when TOP
      ret_val[2] = ext
    when RIGHT
      ret_val[1] = ext
    end
  end
  debug "Axis #{self.inspect} extension #{ret_val.inspect}"
  return ret_val
end

#show(t) ⇒ Object

Shows the given label on the figure t



387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/CTioga/axes.rb', line 387

def show(t)
  # We don't do anything unless we have a label to display !
  return unless @label
  # We wrap the call in a context so we don't pollute subpictures
  t.context do 
    for attr in Attributes
      if instance_variables.include?("@#{attr}")
        val = instance_variable_get("@#{attr}")
        t.send("#{which}_#{attr}=", val)
      end
    end
    t.send("show_#{which}", @label) 
  end
end

#tioga_value(t, what) ⇒ Object

Returns the value of the what attribute corresponding to the value of which.



438
439
440
# File 'lib/CTioga/axes.rb', line 438

def tioga_value(t, what)
  return t.send("#{@which}_#{what}")
end