Class: GraphAxis

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

Overview

Calculate axis (only) and draw them

Constant Summary collapse

SAFE_ENL_COEFF =

some issues with float precision and rounding

1.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(technical_graph) ⇒ GraphAxis

Returns a new instance of GraphAxis.



43
44
45
# File 'lib/technical_graph/graph_axis.rb', line 43

def initialize(technical_graph)
  @technical_graph = technical_graph
end

Instance Attribute Details

#technical_graphObject (readonly)

Returns the value of attribute technical_graph.



7
8
9
# File 'lib/technical_graph/graph_axis.rb', line 7

def technical_graph
  @technical_graph
end

Instance Method Details

#adjust_axis_to_zeroObject



64
65
66
# File 'lib/technical_graph/graph_axis.rb', line 64

def adjust_axis_to_zero
  options[:adjust_axis_to_zero]
end

#antialiasObject



200
201
202
# File 'lib/technical_graph/graph_axis.rb', line 200

def antialias
  options[:antialias] == true
end

#axis_distance_image_enlargeObject

Enlarge image to maintain proper axis density



130
131
132
133
134
135
136
137
138
139
# File 'lib/technical_graph/graph_axis.rb', line 130

def axis_distance_image_enlarge
  if options[:axis_density_enlarge_image] or options[:x_axis_density_enlarge_image] or options[:y_axis_density_enlarge_image]
    t = Time.now
    x_axis_distance_image_enlarge if options[:axis_density_enlarge_image] or options[:x_axis_density_enlarge_image]
    y_axis_distance_image_enlarge if options[:axis_density_enlarge_image] or options[:y_axis_density_enlarge_image]

    logger.debug "axis enlarged"
    logger.debug " TIME COST #{Time.now - t}"
  end
end

#calc_axis(from, to, interval, count, fixed_interval) ⇒ Object

Calculate axis using 2 methods



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/technical_graph/graph_axis.rb', line 79

def calc_axis(from, to, interval, count, fixed_interval)
  t = Time.now

  axis = Array.new
  l = to - from
  current = from

  if fixed_interval
    while current < to
      axis << current
      current += interval
    end
    axis = move_axis_to_fit_zero(axis) if adjust_axis_to_zero

    logger.debug "fixed interval axis calculation from #{from} to #{to} using int. #{interval}"
    logger.debug " TIME COST #{Time.now - t}"
    return axis

  else
    (0...count).each do |i|
      axis << from + (l.to_f * i.to_f) / count.to_f
    end
    axis = move_axis_to_fit_zero(axis) if adjust_axis_to_zero

    logger.debug "fixed count axis calculation from #{from} to #{to} using count #{count}"
    logger.debug " TIME COST #{Time.now - t}"
    return axis

  end
end

#data_processorObject

Calculate everything



23
24
25
# File 'lib/technical_graph/graph_axis.rb', line 23

def data_processor
  @technical_graph.data_processor
end

#drawerObject



31
32
33
# File 'lib/technical_graph/graph_axis.rb', line 31

def drawer
  image.drawer
end

#imageObject



27
28
29
# File 'lib/technical_graph/graph_axis.rb', line 27

def image
  @technical_graph.image_drawer
end

#layersObject

Accessor for DataLayer Array



18
19
20
# File 'lib/technical_graph/graph_axis.rb', line 18

def layers
  @technical_graph.layers
end

#loggerObject



35
36
37
# File 'lib/technical_graph/graph_axis.rb', line 35

def logger
  @technical_graph.logger
end

#move_axis_to_fit_zero(axis) ⇒ Object

Process axis array, give offset to match zero axis, and remove zero axis from array



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/technical_graph/graph_axis.rb', line 111

def move_axis_to_fit_zero(axis)
  # if zero axis is within
  if axis.min <= 0 and axis.max >= 0
    # if there is axis within -1..1
    axis.each_with_index do |a, i|
      if a >= -1.0 and a <= 1.0
        # this is the offset, move using found offset
        return axis.collect { |b| b - a }
      end
    end
  end

  # TODO when it won't work?

  # return unmodified
  return axis
end

#optionsObject

Accessor for options Hash



13
14
15
# File 'lib/technical_graph/graph_axis.rb', line 13

def options
  @technical_graph.options
end

#parameter_axisObject

Where to put axis values



74
75
76
# File 'lib/technical_graph/graph_axis.rb', line 74

def parameter_axis
  return calc_axis(data_processor.x_min, data_processor.x_max, options[:x_axis_interval], options[:x_axis_count], x_axis_fixed?)
end

#render_axisObject

Render normal axis



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/technical_graph/graph_axis.rb', line 205

