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

#antialiasObject



160
161
162
# File 'lib/technical_graph/graph_axis.rb', line 160

def antialias
  options[:antialias] == true
end

#axis_distance_image_enlargeObject

Enlarge image to maintain proper axis density



100
101
102
103
104
105
106
107
108
109
# File 'lib/technical_graph/graph_axis.rb', line 100

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/technical_graph/graph_axis.rb', line 72

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
    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
    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

#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



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

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



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

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



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

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



152
153
154
155
156
157
158
# File 'lib/technical_graph/graph_axis.rb', line 152

def render_on_image(image)
  @image = image

  render_axis
  render_zero_axis
  render_axis_labels
end

#render_zero_axisObject

Render axis for zeros



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/technical_graph/graph_axis.rb', line 183

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



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

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



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

def x_axis_distance_image_enlarge
  a = parameter_axis
  # must be at least 2 axis
  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

  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
  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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/technical_graph/graph_axis.rb', line 132

def y_axis_distance_image_enlarge
  a = value_axis
  # must be at least 2 axis
  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

  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
  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