Class: GraphAxis

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

Overview

Calculate axis (only) and draw them

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(technical_graph) ⇒ GraphAxis

Returns a new instance of GraphAxis.



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

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



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

def adjust_axis_to_zero
  options[:adjust_axis_to_zero]
end

#antialiasObject



193
194
195
# File 'lib/technical_graph/graph_axis.rb', line 193

def antialias
  options[:antialias] == true
end

#axis_distance_image_enlargeObject

Enlarge image to maintain proper axis density



127
128
129
130
131
132
133
134
135
136
# File 'lib/technical_graph/graph_axis.rb', line 127

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

    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



76
77
78
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
# File 'lib/technical_graph/graph_axis.rb', line 76

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



20
21
22
# File 'lib/technical_graph/graph_axis.rb', line 20

def data_processor
  @technical_graph.data_processor
end

#drawerObject



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

def drawer
  image.drawer
end

#imageObject



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

def image
  @technical_graph.image_drawer
end

#layersObject

Accessor for DataLayer Array



15
16
17
# File 'lib/technical_graph/graph_axis.rb', line 15

def layers
  @technical_graph.layers
end

#loggerObject



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

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



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

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



10
11
12
# File 'lib/technical_graph/graph_axis.rb', line 10

def options
  @technical_graph.options
end

#parameter_axisObject

Where to put axis values



71
72
73
# File 'lib/technical_graph/graph_axis.rb', line 71

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/technical_graph/graph_axis.rb', line 198

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



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/technical_graph/graph_axis.rb', line 234

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



185
186
187
188
189
190
191
# File 'lib/technical_graph/graph_axis.rb', line 185

def render_on_image(image)
  @image = image

  render_axis
  render_zero_axis
  render_axis_labels
end

#render_zero_axisObject

Render axis for zeros



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/technical_graph/graph_axis.rb', line 216

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



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

def truncate_string
  options[:truncate_string]
end

#value_axisObject

Where to put axis values



66
67
68
# File 'lib/technical_graph/graph_axis.rb', line 66

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/technical_graph/graph_axis.rb', line 139

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_y(ax).round
  bx = a[1]
  bx = image.calc_bitmap_y(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[:x_axis_min_distance] / axis_distance).ceil
    logger.debug "axis enlarged - width modified to #{options[:width]}"
  end
end

#x_axis_fixed?Boolean

Returns:

  • (Boolean)


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

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

#x_axis_intervalObject



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

def x_axis_interval
  options[:x_axis_interval]
end

#y_axis_distance_image_enlargeObject

Enlarge image to maintain proper axis density



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/technical_graph/graph_axis.rb', line 162

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[:y_axis_min_distance] / axis_distance).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)


49
50
51
# File 'lib/technical_graph/graph_axis.rb', line 49

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

#y_axis_intervalObject



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

def y_axis_interval
  options[:y_axis_interval]
end