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.



46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

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)



40
41
42
# File 'lib/ctioga2/graphics/root.rb', line 40

def count_legend_in_page
  @count_legend_in_page
end

#current_containerObject

The current Elements::Container of the object.



32
33
34
# File 'lib/ctioga2/graphics/root.rb', line 32

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.



36
37
38
# File 'lib/ctioga2/graphics/root.rb', line 36

def legend_area
  @legend_area
end

#page_sizeObject

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



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

def page_size
  @page_size
end

Instance Method Details

#current_legend_areaObject

Returns the legend_area in charge of the current container.



229
230
231
232
233
234
235
236
237
238
239
240
# 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_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…).



66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object

Draws this object onto an appropriate FigureMaker object.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# 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?Boolean

Whether we are drawing a top-level legend

Returns:

  • (Boolean)


223
224
225
226
# 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?Boolean

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

Returns:

  • (Boolean)


189
190
191
# File 'lib/ctioga2/graphics/root.rb', line 189

def empty?
  return @current_container.nil?
end

#enter_gradient(opts) ⇒ Object

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



178
179
180
181
182
183
184
185
# 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) ⇒ Object

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



167
168
169
170
171
172
173
174
# 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) ⇒ Object

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



79
80
81
82
83
84
85
# 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_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

#subplot(opts, box = nil) ⇒ Object

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 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_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