Class: CTioga2::Graphics::Elements::Curve2D

Inherits:
TiogaElement show all
Includes:
Log, Dobjects
Defined in:
lib/ctioga2/graphics/elements/curve2d.rb

Overview

A Curve2D object represents a 2D curve, along with its style and so on.

todo Put back various stylistic aspects that were present in the old ctioga, such as:

  • transparency

  • drawing order

Instance Attribute Summary collapse

Attributes inherited from TiogaElement

#parent

Instance Method Summary collapse

Methods included from Log

debug, error, fatal, #format_exception, #identify, info, init_logger, logger, set_level, #spawn, warn

Methods inherited from TiogaElement

#do, #inspect

Constructor Details

#initialize(dataset, style = nil) ⇒ Curve2D

Creates a new Curve2D object with the given dataset and style.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 59

def initialize(dataset, style = nil)
  @dataset = dataset
  if @dataset.size > 2
    warn { "Columns Y2 and further were ignored for set #{dataset.name}" }
  end
  # We build the function on a duplicate of the values ?
  @function = Function.new(@dataset.x.values.dup, 
                           @dataset.y.values.dup)
  @curve_style = style

  # Preparation of the subpath elements
  if @curve_style.split_on_nan
    # This requires Tioga r601 !
    @path_elements = @function.split_on_nan(:xy)
    info { "Dividing into #{@path_elements.size} subpaths" }
  else
    @path_elements = [@function]
  end
  @function.strip_nan

end

Instance Attribute Details

#curve_styleObject

A Styles::CurveStyle object saying how the curve should be drawn.



53
54
55
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 53

def curve_style
  @curve_style
end

#datasetObject

The Data::Dataset object that should get plotted.



49
50
51
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 49

def dataset
  @dataset
end

#functionObject

A Dobjects::Function holding the “real” X and Y values, for the sake of manipulations.



43
44
45
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 43

def function
  @function
end

#path_elementsObject

Elements of the path, when there are more than one:



46
47
48
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 46

def path_elements
  @path_elements
end

Instance Method Details

#close_path(t, y0) ⇒ Object

A function to close the path created by make_path. Overridden in the histogram code.



150
151
152
153
154
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 150

def close_path(t, y0)
  t.append_point_to_path(@function.x.last, y0)
  t.append_point_to_path(@function.x.first, y0)
  t.close_path
end

#draw_errorbars(t) ⇒ Object



176
177
178
179
180
181
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 176

def draw_errorbars(t)
  return unless @dataset.has_xy_errors?
  @dataset.each_values(true, true) do |*vals|
    @curve_style.error_bar.show_error_bar(t, *(vals[1..6]))
  end
end

#draw_fill(t) ⇒ Object

Draws the filled region according to the :fill_type element of the style pseudo-hash. It can be:



166
167
168
169
170
171
172
173
174
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 166

def draw_fill(t)
  return unless @curve_style.fill.y0
  t.context do
    # Remember: first setup_fill, then draw path, then do_fill
    @curve_style.fill.setup_fill(t)
    make_closed_path(t,@curve_style.fill.y0)
    @curve_style.fill.do_fill(t)
  end
end

#draw_markers(t) ⇒ Object

Draws the markers, if applicable.



140
141
142
143
144
145
146
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 140

def draw_markers(t)
  if @curve_style.has_marker?
    xs = @function.x
    ys = @function.y
    @curve_style.marker.draw_markers_at(t, xs, ys)
  end
end

#draw_path(t) ⇒ Object

Strokes the path.



129
130
131
132
133
134
135
136
137
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 129

def draw_path(t)
  if @curve_style.has_line?
    t.context do 
      @curve_style.line.set_stroke_style(t)
      make_path(t)
      t.stroke
    end
  end
end

#get_axesObject

Returns the AxisSyle objects for the X and Y axes as an array.



157
158
159
160
161
162
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 157

def get_axes
  return [ 
          parent.style.get_axis_style(@curve_style.xaxis),
          parent.style.get_axis_style(@curve_style.yaxis)
         ]
end

#get_boundariesObject

Returns the Types::Boundaries of this curve.



88
89
90
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 88

def get_boundaries
  return Types::Boundaries.bounds(@function.x, @function.y)
end

#locationObject

Returns the LocationStyle object of the curve. Returns the one from #curve_style.



83
84
85
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 83

def location
  return @curve_style.location
end

#make_closed_path(t, fv) ⇒ Object

Adds a closed path to the given FigureMaker object. The path is closed according to the specification given as fv, which is the same as the y0 attribute of a CurveFillStyle.

It must not be false



122
123
124
125
126
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 122

def make_closed_path(t, fv)
  y0 = fill_value_to_y(fv)
  make_path(t)
  close_path(t, y0)
end

#make_path(t) ⇒ Object

Creates a path for the given curve. This should be defined with care, as it will be used for instance for region coloring and stroking. The function should only append to the current path, not attempt to create a new path or empty what was done before.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 97

def make_path(t)
  bnds = parent.get_el_boundaries(self)

  for func in @path_elements
    case @curve_style.path_style
    when /splines/
      for f in func.split_monotonic
        new_f = f.bound_values(*bnds.extrema)
        t.append_interpolant_to_path(new_f.make_interpolant)
      end
    else
      f = func.bound_values(*bnds.extrema)
      t.move_to_point(f.x.first, f.y.first)
      t.append_points_to_path(f.x[1..-1], f.y[1..-1])
    end
  end
  
end

#real_do(t) ⇒ Object

Actually draws the curve



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 184

def real_do(t)
  debug { "Plotting curve #{inspect}" }
  t.context do
    ## \todo allow customization of the order of drawing,
    ## using a simple user-specificable array of path,
    ## markers... and use the corresponding #draw_path or
    ## #draw_markers... Ideally, any string could be used, and
    ## warnings should be issued on missing symbols.

    draw_fill(t)
    draw_errorbars(t)
    draw_path(t)
    draw_markers(t)
  end
end