Class: CTioga::PlotStyle

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

Overview

The PlotStyle class is an abstraction for whole-plot styles, storing for instance information about axes/ticks (delegated to an EdgesAndAxes object), background color, and so on…

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

#initialize(target_plot = nil, subplot = false) ⇒ PlotStyle

Returns a new instance of PlotStyle.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/CTioga/plot_style.rb', line 63

def initialize(target_plot = nil, subplot = false)
  @title = Label.new(:title)
  @title.label = "A nice plot" unless subplot
  
  @xlabel = Label.new(:xlabel)
  @xlabel.label = "$x$" unless subplot
  
  @ylabel = Label.new(:ylabel)
  @ylabel.label = "$y$" unless subplot

  @xticks = TickLabels.new(:xaxis_numeric_label)
  @yticks = TickLabels.new(:yaxis_numeric_label)

  @edges = EdgesAndAxes.new(@xticks, @yticks)

  @target_plot = target_plot
  
  @background_color = false

  @legend_style = LegendStyle.new

  @watermark_scale = 0.15
end

Instance Attribute Details

#background_colorObject

The background color, if applicable



49
50
51
# File 'lib/CTioga/plot_style.rb', line 49

def background_color
  @background_color
end

#edgesObject

An EdgesAndAxes object attached to the plot



37
38
39
# File 'lib/CTioga/plot_style.rb', line 37

def edges
  @edges
end

#legend_styleObject

The style with which to display legends



52
53
54
# File 'lib/CTioga/plot_style.rb', line 52

def legend_style
  @legend_style
end

#target_plotObject

The target SubPlot object



46
47
48
# File 'lib/CTioga/plot_style.rb', line 46

def target_plot
  @target_plot
end

#titleObject

Various textual objects laying around:



40
41
42
# File 'lib/CTioga/plot_style.rb', line 40

def title
  @title
end

#watermark_colorObject

The color of the watermark



58
59
60
# File 'lib/CTioga/plot_style.rb', line 58

def watermark_color
  @watermark_color
end

#watermark_scaleObject

The scale of the watermark



61
62
63
# File 'lib/CTioga/plot_style.rb', line 61

def watermark_scale
  @watermark_scale
end

#watermark_textObject

A watermark in the background



55
56
57
# File 'lib/CTioga/plot_style.rb', line 55

def watermark_text
  @watermark_text
end

#xlabelObject

Various textual objects laying around:



40
41
42
# File 'lib/CTioga/plot_style.rb', line 40

def xlabel
  @xlabel
end

#xticksObject

X and Y tick labels



43
44
45
# File 'lib/CTioga/plot_style.rb', line 43

def xticks
  @xticks
end

#ylabelObject

Various textual objects laying around:



40
41
42
# File 'lib/CTioga/plot_style.rb', line 40

def ylabel
  @ylabel
end

#yticksObject

X and Y tick labels



43
44
45
# File 'lib/CTioga/plot_style.rb', line 43

def yticks
  @yticks
end

Instance Method Details

#deep_copy(new_target = nil) ⇒ Object

Creates a deep copy of the style object, and give it a new container.



217
218
219
220
221
222
223
224
# File 'lib/CTioga/plot_style.rb', line 217

def deep_copy(new_target = nil)
  old_target = @target_plot
  @target_plot = nil
  new_object = Marshal::load(Marshal::dump(self))
  new_object.target_plot = new_target
  @target_plot = old_target
  return new_object
end

#disable_all_axis_and_edges(*which) ⇒ Object

Hides axis and all edges for the given sides. Careful, as this also disables the children’s axes and edges



135
136
137
138
139
140
# File 'lib/CTioga/plot_style.rb', line 135

def disable_all_axis_and_edges(*which)
  for w in which
    @edges.set_edges_visibility(w, false)
    @edges.axis(w).visible = false
  end
end

#hide_axis_and_edges(*which) ⇒ Object



150
151
152
153
154
# File 'lib/CTioga/plot_style.rb', line 150

def hide_axis_and_edges(*which)
  for w in which
    @edges.set_axis_and_edges_style(w, AXIS_HIDDEN)
  end
end

#set_axis_style(which, style) ⇒ Object

