Module: CTioga::Axes

Included in:
PlotMaker
Defined in:
lib/CTioga/axes.rb

Overview

A module to be included by the main PlotMaker instance. It deals with various aspects of axes manipulations.

Constant Summary collapse

LabelSpecification =
{
  /^x(label)?$/i => :xlabel,
  /^y(label)?$/i => :ylabel,
  /^t(itle)?$/i => :title,
  /^xticks?$/i => :xticks,
  /^yticks?$/i => :yticks,
}
AxisSpecification =
{
  /^x$/i => 'xaxis',
  /^y?$/i => 'yaxis',
}
LabelSide =
{
  /bottom/i => Tioga::FigureConstants::BOTTOM,
  /top/i => Tioga::FigureConstants::TOP,
  /left/i => Tioga::FigureConstants::LEFT,
  /right/i => Tioga::FigureConstants::RIGHT,
}
Alignment =
{
  /B/ => Tioga::FigureConstants::ALIGNED_AT_BASELINE,
  /baseline/i => Tioga::FigureConstants::ALIGNED_AT_BASELINE,
  /b/ => Tioga::FigureConstants::ALIGNED_AT_BOTTOM,
  /bottom/i => Tioga::FigureConstants::ALIGNED_AT_BOTTOM,
  /t(op)?/i => Tioga::FigureConstants::ALIGNED_AT_TOP,
}
Justification =
{
  /c(enter)?/i => Tioga::FigureConstants::CENTERED,
  /l(eft)?/i => Tioga::FigureConstants::LEFT_JUSTIFIED,
  /r(ight)?/i => Tioga::FigureConstants::RIGHT_JUSTIFIED,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#decimal_separatorObject

Decimal separator



74
75
76
# File 'lib/CTioga/axes.rb', line 74

def decimal_separator
  @decimal_separator
end

#x_factorObject

Scaling of data axes



70
71
72
# File 'lib/CTioga/axes.rb', line 70

def x_factor
  @x_factor
end

#x_logObject

Whether to have X or Y log axes



68
69
70
# File 'lib/CTioga/axes.rb', line 68

def x_log
  @x_log
end

#x_offsetObject

Offsets



72
73
74
# File 'lib/CTioga/axes.rb', line 72

def x_offset
  @x_offset
end

#y_factorObject

Scaling of data axes



70
71
72
# File 'lib/CTioga/axes.rb', line 70

def y_factor
  @y_factor
end

#y_logObject

Whether to have X or Y log axes



68
69
70
# File 'lib/CTioga/axes.rb', line 68

def y_log
  @y_log
end

#y_offsetObject

Offsets



72
73
74
# File 'lib/CTioga/axes.rb', line 72

def y_offset
  @y_offset
end

Instance Method Details

#axes_options(parser) ⇒ Object

Prepare the option parser for axes options



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/CTioga/axes.rb', line 129

def axes_options(parser)

  parser.separator "\nVarious axes manipulations:"
  parser.on("--xfact FACT",
            "Multiply all x values by FACT") do |f|
    @x_factor = safe_float(f)
  end
  parser.on("--yfact FACT",
            "Multiply all y values by FACT") do |f|
    @y_factor = safe_float(f)
  end
  parser.on("--xoffset OFFSET",
            "Add OFFSET to all X values (after multiplication)") do |f|
    @x_offset = safe_float(f)
  end
  parser.on("--yoffset OFFSET",
            "Add OFFSET to all X values (after multiplication)") do |f|
    @y_offset = safe_float(f)
  end

  parser.on("--[no-]xlog",
            "Uses logarithmic scale for X axis") do |v|
    @x_log = v
    add_elem_funcall(:xaxis_log_values=, v)
  end
  parser.on("--[no-]ylog",
            "Uses logarithmic scale for Y axis") do |v|
    @y_log = v
    add_elem_funcall(:yaxis_log_values=, v)
  end

  parser.on("--reset-transformations",
            "Reset axes transformations") do |v|
    @y_log = false
    @x_log = false
    # TODO: remove that when this is move to PlotStyle:
    # it should be setup automatically by the Container
    # at the beginning of a plot...
    add_elem_funcall(:xaxis_log_values=, false)
    add_elem_funcall(:yaxis_log_values=, false)
    @x_factor = nil
    @y_factor = nil
    @x_offset = nil
    @y_offset = nil
  end

  

  parser.on("--comma",
            "Uses a comma for the decimal separator") do 
    @decimal_separator = ','
  end
  parser.on("--decimal SEP",
            "Uses SEP for the decimal separator") do |s|
    @decimal_separator = s
  end

  parser.separator "\nLabels and titles"
  parser.on("-x","--[no-]xlabel [LABEL]",
            "Label of the x axis") do |l|
    current_object.plot_style.xlabel.label = l
  end
  parser.on("-y","--[no-]ylabel [LABEL]",
             "Label of the y axis") do |l|
    current_object.plot_style.ylabel.label = l
  end
  parser.on('-t', "--[no-]title [TITLE]",
             "Sets the title of the plot") do |l|
    current_object.plot_style.title.label = l
  end
  parser.on("--side WHAT ALIGN",
            "Sets the side for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle)") do |w|
    run_with_label(w) do |label|
      a = Utils::interpret_arg(@args.shift, LabelSide) {false}
      label.side = a if a
    end
  end

  parser.on("--lcolor WHAT COLOR",
            "Sets the color for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle)") do |w|
    run_with_label(w) do |label|
      a = CTioga.get_tioga_color(@args.shift)
      label.color = a if a
    end
  end

  parser.on("--position WHAT WHERE",
            "Sets the position for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle)",
            "and WHERE a number between 0 and 1, 0.5 = centered") do |w|
    run_with_label(w) do |label|
      a = safe_float(@args.shift)
      label.position = a if a
    end
  end

  parser.on("--angle WHAT ANGLE",
            "Sets the angle for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle).") do |w|
    run_with_label(w) do |label|
      a = safe_float(@args.shift)
      label.angle = a if a
    end
  end

  parser.on("--scale WHAT SCALE",
            "Sets the scale for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle).") do |w|
    run_with_label(w) do |label|
      a = safe_float(@args.shift)
      label.scale = a if a
    end
  end

  parser.on("--shift WHAT SHIFT",
            "Sets the shift for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle).",
            "The shift is the distance of the label from the plot") do |w|
    run_with_label(w) do |label|
      a = safe_float(@args.shift)
      label.shift = a if a
    end
  end

  parser.on("--align WHAT ALIGN",
            "Sets the 'vertical' alignment for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle).") do |w|
    run_with_label(w) do |label|
      a = Utils::interpret_arg(@args.shift, Alignment) {false}
      label.alignment = a if a
    end
  end

  parser.on("--just WHAT JUST",
            "Sets the 'horizontal' alignment for the WHAT label, ",
            "where WHAT = x(label),y(label) or t(itle).") do |w|
    run_with_label(w) do |label|
      a = Utils::interpret_arg(@args.shift, Justification) {false}
      label.justification = a if a
    end
  end

  parser.separator "\nEdges and axes look:"

  parser.on("--edges-visibility SPEC",
            "Chooses which edges of the current graph will be",
            "visible. Will not act on edges that are also axes.") do |w|
    current_object.plot_style.edges.edge_visibility = 
      parse_edge_visibility(w)
  end

  parser.on("--axis-pos AXIS POS",
            "Sets the position of AXIS to POS") do |w|
    run_with_axis(w) do |axis|
      axis.loc = EdgesAndAxes.parse_axis_position(@args.shift)
    end
  end


  parser.on("--xaxis STYLE",
            "Sets the style of the X axis") do |w|
    current_object.plot_style.set_axis_style(:x, w)
  end

  parser.on("--yaxis STYLE",
            "Sets the style of the Y axis") do |w|
    current_object.plot_style.set_axis_style(:y, w)
  end

  parser.on("--edge-style WHICH STYLE",
            "Sets the style of the X axis") do |w|
    current_object.plot_style.set_axis_style(w.to_sym,
                                             @args.shift)
  end

  parser.on("--no-axes",
            "No title, labels and axes") do
    ps = current_plot_style
    ps.set_axis_style(:x, "none")
    ps.set_axis_style(:y, "none")
    ps.xlabel.label = false
    ps.ylabel.label = false
    ps.title.label = false
  end

  parser.on("--lines-color AXIS COLOR",
            "Sets the color for lines perpendicular to AXIS",
            "'none' for no lines") do |w|
    run_with_axis(w) do |axis|
      color = @args.shift
      if color =~ /no(ne)?/
        color = false
      else
        color = CTioga.get_tioga_color(color)
      end
      axis.lines_color = color
    end
  end

  parser.on("--new-axis NAME POSITION",
            "Creates a new axis named NAME for this plot") do |name|
    current_plot_style.edges.axes[name] = 
      EdgesAndAxes::Axis.new(@args.shift)
  end

  parser.on("--axis-function AXIS TO FROM",
            "Sets AXIS to use a non-linear mapping represented",
            "by 2 mathematical functions (of x): TO (to convert from",
            "real to axis) and FROM that does the opposite") do |w|
    run_with_axis(w) do |axis|
      axis.real_to_axis = eval "proc { |x| #{@args.shift} }"
      axis.axis_to_real = eval "proc { |x| #{@args.shift} }"
    end
  end



end

#init_axesObject

Initializes variables pertaining to axes



77
78
79
80
81
82
83
84
85
86
# File 'lib/CTioga/axes.rb', line 77

def init_axes
  # TODO: same as above
  @x_log = false
  @y_log = false
  @x_factor = false
  @y_factor = false
  @x_offset = false
  @y_offset = false
  @decimal_separator = false
end

#parse_edge_visibility(str) ⇒ Object

Turns a String into a [ left, right, top, bottom ] edge visibility specification



90
91
92
93
94
95
96
97
98
99
# File 'lib/CTioga/axes.rb', line 90

def parse_edge_visibility(str)
  # First and simplest : a series of v (for visible) and i
  # (for invisible)
  if str =~ /^\s*([vi]{4})\s*$/i
    val = $1.split(//).map { |s| s =~ /v/i ? true : false }
    return val
  end
  # By default, everything is visible
  return [ true, true, true, true ]
end

#run_with_axis(name) ⇒ Object

Runs a block safely with an axis specification.

This function relies on the fact that it is being used from within a PlotMaker instance !!



117
118
119
120
121
122
123
124
125
126
# File 'lib/CTioga/axes.rb', line 117

def run_with_axis(name)
  w = Utils::interpret_arg(name, AxisSpecification) {name}
  if current_plot_style.edges.axes.key? w
    yield current_plot_style.edges.axes[w]
  else
    error "The axis specification #{w} was not understood, ignoring"
    error "Valid axis specifications are " +
      current_plot_style.edges.axes.keys.join(', ')
  end
end

#run_with_label(name) ⇒ Object

Runs a block safely with a label specification



102
103
104
105
106
107
108
109
110
111
# File 'lib/CTioga/axes.rb', line 102

def run_with_label(name)
  w = Utils::interpret_arg(name, LabelSpecification) {false}
  if w
    yield current_object.plot_style.send(w)
  elsif current_plot_style.edges.axes.key? name
    yield current_plot_style.edges.axes[name].ticks
  else
    error "The label/axis specification #{w} was not understood, ignoring"
  end
end