Class: CTioga2::Graphics::RootObject

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRootObject

Returns a new instance of RootObject.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ctioga2/graphics/root.rb', line 48

def initialize
  @current_container = nil

  @container_stack = []

  @legend_area = Legends::LegendArea.new

  @count_legend_in_page = false
  #     @count_legend_in_page = true

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

Instance Attribute Details

#count_legend_in_pageObject

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



42
43
44
# File 'lib/ctioga2/graphics/root.rb', line 42

def count_legend_in_page
  @count_legend_in_page
end

#current_containerObject

The current Elements::Container of the object.



34
35
36
# File 'lib/ctioga2/graphics/root.rb', line 34

def current_container
  @current_container
end

#legend_areaObject

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



38
39
40
# File 'lib/ctioga2/graphics/root.rb', line 38

def legend_area
  @legend_area
end

#page_sizeObject

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



45
46
47
# File 'lib/ctioga2/graphics/root.rb', line 45

def page_size
  @page_size
end

Instance Method Details

#current_legend_areaObject

Returns the legend_area in charge of the current container.



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/ctioga2/graphics/root.rb', line 213

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_plotObject

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…).



68
69
70
71
72
73
74
75
76
# File 'lib/ctioga2/graphics/root.rb', line 68

def current_plot
  if @current_container
    return @current_container
  else
    subplot = Elements::Subplot.new(nil, self, nil)
    enter_subobject(subplot)
    return subplot
  end
end

#draw_root_object(t) ⇒ Object

Draws this object onto an appropriate FigureMaker object.



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
# File 'lib/ctioga2/graphics/root.rb', line 178

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?Boolean

Whether we are drawing a top-level legend

Returns:

  • (Boolean)


207
208
209
210
# File 'lib/ctioga2/graphics/root.rb', line 207

def draw_top_level_legend?
  return (! top_level_container.legend_area) && 
    ( top_level_container.legend_storage.harvest_contents.size > 0)
end

#empty?Boolean

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

Returns:

  • (Boolean)


173
174
175
# File 'lib/ctioga2/graphics/root.rb', line 173

def empty?
  return @current_container.nil?
end

#enter_gradientObject

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



162
163
164
165
166
167
168
169
# File 'lib/ctioga2/graphics/root.rb', line 162

def enter_gradient
  if ! @current_container
    subplot
  end
  region = Elements::GradientRegion.new(@current_container, self)
  enter_subobject(region)
  return region
end

#enter_regionObject

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



151
152
153
154
155
156
157
158
# File 'lib/ctioga2/graphics/root.rb', line 151

def enter_region
  if ! @current_container
    subplot
  end
  region = Elements::Region.new(@current_container, self)
  enter_subobject(region)
  return region
end

#enter_subobject(new_object) ⇒ Object

Enters into a new Elements::Container, new_object.



79
80
81
82
83
84
85
# File 'lib/ctioga2/graphics/root.rb', line 79

def enter_subobject(new_object)
  if @current_container
    @current_container.add_element(new_object)
  end
  @current_container = new_object
  @container_stack << @current_container
end

#leave_subobjectObject

Leaves a subobject.



88
89
90
91
92
93
94
95
96
# 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) ⇒ Object

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



105
106
107
108
109
# 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) ⇒ Object

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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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

#subplotObject

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



140
141
142
143
144
145
146
147
# File 'lib/ctioga2/graphics/root.rb', line 140

def subplot()
  if ! @current_container
    enter_subobject(Elements::Container.new(nil, self))
  end
  subplot = Elements::Subplot.new(@current_container, self, nil)
  enter_subobject(subplot)
  return subplot
end

#top_level_containerObject

The only top-level container of the graph.



99
100
101
# File 'lib/ctioga2/graphics/root.rb', line 99

def top_level_container
  return @container_stack.first
end