Class: CTioga::LegendStyle

Inherits:
Object
  • Object
show all
Includes:
Debug, Log
Defined in:
lib/CTioga/legends/style.rb

Overview

A class that provides style information and display facilities for legends.

It does not provide contents for the legends themselves

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#identify, #init_logger, #logger, #logger_options, #spawn

Methods included from Debug

#debug_figmaker, #debug_patterns, #debug_puts, #figmaker_options, #test_pattern, #test_pattern_right

Constructor Details

#initializeLegendStyle

Returns a new instance of LegendStyle.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/CTioga/legends/style.rb', line 76

def initialize
  # All in relative units ;-)...
  @dy = TextDimension.new(1.9)
  @padding = TextDimension.new(0.5)
  @picto_width = TextDimension.new(1.5)
  @picto_height = TextDimension.new(0.75)
  @picto_to_text = TextDimension.new(0.5)
  @scale = 1.0

  @box_style = false        # For no box.

  # Frame styles:
  @background_color = false
  @background_transparency = 0.0
  @line_color = Tioga::FigureConstants::Black
  @line_width = 1.0
  @line_style = false


  @margin = TextDimension.new(0.4)

end

Instance Attribute Details

#background_colorObject

The color of the background. false means no background Only meaningful when the #box_style is set to something different of none.



62
63
64
# File 'lib/CTioga/legends/style.rb', line 62

def background_color
  @background_color
end

#background_transparencyObject

The transparency of the background



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

def background_transparency
  @background_transparency
end

#box_styleObject

Style of the box around the legend : (none/round/box)



53
54
55
# File 'lib/CTioga/legends/style.rb', line 53

def box_style
  @box_style
end

#dyObject

The distance between two legend lines



32
33
34
# File 'lib/CTioga/legends/style.rb', line 32

def dy
  @dy
end

#line_colorObject

The color for the lines



65
66
67
# File 'lib/CTioga/legends/style.rb', line 65

def line_color
  @line_color
end

#line_styleObject

The color for the lines



71
72
73
# File 'lib/CTioga/legends/style.rb', line 71

def line_style
  @line_style
end

#line_widthObject

The color for the lines



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

def line_width
  @line_width
end

#marginObject

The amount of space that should be left around the legend when drawing the box.



57
58
59
# File 'lib/CTioga/legends/style.rb', line 57

def margin
  @margin
end

#paddingObject

The padding between the legend and anything else around



38
39
40
# File 'lib/CTioga/legends/style.rb', line 38

def padding
  @padding
end

#picto_heightObject

The height of the pictogram



44
45
46
# File 'lib/CTioga/legends/style.rb', line 44

def picto_height
  @picto_height
end

#picto_to_textObject

The distance between the pictogram and the text



47
48
49
# File 'lib/CTioga/legends/style.rb', line 47

def picto_to_text
  @picto_to_text
end

#picto_widthObject

The width of the pictogram



41
42
43
# File 'lib/CTioga/legends/style.rb', line 41

def picto_width
  @picto_width
end

#scaleObject

The legend scale



35
36
37
# File 'lib/CTioga/legends/style.rb', line 35

def scale
  @scale
end

#text_scaleObject

Scales all the text by that factor:



50
51
52
# File 'lib/CTioga/legends/style.rb', line 50

def text_scale
  @text_scale
end

Instance Method Details

#legend_context(t, legend_specs, plot_margins) ⇒ Object

Prepares the context in which the legend is made, and run the corresponding code



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
# File 'lib/CTioga/legends/style.rb', line 103

def legend_context(t, legend_specs,plot_margins)
  subfigure_margins = {}

  if legend_specs.key?('legend_left_margin')
    # Now, we enter a subfigure:
    for pos in %w{left right top bottom}
      subfigure_margins[pos] = legend_specs["legend_#{pos}_margin"]
    end
    debug "Legend margins #{subfigure_margins.inspect}"
  else                      # We make a subfigure relative to the plot
    if plot_margins
      subfigure_margins = plot_margins.dup
    else
      plot_margins = false
    end
  end

  t.context do
    t.set_subframe(subfigure_margins) if subfigure_margins
    t.rescale(@scale)
    t.rescale_text(@text_scale)
    yield 
  end

end

#prepare_frame_path(t, xl, yt, total_width, total_height) ⇒ Object

Prepare the frame path



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
# File 'lib/CTioga/legends/style.rb', line 130

