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.

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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 48

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)

  # We remove NaN, as they are not very liked by Tioga...
  #
  # TODO: maybe there should be a way to *split* on NaN rather
  # than to ignore them ?
  @function.strip_nan

  @curve_style = style
end

Instance Attribute Details

#curve_styleObject

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



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

def curve_style
  @curve_style
end

#datasetObject

The Data::Dataset object that should get plotted.



40
41
42
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 40

def dataset
  @dataset
end

#functionObject

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



37
38
39
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 37

def function
  @function
end

Instance Method Details

#close_path(t, y0) ⇒ Object

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



111
112
113
114
115
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 111

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_fill(t) ⇒ Object

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 119

def draw_fill(t)
  #           y = y_value(@style.fill_type)
  #           return unless y

  #           t.fill_transparency = @style.fill_transparency || 0
  #           # Now is the tricky part. To do the actual fill, we first make a
  #           # path according to the make_path function.
  #           make_path(t)

  #           # Then we add two line segments that go from the end to the
  #           # beginning.
  #           close_path(t, y)

  #           # Now the path is ready. Just strike -- or, rather, fill !
  #           t.fill_color = @style.fill_color
  #           t.fill
end

#draw_markers(t) ⇒ Object

Draws the markers, if applicable.



101
102
103
104
105
106
107
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 101

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.



90
91
92
93
94
95
96
97
98
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 90

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_boundariesObject

Returns the Types::Boundaries of this curve.



67
68
69
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 67

def get_boundaries
  return Types::Boundaries.bounds(@function.x, @function.y)
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.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 76

def make_path(t)
  bnds = parent.real_boundaries
  #           if @style.interpolate
  #             for f in @function.split_monotonic
  #               new_f = f.bound_values(*bnds.to_a)
  #               t.append_interpolant_to_path(f.make_interpolant)
  #             end
  #           else
  f = @function.bound_values(*bnds.extrema)
  t.append_points_to_path(f.x, f.y)
  #          end
end

#real_do(t) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ctioga2/graphics/elements/curve2d.rb', line 137

def real_do(t)
  debug "Plotting curve #{inspect}"
  t.context do
    # TODO reinstate the choice of the order of drawing ???
    draw_path(t)
    draw_markers(t)
    #             # The fill is always first
    #             draw_fill(t)

    #             for op in CurveStyle::DrawingOrder[@style[:drawing_order]]
    #               self.send("draw_#{op}".to_sym, t)
    #             end
  end
end