Class: DataLayer

Inherits:
Object
  • Object
show all
Defined in:
lib/technical_graph/data_layer.rb

Overview

Stores only data used for one layer Instances of this class are used elsewhere Stores also drawing parameters for one layer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(d = [], options = { }, technical_graph = nil) ⇒ DataLayer

Returns a new instance of DataLayer.



26
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
# File 'lib/technical_graph/data_layer.rb', line 26

def initialize(d = [], options = { }, technical_graph = nil)
  # used for accessing logger
  @technical_graph = technical_graph

  @data_params = options

  @data_params[:color] ||= GraphColorLibrary.instance.get_color
  @data_params[:label] ||= ' '
  # default true, write values near dots
  @data_params[:value_labels] = false if options[:value_labels] == false

  # smoothing parameters
  # by default it is false
  @data_params[:simple_smoother] = true if options[:simple_smoother] == true
  @data_params[:simple_smoother_x] = true if options[:simple_smoother_x] == true
  @data_params[:simple_smoother_level] ||= 3
  @data_params[:simple_smoother_strategy] ||= DataLayerProcessor::DEFAULT_SIMPLE_SMOOTHER_STRATEGY
  # was already done
  @data_params[:processor_finished] = false

  @processor = DataLayerProcessor.new(self)

  # set data and append initial data
  clear_data
  append_data(d)
end

Instance Attribute Details

#data_paramsObject (readonly)

Additional parameters



103
104
105
# File 'lib/technical_graph/data_layer.rb', line 103

def data_params
  @data_params
end

#processorObject (readonly)

can be used to approximation and other data processing



54
55
56
# File 'lib/technical_graph/data_layer.rb', line 54

def processor
  @processor
end

Instance Method Details

#append_data(data_array) ⇒ Object

Accessor for setting chart data for layer to draw



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/technical_graph/data_layer.rb', line 57

def append_data(data_array)
  if data_array.kind_of? Array
    # append as DataPoint
    # convert to DataPoints, which has more specialized methods

    t = Time.now
    data_array.each do |d|
      @data << DataPoint.new(d)
    end
    logger.debug "appending data, size #{data_array.size}"
    logger.debug " TIME COST #{Time.now - t}"

    # sort, clean bad records
    process_data_internal

    # @raw_data is dirty, deleting @processed_data
    @processed_data = nil
  else
    raise 'Data not an Array'
  end
end

#clear_dataObject

Clear data



143
144
145
# File 'lib/technical_graph/data_layer.rb', line 143

def clear_data
  @data = Array.new
end

#colorObject

Color of



106
107
108
# File 'lib/technical_graph/data_layer.rb', line 106

def color
  return @data_params[:color]
end

#labelObject



110
111
112
# File 'lib/technical_graph/data_layer.rb', line 110

def label
  return @data_params[:label]
end

#loggerObject

Use global logger for technical_graph or create new



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/technical_graph/data_layer.rb', line 14

def logger
  return @logger if not @logger.nil?

  if not @technical_graph.nil?
    @logger = @technical_graph.logger
  else
    @logger = Logger.new(STDOUT)
  end

  @logger
end

#perform_parameter_uniqObject



138
139
140
# File 'lib/technical_graph/data_layer.rb', line 138

def perform_parameter_uniq
  return @data_params[:perform_parameter_uniq] == true
end

#process!Object

Run external processor (smoothing, …)



94
95
96
97
98
99
100
# File 'lib/technical_graph/data_layer.rb', line 94

def process!
  t = Time.now
  @processed_data = @data.clone
  @processed_data = @processor.process
  logger.debug "processed data using external processor"
  logger.debug " TIME COST #{Time.now - t}"
end

#process_data_internalObject

Clean and process data used for drawing current data layer



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
# File 'lib/technical_graph/data_layer.rb', line 148

def process_data_internal
  t = Time.now

  # delete duplicates
  if perform_parameter_uniq
    @data = @data.inject([]) { |result, d| result << d unless result.select { |r| r.x == d.x }.size > 0; result }
  end

  logger.debug "internal processor - deleting duplicates"
  logger.debug " TIME COST #{Time.now - t}"
  t = Time.now

  @data.delete_if { |d| d.x.nil? or d.y.nil? }
  @data.sort! { |d, e| d.x <=> e.x }

  logger.debug "internal processor - deleting nils and sorting"
  logger.debug " TIME COST #{Time.now - t}"
  t = Time.now

  # default X values, if data is not empty
  if @data.size > 0
    @data_params[:x_min] = @data.first.x || @options[:default_x_min]
    @data_params[:x_max] = @data.last.x || @options[:default_x_max]

    # default Y values
    y_sort = @data.sort { |a, b| a.y <=> b.y }
    @data_params[:y_min] = y_sort.first.y || @options[:default_y_min]
    @data_params[:y_max] = y_sort.last.y || @options[:@default_y_max]
  end

  logger.debug "internal processor - setting min and max"
  logger.debug " TIME COST #{Time.now - t}"
end

#processed_dataObject

Array of DataPoints, after external processing



85
86
87
88
89
90
91
# File 'lib/technical_graph/data_layer.rb', line 85

def processed_data
  if @processed_data.nil?
    process!
  end

  @processed_data
end

#raw_dataObject

Array of DataPoints, not processed



80
81
82
# File 'lib/technical_graph/data_layer.rb', line 80

def raw_data
  @data
end

#simple_smootherObject

Turn on smoothing processor



120
121
122
# File 'lib/technical_graph/data_layer.rb', line 120

def simple_smoother
  return @data_params[:simple_smoother]
end

#simple_smoother_levelObject

Smoother level



125
126
127
# File 'lib/technical_graph/data_layer.rb', line 125

def simple_smoother_level
  return @data_params[:simple_smoother_level]
end

#simple_smoother_strategyObject

Smoother strategy



130
131
132
# File 'lib/technical_graph/data_layer.rb', line 130

def simple_smoother_strategy
  return @data_params[:simple_smoother_strategy]
end

#simple_smoother_xObject



134
135
136
# File 'lib/technical_graph/data_layer.rb', line 134

def simple_smoother_x
  return @data_params[:simple_smoother_x]
end

#value_labelsObject

Write values near dots



115
116
117
# File 'lib/technical_graph/data_layer.rb', line 115

def value_labels
  return @data_params[:value_labels]
end

#x_maxObject



186
187
188
# File 'lib/technical_graph/data_layer.rb', line 186

def x_max
  @data_params[:x_max]
end

#x_minObject



182
183
184
# File 'lib/technical_graph/data_layer.rb', line 182

def x_min
  @data_params[:x_min]
end

#y_maxObject



194
195
196
# File 'lib/technical_graph/data_layer.rb', line 194

def y_max
  @data_params[:y_max]
end

#y_minObject



190
191
192
# File 'lib/technical_graph/data_layer.rb', line 190

def y_min
  @data_params[:y_min]
end