def prepare_frame_path(t,xl,yt, total_width, total_height)
  case @box_style
  when :square
    t.append_rect_to_path(xl - @margin.to_figure(t, :x), 
                          yt + @margin.to_figure(t, :y),
                          total_width + 2 * @margin.to_figure(t, :x), 
                          - total_height - 2 * @margin.to_figure(t, :y))   
  when :round
    # For some reason, the append_rounded_rect_to_path does not
    # work so well...
    dx = @margin.to_figure(t, :x)
    dy = @margin.to_figure(t, :y)
    xr = xl + total_width
    yb = yt - total_height
    t.move_to_point(xl - dx, yt)
    t.append_curve_to_path(xl - dx, yt + 0.5 * dy, # First control point
                           xl - 0.5 * dx, yt + dy, 
                           xl, yt + dy)
    t.append_point_to_path(xr, yt + dy)
    t.append_curve_to_path(xr + 0.5 * dx, yt + dy, # First control point
                           xr + dx, yt + 0.5 * dy, 
                           xr + dx, yt)
    t.append_point_to_path(xr + dx, yb)
    t.append_curve_to_path(xr + dx, yb - 0.5 * dy, # First control point
                           xr + 0.5 * dx, yb - dy, 
                           xr, yb - dy)
    t.append_point_to_path(xl, yb - dy)
    t.append_curve_to_path(xl - 0.5 * dx, yb - dy, # First control point
                           xl - dx, yb - 0.5 * dy, 
                           xl - dx, yb)
    t.close_path
  end
end

#show_legends(t, legend_specs, plot_margins, legend_info) ⇒ Object

Displays legends legend_info to the FigureMaker object t using boundaries given by legend_specs for the plot with the given plot_margins



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
# File 'lib/CTioga/legends/style.rb', line 167

def show_legends(t, legend_specs, plot_margins, legend_info)
  debug "Plotting legends: #{legend_specs.inspect}"
  @text_scale ||= t.legend_scale

  # First, we collect data on the legend_info
  widths = []
  heights = []
  total_height = 0
  total_width = 0
  legend_context(t, legend_specs, plot_margins) do 
    for item in legend_info
      w, h = item.size(t, self)
      widths << w
      heights << h
      total_height += h
      total_width = w if total_width < w
    end
  end

  debug "Total height: #{total_height}\tTotal width: #{total_width}"

  # Need to scope them now, so the context down actually modifies them !
  xl = 0
  yl = 0
  if legend_specs.key?('legend_left_margin')
    legend_context(t, legend_specs, plot_margins) do 
      yl = 1
      xl = @padding.to_figure(t, :x)
    end
  elsif legend_specs.key?('legend_left') or 
      legend_specs.key?('legend_right') or 
      legend_specs.key?('legend_hcenter')

    legend_context(t, legend_specs, plot_margins) do 
      if legend_specs.key?('legend_left')
        xl = t.convert_frame_to_figure_x(legend_specs['legend_left'])
      elsif legend_specs.key?('legend_right')
        xl = t.convert_frame_to_figure_x(legend_specs['legend_right']) - 
          total_width
      else
        xl = t.convert_frame_to_figure_x(legend_specs['legend_hcenter']) - 
          total_width * 0.5
      end
      
      if legend_specs.key?('legend_top')
        yl = t.convert_frame_to_figure_y(legend_specs['legend_top'])
      elsif legend_specs.key?('legend_bottom')
        yl = t.convert_frame_to_figure_y(legend_specs['legend_bottom']) + 
          total_height
      else
        yl = t.convert_frame_to_figure_y(legend_specs['legend_vcenter']) + 
          total_height * 0.5
      end
    end
  else
    # Silently do nothing:
    return
  end

  # Always wrapping within a context call ;-)...
  legend_context(t, legend_specs, plot_margins) do 
    # First, we draw the box if necessary 
    if @box_style
      t.context do 
        if @background_color
          t.fill_color = @background_color
          t.fill_transparency = @background_transparency
          prepare_frame_path(t, xl, yl, total_width, total_height)
          t.fill
        end
        if @line_color
          t.stroke_color = @line_color
          t.line_width = @line_width
          t.line_type = @line_style if @line_style
          prepare_frame_path(t, xl, yl, total_width, total_height)
          t.stroke
        end
      end
    end
    
    for item in legend_info
      w, h = item.size(t, self)
      item.draw(t, self, xl, yl)
      yl -= h
    end
  end
end