def render_axis
  drawer.axis(
    # X
    parameter_axis.collect { |x| image.calc_bitmap_x(x).to_i },
    # Y
    value_axis.collect { |y| image.calc_bitmap_y(y).to_i },
    # options
    { :color => options[:axis_color], :width => 1 },
    # draw labels
    options[:axis_value_and_param_labels],
    # X axis labels
    parameter_axis,
    # Y axis labels
    value_axis
  )
end

#render_axis_labelsObject



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/technical_graph/graph_axis.rb', line 241

def render_axis_labels
  drawer.axis_labels(
    options[:x_axis_label].to_s,
    options[:y_axis_label].to_s,
    {
      :color => options[:axis_color],
      :width => 1,
      :size => options[:axis_label_font_size],
    }
  )
end

#render_on_image(image) ⇒ Object

Render axis on image



192
193
194
195
196
197
198
# File 'lib/technical_graph/graph_axis.rb', line 192

def render_on_image(image)
  @image = image

  render_axis
  render_zero_axis
  render_axis_labels
end

#render_zero_axisObject

Render axis for zeros



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/technical_graph/graph_axis.rb', line 223

def render_zero_axis
  drawer.axis(
    # X - 0
    image.calc_bitmap_x(0.0).to_i,
    # Y - 0
    image.calc_bitmap_y(0.0).to_i,
    # options, slightly wider
    { :color => options[:axis_color], :width => 2 },
    # draw label
    options[:axis_zero_labels],
    # X label,
    [0.0],
    # Y label
    [0.0]
  )
end

#truncate_stringObject



39
40
41
# File 'lib/technical_graph/graph_axis.rb', line 39

def truncate_string
  options[:truncate_string]
end

#value_axisObject

Where to put axis values



69
70
71
# File 'lib/technical_graph/graph_axis.rb', line 69

def value_axis
  return calc_axis(data_processor.y_min, data_processor.y_max, options[:y_axis_interval], options[:y_axis_count], y_axis_fixed?)
end

#x_axis_distance_image_enlargeObject

Enlarge image to maintain proper axis density



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/technical_graph/graph_axis.rb', line 142

def x_axis_distance_image_enlarge
  a = parameter_axis
  # must be at least 2 axis
  logger.debug "axis enlargement - parameter_axis #{a.inspect}"
  return if a.size < 2

  ax = a[0]
  ax = image.calc_bitmap_x(ax).round
  bx = a[1]
  bx = image.calc_bitmap_x(bx).round

  axis_distance = (bx - ax).abs

  logger.debug "axis enlargement - width, axis distance #{axis_distance} should be at least #{options[:x_axis_min_distance]}"
  if axis_distance < options[:x_axis_min_distance]
    # enlarging image
    options[:old_width] = options[:width]
    options[:width] = options[:width].to_f * (options[:x_axis_min_distance].to_f / axis_distance.to_f)
    options[:width] *= SAFE_ENL_COEFF
    options[:width] = options[:width].ceil
    logger.debug "axis enlarged - width modified to #{options[:width]}"
  end
end

#x_axis_fixed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/technical_graph/graph_axis.rb', line 47

def x_axis_fixed?
  options[:x_axis_fixed_interval] == true
end

#x_axis_intervalObject



60
61
62
# File 'lib/technical_graph/graph_axis.rb', line 60

def x_axis_interval
  options[:x_axis_interval]
end

#y_axis_distance_image_enlargeObject

Enlarge image to maintain proper axis density



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/technical_graph/graph_axis.rb', line 167

def y_axis_distance_image_enlarge
  a = value_axis
  # must be at least 2 axis
  logger.debug "axis enlargement - value_axis #{a.inspect}"
  return if a.size < 2

  ay = a[0]
  ay = image.calc_bitmap_y(ay).round
  by = a[1]
  by = image.calc_bitmap_y(by).round

  axis_distance = (by - ay).abs

  logger.debug "axis enlargement - height, axis distance #{axis_distance} should be at least #{options[:y_axis_min_distance]}"
  if axis_distance < options[:y_axis_min_distance]
    # enlarging image
    options[:old_height] = options[:height]
    options[:height] = options[:height].to_f * (options[:y_axis_min_distance].to_f / axis_distance.to_f)
    options[:height] *= SAFE_ENL_COEFF
    options[:height] = options[:height].ceil
    logger.debug "axis enlarged - height modified from #{options[:old_height]} to #{options[:height]}"
  end
end

#y_axis_fixed?Boolean

Value axis has fixed count

Returns:

  • (Boolean)


52
53
54
# File 'lib/technical_graph/graph_axis.rb', line 52

def y_axis_fixed?
  options[:y_axis_fixed_interval] == true
end

#y_axis_intervalObject



56
57
58
# File 'lib/technical_graph/graph_axis.rb', line 56

def y_axis_interval
  options[:y_axis_interval]
end