Module: CTioga::Legends

Defined in:
lib/CTioga/legends/cmdline.rb

Overview

A module to deal with legends…

Class Method Summary collapse

Class Method Details

.fill_option_parser(parser, target) ⇒ Object

Fills up an OptionParser, parser with legend-related command-line options. Takes a Plotmaker target



64
65
66
67
68
69
70
71
72
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
# File 'lib/CTioga/legends/cmdline.rb', line 64

def self.fill_option_parser(parser, target)

  parser.separator "\nLegends:"
  
  parser.on("-l","--[no-]legend [LEGEND]",
             "Sets the legend of the next curve.") do |l|
    target.override_style.legend = l
  end

  parser.on("--[no-]auto-legend", 
             "Whether to automatically add legends",
             "to curves") do |l|
    target.autolegends = l
  end

  parser.on('-N', "--no-legends", 
             "Alias for --no-auto-legend") do
    target.autolegends = false
  end

  ## Legend scale
  parser.on("--legend-scale FACT",
             "Sets the scale for the legends") do |l|
    target.add_elem_funcall(:legend_scale=, Float(l))
  end

  parser.on("--legend-line TEXT",
             "Adds the line TEXT in the legend") do |l|
    target.current_object.add_legend_info(LegendLine.new(l))
  end


  parser.on("--legend-dy DIM",
             "The distance between two legend lines, ",
             "expressed in terms of text line size") do |l|
    target.current_object.plot_style.legend_style.dy = 
      TextDimension.new(l)
  end

  parser.on("--legend-pos SPEC", 
             "Sets the legend position and its size.",
             "SPEC looks like pos,size[,delta]") do |a|
    w,s,d = a.split /\s*,\s*/
    if d
      warn "The delta specification (#{d}) for #{a} is ignored"
    end
    target.debug "Legend position #{w.inspect} #{s.inspect}"
    target.current_object.layout_preferences.legend_position = w.to_sym
    target.current_object.layout_preferences.legend_size = Dimension.new(s)
  end

  parser.on("--legend-inside SPEC", 
             "Makes a legend inside the plot.",
             "See --inset for the format of SPEC") do |a|
    specs = legend_position(a)
    target.debug "Specs: #{specs.inspect}"
    target.current_object.layout_preferences.legend_position = :inside
    target.current_object.layout_preferences.legend_spec = specs
  end

  # Frames around legends:
  parser.on("--[no-]legend-frame [WHAT]",
            "Which frame should ctioga draw around legends ?") do |l|
    if l =~ /square|round/i
      l = l.downcase.to_sym
    elsif l =~ /none/i || (!l)
      l = false
    else
      warn "Incorrect legend frame: '#{l}'"
    end
    target.current_object.plot_style.legend_style.box_style = l
  end

  # TODO: make all the legend styles...
  parser.on("--[no-]legend-background [COLOR]",
            "Background color for the legend") do |c|
    c = CTioga.get_tioga_color(c) if c
    target.current_object.plot_style.legend_style.background_color = c
  end

  parser.on("--[no-]legend-transparency [VALUE]",
            "Transparency for the background") do |f|
    if !f 
      f = 0.0
    else
      f = f.to_f
    end
    target.current_object.plot_style.legend_style.
      background_transparency = f
  end

  parser.on("--[no-]legend-color [COLOR]",
            "Color for the line around the legend") do |c|
    c = CTioga.get_tioga_color(c) if c
    target.current_object.plot_style.legend_style.line_color = c
  end

  parser.on("--legend-line-width WIDTH",
            "Line width for drawing the legend") do |c|
    c = Float(c)
    target.current_object.plot_style.legend_style.line_width = c
  end

  parser.on("--legend-line-style STYLE",
            "Line style for drawing the legend") do |c|
    c = MetaBuilder::ParameterType.from_string(:line_style,c)
    target.current_object.plot_style.legend_style.line_style = c
  end
  
  parser.on("--[no-]separate-legends",
            "If set, PDF files are produced that contain",
            "what the legend pictogram would look like") do |l|
    target.separate_legends = l
    if l
      # Switches off autolegends by default
      target.autolegends = false
    end
  end

end

.legend_position(spec) ⇒ Object

Parse the legend positions



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/CTioga/legends/cmdline.rb', line 27

def self.legend_position(spec)
  specs = {}
  begin
    margins = Utils::inset_margins(spec)
    specs = margins.to_frame("legend_%s_margin")
  rescue                  # in case of an incorrect margin
    # Then we look for a position in the spirit of:
    if spec =~ /\s*([tbc])([rlc])\s*(?::\s*([\d.eE+-]+),([\d.eE+-]+))?\s*$/
      if($3 && $4)
        x = Float($3)
        y = Float($4)
      else
        x = y = nil
      end
      if $1 == 't'
        specs['legend_top'] = y || 0.95 
      elsif $1 == 'b'
        specs['legend_bottom'] = y || 0.05 
      else
        specs['legend_vcenter'] = y || 0.5
      end
      if $2 == 'l'
        specs['legend_left'] = x || 0.05
      elsif $2 == 'r'
        specs['legend_right'] = x || 0.95
      else
        specs['legend_hcenter'] = x || 0.5
      end
    else
      raise "Incorrect legend (inside) position: '#{spec}'"
    end
  end
  return specs
end