Class: CTioga2::Graphics::Styles::PlotStyle

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

Overview

The style of a Elements::Subplot object.

TODO: it should hold

  • labels

  • axes and edges (in a clean way !)

  • ticks

  • background (uniform fill + watermark if applicable + possibly a picture .?)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlotStyle

Returns a new instance of PlotStyle.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ctioga2/graphics/styles/plot.rb', line 73

def initialize
  # Default style for the plots.
  @axes = {}
  @axes[:left] = AxisStyle.new(:left, 
                               AXIS_WITH_TICKS_AND_NUMERIC_LABELS,
                               '$y$')
  @axes[:bottom] = AxisStyle.new(:bottom, 
                                 AXIS_WITH_TICKS_AND_NUMERIC_LABELS,
                                 '$x$')

  @axes[:right] = AxisStyle.new(:right, AXIS_WITH_TICKS_ONLY)
  @axes[:top] = AxisStyle.new(:top, AXIS_WITH_TICKS_ONLY)

  @xaxis_location = :bottom
  @yaxis_location = :left

  @title = TextLabel.new
  @title.loc = :top

  @plot_margin = nil

  @transforms = CoordinateTransforms.new

  @background = BackgroundStyle.new

  # A padding of 4bp ? Why ?? Why not ?
  @padding = Types::Dimension.new(:bp, 4)
end

Instance Attribute Details

#axesObject

The various sides of the plot. A hash location -> AxisStyle.



41
42
43
# File 'lib/ctioga2/graphics/styles/plot.rb', line 41

def axes
  @axes
end

#backgroundObject

Style of the background of the plot



59
60
61
# File 'lib/ctioga2/graphics/styles/plot.rb', line 59

def background
  @background
end

#lines_scaleObject

Scale of the lines of the plot. The plot is wrapped in a t.rescale_lines call.



63
64
65
# File 'lib/ctioga2/graphics/styles/plot.rb', line 63

def lines_scale
  @lines_scale
end

#paddingObject

A padding around the box when automatic spacing is in auto mode. A Dimension.



71
72
73
# File 'lib/ctioga2/graphics/styles/plot.rb', line 71

def padding
  @padding
end

#plot_marginObject

A margin to be left around the data points



53
54
55
# File 'lib/ctioga2/graphics/styles/plot.rb', line 53

def plot_margin
  @plot_margin
end

#text_scaleObject

Scale of the text of the plot. The plot is wrapped in a t.rescale_text call.



67
68
69
# File 'lib/ctioga2/graphics/styles/plot.rb', line 67

def text_scale
  @text_scale
end

#titleObject

The title of the plot



50
51
52
# File 'lib/ctioga2/graphics/styles/plot.rb', line 50

def title
  @title
end

#transformsObject

Coordinate tranforms



56
57
58
# File 'lib/ctioga2/graphics/styles/plot.rb', line 56

def transforms
  @transforms
end

#xaxis_locationObject

The default location of the X axis (well, mainly, the X label)



44
45
46
# File 'lib/ctioga2/graphics/styles/plot.rb', line 44

def xaxis_location
  @xaxis_location
end

#yaxis_locationObject

The default location of the Y axis (well, mainly, the Y label)



47
48
49
# File 'lib/ctioga2/graphics/styles/plot.rb', line 47

def yaxis_location
  @yaxis_location
end

Class Method Details

.current_plot_style(plotmaker) ⇒ Object

Returns the PlotStyle object of the current plot



210
211
212
# File 'lib/ctioga2/graphics/styles/plot.rb', line 210

def self.current_plot_style(plotmaker)
  return plotmaker.root_object.current_plot.style
end

Instance Method Details

#deep_copyObject

Returns a deep copy of self, with all references stripped.



205
206
207
# File 'lib/ctioga2/graphics/styles/plot.rb', line 205

def deep_copy
  return Marshal.load(Marshal.dump(self))
end

#draw_all_axes(t) ⇒ Object

Draws all axes for the plot.



176
177
178
179
180
181
182
# File 'lib/ctioga2/graphics/styles/plot.rb', line 176

def draw_all_axes(t)
  for which, axis in @axes
    axis.draw_axis(t)
  end
  # We draw the title last
  title.draw(t, 'title')