A very nice-and-convenient way to set quickly axes properties. which is either :x or :y, or :left, :top, :right, and :bottom, in which case the style applies only to the given edge or axis



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
# File 'lib/CTioga/plot_style.rb', line 165

def set_axis_style(which, style)
  # We set various parameters according to the given style
  for s in style.split(/,/)
    case s
    when /none/i
      hide_axis_and_edges(which)
    when /[xy]?=?0/i, /ori?g(in)?/i
      @edges.set_edges_visibility(which, false)
      @edges.axis(which).ticks_inside = false
      @edges.axis(which).ticks_outside = true
      @edges.axis(which).loc = (which == :x ? AT_Y_ORIGIN : AT_X_ORIGIN)
    when /both/             # Both sides visible
      # We make sure the edges are visible
      @edges.set_edges_visibility(which, true)
    when /left/i, /right/i
      if which == :x
        warn "Axis style #{s} can only apply to the Y axis, ignoring"
      else
        @edges.set_edges_visibility(which, false)
        @edges.axis(which).loc = ( s =~ /left/i ? LEFT : RIGHT)
      end
    when /top/i, /bottom/i
      if which == :y
        warn "Axis style #{s} can only apply to the X axis, ignoring"
      else
        @edges.set_edges_visibility(which, false)
        @edges.axis(which).loc = ( s =~ /top/i ? TOP : BOTTOM)
      end
      # Now, stylistic information rather than position:
    when /hidden/i
      @edges.set_axis_and_edges_style(which, AXIS_HIDDEN)
    when /line/i
      @edges.set_axis_and_edges_style(which, AXIS_LINE_ONLY)
    when /ticks/i
      @edges.set_axis_and_edges_style(which, AXIS_WITH_TICKS_ONLY)
    when /majornum/i
      @edges.
        set_axis_and_edges_style(which, 
                                 AXIS_WITH_MAJOR_TICKS_AND_NUMERIC_LABELS)
    when /major/i
      @edges.set_axis_and_edges_style(which, AXIS_WITH_MAJOR_TICKS_ONLY)
    when /full/i
      @edges.set_axis_and_edges_style(which, 
                                      AXIS_WITH_TICKS_AND_NUMERIC_LABELS)
    else
      warn "Axis style #{s} not understood, ignoring"
    end
  end
end

#set_xy_labels(xlabel, ylabel) ⇒ Object

Sets quickly all X and Y labels



144
145
146
147
# File 'lib/CTioga/plot_style.rb', line 144

def set_xy_labels(xlabel, ylabel)
  @xlabel.label = xlabel
  @ylabel.label = ylabel
end

#show_background(t, container = @target_plot) ⇒ Object

Displays background (background color, grids)



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
# File 'lib/CTioga/plot_style.rb', line 105

def show_background(t, container = @target_plot)
  if @background_color
    t.fill_color = @background_color
    t.fill_frame
  end

  # We draw a watermark text at the back of the plot.
  if @watermark_text
    x = t.convert_frame_to_figure_x(0.5)
    y = t.convert_frame_to_figure_y(0.5)

    delta_y = t.convert_frame_to_figure_dy(@watermark_scale)
    text_scale = delta_y/t.default_text_height_dy

    lines = @watermark_text.split(/\n|\\n/)
    i = + (lines.size-1)/2.0
    for text in lines 
      t.show_marker('string' => text,
                    'color' => @watermark_color || [0.5,0.5,0.5],
                    'x' => x, 'y' => y + delta_y * i,
                    'scale' => text_scale)
      i -= 1
    end
  end
  
  edges.show_axis_lines(t, container)
end

#show_edges(t, container = @target_plot) ⇒ Object

Displays edges and ticks for the given object



88
89
90
91
92
93
# File 'lib/CTioga/plot_style.rb', line 88

def show_edges(t, container = @target_plot)
  @edges.setup(t, container)
  for l in [@xticks, @yticks]
    l.show(t)
  end
end

#show_labels(t, container = @target_plot) ⇒ Object

Sets up the various parameters for titles and labels



96
97
98
99
100
101
102
# File 'lib/CTioga/plot_style.rb', line 96

def show_labels(t, container = @target_plot)
  # Show labels
  for l in [@title, @xlabel, @ylabel]
    l.show(t)
    debug "Extension -> #{l.extension(t).inspect}"
  end
end