class CTioga2::Graphics::RootObject

The root object of the plot. The PlotMaker has one object like that. It is the real object drawing the plot.

Attributes

count_legend_in_page[RW]

Whether top-level legends are part of the “real size” of the graph or outside the graph (easier to align anything)

current_container[RW]

The current Elements::Container of the object.

legend_area[RW]

The top-level Legends::LegendArea. This one gets necessarily displayed on one of the sides of the graph.

page_size[RW]

The page size of the graph, a [width,height] array.

Public Class Methods

new() click to toggle source
# File lib/ctioga2/graphics/root.rb, line 46
def initialize
  @current_container = nil

  @container_stack = []

  @legend_area = Legends::LegendArea.new(:right, nil, {'class' => 'root'})

  @count_legend_in_page = false
  #     @count_legend_in_page = true

  # Page size:
  set_page_size("12cmx12cm")  # Same as old ctioga
end

Public Instance Methods

current_legend_area() click to toggle source

Returns the legend_area in charge of the current container.

# File lib/ctioga2/graphics/root.rb, line 229
def current_legend_area
  area = nil
  for el in @container_stack
    if el.respond_to?(:legend_area) and el.legend_area
      area = el.legend_area
    end
  end
  if ! area
    area = @legend_area
  end
  return area
end
current_plot() click to toggle source

Returns the current Elements::Container, or create an Elements::Subplot if there isn't.

This function should be used by all functions that add Elements::TiogaElement to plots (or modify plot's data, such as title, axes…).

# File lib/ctioga2/graphics/root.rb, line 66
def current_plot
  if @current_container
    return @current_container
  else
    subplot = Elements::Subplot.new(nil, self, 
                                    {'id' => 'root' })
    enter_subobject(subplot)
    return subplot
  end
end
draw_root_object(t) click to toggle source

Draws this object onto an appropriate FigureMaker object.

# File lib/ctioga2/graphics/root.rb, line 194
def draw_root_object(t)
  setup_page(t)
  if top_level_container
    
    plot_margins, legend_margins =  
      if draw_top_level_legend?
        @legend_area.partition_frame(t, top_level_container)
      else
        [[0, 0, 0, 0], nil]
      end

    t.context do 
      t.set_subframe(plot_margins)
      top_level_container.do(t)
    end

    # Draw the legend only when applicable.
    if legend_margins
      t.context do 
        t.set_subframe(legend_margins)
        @legend_area.display_legend(t, top_level_container)
      end
    end
  else
    raise "The root object should not be drawn empty ?"
  end
end
draw_top_level_legend?() click to toggle source

Whether we are drawing a top-level legend

# File lib/ctioga2/graphics/root.rb, line 223
def draw_top_level_legend?
  return (! top_level_container.legend_area) && 
    ( top_level_container.legend_storage.harvest_contents.size > 0)
end
empty?() click to toggle source

Returns true if not a single drawable object has been pushed unto the RootObject yet.

# File lib/ctioga2/graphics/root.rb, line 189
def empty?
  return @current_container.nil?
end
enter_gradient(opts) click to toggle source

This function is the companion of subplot, but for GradientRegion objects. Returns the newly created GradientRegion

# File lib/ctioga2/graphics/root.rb, line 178
def enter_gradient(opts)
  if ! @current_container
    subplot({})
  end
  region = Elements::GradientRegion.new(@current_container, self, opts)
  enter_subobject(region)
  return region
end
enter_region(opts) click to toggle source

This function is the companion of subplot, but for Region objects. Returns the newly created Region.

# File lib/ctioga2/graphics/root.rb, line 167
def enter_region(opts)
  if ! @current_container
    subplot({})
  end
  region = Elements::Region.new(@current_container, self, opts)
  enter_subobject(region)
  return region
end
enter_subobject(new_object, add = true) click to toggle source

Enters into a new Elements::Container, new_object, and adds it to the current container, unless add is false.

# File lib/ctioga2/graphics/root.rb, line 79
def enter_subobject(new_object, add = true)
  if @current_container && add
    @current_container.add_element(new_object)
  end
  @current_container = new_object
  @container_stack << @current_container
end
leave_subobject() click to toggle source

Leaves a subobject.

# File lib/ctioga2/graphics/root.rb, line 88
def leave_subobject
  if @container_stack.size == 1
    raise "Trying to leave top-level object"
  end
  if @container_stack.pop != @current_container
    raise "We have a serious problem here"
  end
  @current_container = @container_stack.last
end
set_page_size(size) click to toggle source

Sets the page of the object, from a pure text object, such as “12cmx12cm”

# File lib/ctioga2/graphics/root.rb, line 105
def set_page_size(size)
  @page_size = size.split(/\s*x\s*/).collect {|s| 
    Tioga::Utils::tex_dimension_to_bp(s)
  }
end
setup_page(t) click to toggle source

Sets up the page width and other parameters for the given FigureMaker object. Must be within a figure object, so that potential modifications to the page size due to text objects (legends) can be taken into account.

# File lib/ctioga2/graphics/root.rb, line 116
def setup_page(t)
  if @count_legend_in_page or ! draw_top_level_legend?
    effective_size = @page_size
  else
    effective_size = @legend_area.
      enlarged_page_size(t,  top_level_container, *@page_size)
  end
  t.page_setup(*effective_size)
  t.set_frame_sides(0,1,1,0) 

  # Setting label and title scale to 1
  t.title_scale = 1
  t.xlabel_scale = 1
  t.ylabel_scale = 1
  # \todo I think this is mostly useless. Check.
end
subplot(opts, box = nil) click to toggle source

Creates a subplot of the current plot. If @current_container is null, create it as a Elements::Container: this will make it easy to create complex graphs (no need to disable axes and other kinds of stuff on the main plot).

For the sake of convenience, returns the newly created Elements::Subplot

# File lib/ctioga2/graphics/root.rb, line 140
def subplot(opts, box = nil)
  if ! @current_container
    enter_subobject(Elements::Container.new(nil, self, {}))
  end

  o = opts.dup
  o["class"] ||= []

  # We add automatic classes from the box
  #
  # Most of the time empty, for sure.
  if box
    o["class"] += box.classes
  end

  subplot = Elements::Subplot.new(@current_container, self, o)

  if box
    subplot.subframe = box
  end

  enter_subobject(subplot)
  return subplot
end
top_level_container() click to toggle source

The only top-level container of the graph.

# File lib/ctioga2/graphics/root.rb, line 99
def top_level_container
  return @container_stack.first
end