end

#draw_all_background_lines(t) ⇒ Object

Draws all axes background lines for the plot.



185
186
187
188
189
# File 'lib/ctioga2/graphics/styles/plot.rb', line 185

def draw_all_background_lines(t)
  for which, axis in @axes
    axis.draw_background_lines(t)
  end
end

#estimate_margins(t) ⇒ Object

Estimate the margins of the plot whose style this object controls. These margins are used when the plot margins are in automatic mode.

Returns a Types::MarginsBox



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ctioga2/graphics/styles/plot.rb', line 219

def estimate_margins(t)
  margins = [:left, :right, :top, :bottom].map do |side|
    ext = @axes[side].extension(t,self)
    if side == @title.loc
      ext2 = @title.label_extension(t, 'title', side) * 
        (@text_scale || 1)
      if ext < ext2
        ext = ext2
      end
    end
    Types::Dimension.new(:dy, ext)
  end

  # TODO: add the plot title !
  box = Types::MarginsBox.new(*margins)
  if @padding
    for dim in box.margins
      dim.replace_if_bigger(t, @padding)
    end
  end
  return box
end

#get_axis_style(name) ⇒ Object

Returns the AxisStyle corresponding to the named axis. name can be:

  • one of the named elements of axes (ie, by default: top, left, right, bottom). All names are stripped from spaces around, and downcased (see #clean_axis_name).

  • x(axis)?/y(axis)?, which returns the default object for the given location



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ctioga2/graphics/styles/plot.rb', line 128

def get_axis_style(name)
  if name =~ /^\s*([xy])(?:axis)?\s*$/i
    return @axes[self.send("#{$1.downcase}axis_location")]
  else
    style = @axes[clean_axis_name(name)]
    if ! style
      raise "Unkown named axis: '#{name}'"
    else
      return style
    end
  end 
end

#get_label_style(location) ⇒ Object

Returns a BaseTextStyle or similar for the given location. The location is of the form:

axis_name(_(ticks?|label))

or

title

If neither label nor ticks is specified in the first form, ticks are implied.



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ctioga2/graphics/styles/plot.rb', line 149

def get_label_style(location)
  if location =~ /^\s*title\s*$/
    return @title
  end
  location =~ /^\s*(.*?)(?:_(ticks?|label))?\s*$/i
  which = $2
  axis = get_axis_style($1)
  if which =~ /label/
    return axis.axis_label
  else
    return axis.tick_label_style
  end
end

#set_label_style(which, hash, text = nil) ⇒ Object

Sets the style of the given label. Sets the text as well, if text is not nil



165
166
167
168
169
170
171
172
# File 'lib/ctioga2/graphics/styles/plot.rb', line 165

def set_label_style(which, hash, text = nil)
  style = get_label_style(which)
  hash = hash.merge({'text' => text}) unless text.nil?
  if hash.key?('text') and ! style.is_a?(TextLabel)
    CTioga2::Log::warn("Text property of label #{which} was set, but this has no meaning: tick labels can't be set this way. Did you mean to use \"#{which}_label\"" + " instead ?")
  end
  style.set_from_hash(hash)
end

#set_log_scale(which, val) ⇒ Object

Whether to use log scale for the given axis.

Now the question is: how should that affect user-defined axes ? It should not.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ctioga2/graphics/styles/plot.rb', line 106

def set_log_scale(which, val)
  case which
  when :x
    @axes[:top].log = val
    @axes[:bottom].log = val
    @transforms.x_log = val
  when :y
    @axes[:left].log = val
    @axes[:right].log = val
    @transforms.y_log = val
  else
    raise "Unknown axis: #{which.inspect}"
  end
end

#setup_figure_maker(t) ⇒ Object

Sets up the FigureMaker object for the plot. To be called just after the outermost context call for the concerned plot.



194
195
196
197
198
199
200
201
# File 'lib/ctioga2/graphics/styles/plot.rb', line 194

def setup_figure_maker(t)
  if @lines_scale
    t.rescale_lines(@lines_scale)
  end
  if @text_scale
    t.rescale_text(@text_scale)
  end
end