Class: Graphit::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/graphit/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, data) ⇒ Graph

Returns a new instance of Graph.



18
19
20
21
22
23
24
25
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/graphit/graph.rb', line 18

def initialize( options, data )
  
  options.each do |k,v|
    if self.respond_to?("#{k}=".to_sym)
      self.send( "#{k}=".to_sym, v )
    end
  end
  
  self.graph_data = data
  
  recalculate_xmin if xmin.to_s == "auto"
  recalculate_xmax if xmax.to_s == "auto"
  recalculate_ymin if ymin.to_s == "auto"
  recalculate_ymax if ymax.to_s == "auto"
  
  # Ensure floats
  self.xmin = self.xmin.to_f
  self.xmax = self.xmax.to_f
  self.ymin = self.ymin.to_f
  self.ymax = self.ymax.to_f
  
  recalculate_pixels_per_unit      
        
  puts "BGColor: #{options[:background_color]}"
        
  self.bitmap_drawing = BitmapDrawing.new( options[:height], options[:width], options[:background_color] )

  draw_horizontal_gridlines
  draw_vertical_gridlines
  draw_graph_outline
  draw_xaxis_labels

  draw_graph( data )
end

Instance Attribute Details

#background_colorObject

Returns the value of attribute background_color.



11
12
13
# File 'lib/graphit/graph.rb', line 11

def background_color
  @background_color
end

#bitmap_drawingObject

Returns the value of attribute bitmap_drawing.



4
5
6
# File 'lib/graphit/graph.rb', line 4

def bitmap_drawing
  @bitmap_drawing
end

#bottom_paddingObject

Returns the value of attribute bottom_padding.



14
15
16
# File 'lib/graphit/graph.rb', line 14

def bottom_padding
  @bottom_padding
end

#data_colorObject

Returns the value of attribute data_color.



16
17
18
# File 'lib/graphit/graph.rb', line 16

def data_color
  @data_color
end

#debugObject

Returns the value of attribute debug.



13
14
15
# File 'lib/graphit/graph.rb', line 13

def debug
  @debug
end

#graph_dataObject

Returns the value of attribute graph_data.



6
7
8
# File 'lib/graphit/graph.rb', line 6

def graph_data
  @graph_data
end

#heightObject

Returns the value of attribute height.



8
9
10
# File 'lib/graphit/graph.rb', line 8

def height
  @height
end

#left_paddingObject

Returns the value of attribute left_padding.



14
15
16
# File 'lib/graphit/graph.rb', line 14

def left_padding
  @left_padding
end

#right_paddingObject

Returns the value of attribute right_padding.



14
15
16
# File 'lib/graphit/graph.rb', line 14

def right_padding
  @right_padding
end

#top_paddingObject

Returns the value of attribute top_padding.



14
15
16
# File 'lib/graphit/graph.rb', line 14

def top_padding
  @top_padding
end

#verboseObject

Returns the value of attribute verbose.



12
13
14
# File 'lib/graphit/graph.rb', line 12

def verbose
  @verbose
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/graphit/graph.rb', line 8

def width
  @width
end

#x_pixels_per_unitObject

Returns the value of attribute x_pixels_per_unit.



15
16
17
# File 'lib/graphit/graph.rb', line 15

def x_pixels_per_unit
  @x_pixels_per_unit
end

#xmaxObject

Returns the value of attribute xmax.



9
10
11
# File 'lib/graphit/graph.rb', line 9

def xmax
  @xmax
end

#xminObject

Returns the value of attribute xmin.



9
10
11
# File 'lib/graphit/graph.rb', line 9

def xmin
  @xmin
end

#xticsmodObject

Returns the value of attribute xticsmod.



9
10
11
# File 'lib/graphit/graph.rb', line 9

def xticsmod
  @xticsmod
end

#y_pixels_per_unitObject

Returns the value of attribute y_pixels_per_unit.



15
16
17
# File 'lib/graphit/graph.rb', line 15

def y_pixels_per_unit
  @y_pixels_per_unit
end

#ymaxObject

Returns the value of attribute ymax.



10
11
12
# File 'lib/graphit/graph.rb', line 10

def ymax
  @ymax
end

#yminObject

Returns the value of attribute ymin.



10
11
12
# File 'lib/graphit/graph.rb', line 10

def ymin
  @ymin
end

#yticsmodObject

Returns the value of attribute yticsmod.



10
11
12
# File 'lib/graphit/graph.rb', line 10

def yticsmod
  @yticsmod
end

Instance Method Details

#draw_graph(data) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/graphit/graph.rb', line 124

def draw_graph( data )
  self.graph_data = data
  
  recalculate_pixels_per_unit
  
  data.each_with_index do |p, i|
    data[i] = translate_data_point_to_graph_point( p )
  end
  
  data.each_with_index do |point, i|
    if i < data.size - 1
      x1 = point.x
      y1 = point.y

      x2 = data[i+1].x
      y2 = data[i+1].y
  
      self.bitmap_drawing.draw_line( Point.new( x1, y1 ), Point.new( x2, y2), self.data_color )
    end
  end
end

#draw_graph_outlineObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/graphit/graph.rb', line 178

def draw_graph_outline
  p1 = translate_data_point_to_graph_point( Point.new( self.xmin, self.ymin ) )
  p2 = translate_data_point_to_graph_point( Point.new( self.xmax, self.ymin ) )
  self.bitmap_drawing.draw_line( Point.new( p1.x, p1.y ), Point.new( p2.x, p2.y ), [0x22,0x22,0x22] )
  
  p1 = translate_data_point_to_graph_point( Point.new( self.xmax, self.ymin ) )
  p2 = translate_data_point_to_graph_point( Point.new( self.xmax, self.ymax ) )
  self.bitmap_drawing.draw_line( Point.new( p1.x, p1.y ), Point.new( p2.x, p2.y ), [0x22,0x22,0x22] )
  
  p1 = translate_data_point_to_graph_point( Point.new( self.xmax, self.ymax ) )
  p2 = translate_data_point_to_graph_point( Point.new( self.xmin, self.ymax ) )
  self.bitmap_drawing.draw_line( Point.new( p1.x, p1.y ), Point.new( p2.x, p2.y ), [0x22,0x22,0x22] )
  
  p1 = translate_data_point_to_graph_point( Point.new( self.xmin, self.ymax ) )
  p2 = translate_data_point_to_graph_point( Point.new( self.xmin, self.ymin ) )
  self.bitmap_drawing.draw_line( Point.new( p1.x, p1.y ), Point.new( p2.x, p2.y ), [0x22,0x22,0x22] )
end

#draw_horizontal_gridlinesObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/graphit/graph.rb', line 146

def draw_horizontal_gridlines
  (self.ymin.to_i..self.ymax.to_i).each do |y|
    if y % self.yticsmod == 0
      p1 = translate_data_point_to_graph_point( Point.new( self.xmin, y ) )
      p2 = translate_data_point_to_graph_point( Point.new( self.xmax, y ) )
        
      self.bitmap_drawing.draw_line( Point.new( p1.x, p1.y ), Point.new( p2.x, p2.y ), [0xAA,0xAA,0xAA] )
  
      self.bitmap_drawing.draw_text( "#{y}", Point.new( p1.x - (10 * y.to_s.size), p1.y ), [0x00,0x00,0x00] )
    end
  end
end

#draw_vertical_gridlinesObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/graphit/graph.rb', line 159

def draw_vertical_gridlines
  (self.xmin.to_i..self.xmax.to_i).each do |x|
    if x % self.xticsmod == 0
      p1 = translate_data_point_to_graph_point( Point.new( x, self.ymin ) )
      p2 = translate_data_point_to_graph_point( Point.new( x, self.ymax ) )
  
      #Manual offset for GMT-7
      if (x + (3600*-7)) % 86400 == 0
        lcolor = [0x00,0x00,0xFF]
      else
        lcolor = [0xAA,0xAA,0xAA]
      end
  
  
      self.bitmap_drawing.draw_line( Point.new( p1.x, p1.y ), Point.new( p2.x, p2.y ), lcolor )
    end
  end
end

#draw_xaxis_labelsObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/graphit/graph.rb', line 196

def draw_xaxis_labels
  (self.xmin.to_i..self.xmax.to_i).each do |x|
  
    if x % 3600 == 0
      x_offset = translate_data_point_to_graph_point( Point.new( x, 10 ) ).x.to_i

      h = Time.at(x).hour

      if h % 2 == 0
        if self.debug
          puts "h: #{h.to_s}, x: #{x_offset}"
        end
        self.bitmap_drawing.draw_text( h.to_s, Point.new( x_offset, self.height - 15 ), [0x00,0x00,0x00] )
      end
    end
  end
end

#draw_yaxis_labelsObject



214
215
216
# File 'lib/graphit/graph.rb', line 214

def draw_yaxis_labels
  # Currently happening in the y gridlines method
end

#graph_heightObject



98
99
100
# File 'lib/graphit/graph.rb', line 98

def graph_height
  height - bottom_padding - top_padding
end

#graph_widthObject



94
95
96
# File 'lib/graphit/graph.rb', line 94

def graph_width
  width - left_padding - right_padding
end

#recalculate_pixels_per_unitObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/graphit/graph.rb', line 102

def recalculate_pixels_per_unit
  self.x_pixels_per_unit = graph_width / (xmax.to_f - xmin.to_f)
  self.y_pixels_per_unit = graph_height / (ymax.to_f - ymin.to_f)
  
  if xmax - xmin < (86400 * 3.5)
    self.xticsmod = 3600
  elsif xmax - xmin < (86400 * 7.5)
    self.xticsmod = (86400 * 24)
  elsif xmax - xmin < (86400 * 50)
    self.xticsmod = (86400 * 24 * 7)
  elsif xmax - xmin < (86400 * 800)
    self.xticsmod = (86400 * 24 * 30)
  else
    self.xticsmod = 3600
  end
  
  # if options[:debug]
  #   puts "@x_pixels_per_unit: #{@x_pixels_per_unit}"
  #   puts "@y_pixels_per_unit: #{@y_pixels_per_unit}"
  # end
end

#recalculate_xmaxObject



72
73
74
75
76
77
78
79
80
# File 'lib/graphit/graph.rb', line 72

def recalculate_xmax
  puts "recalculate_xmax" if self.debug
  
  self.xmax = self.graph_data.max_by{ |p| p.x.to_f }.x
  
  puts "  xmax: #{self.xmax}" if self.debug
  
  self.xmax
end

#recalculate_xminObject



62
63
64
65
66
67
68
69
70
# File 'lib/graphit/graph.rb', line 62

def recalculate_xmin
  puts "recalculate_xmin" if self.debug
  
  self.xmin = self.graph_data.min_by{ |p| p.x.to_f }.x
  
  puts "  xmin: #{self.xmin}" if self.debug
  
  self.xmin
end

#recalculate_ymaxObject



88
89
90
91
92
# File 'lib/graphit/graph.rb', line 88

def recalculate_ymax
  puts "recalculate_ymax" if self.debug
  
  self.ymax = self.graph_data.max_by{ |p| p.y.to_f }.y
end

#recalculate_yminObject



82
83
84
85
86
# File 'lib/graphit/graph.rb', line 82

def recalculate_ymin
  puts "recalculate_ymin" if self.debug
  
  self.ymin = self.graph_data.min_by{ |p| p.y.to_f }.y
end

#to_sObject



53
54
55
56
57
58
59
60
# File 'lib/graphit/graph.rb', line 53

def to_s
  s = super
  s += "\n"
  s += "  Height: #{height}, Width: #{width}\n"
  s += "  Xmin: #{xmin}, Xmax: #{xmax}, Xticsmod: #{xticsmod}\n"
  s += "  Ymin: #{ymin}, Ymax: #{ymax}, Yticsmod: #{yticsmod}\n"
  s
end

#translate_data_point_to_graph_point(point) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/graphit/graph.rb', line 218

def translate_data_point_to_graph_point( point )

  if point.x < self.xmin
    point.x = self.xmin
  elsif point.x > self.xmax
    point.x = self.xmax
  end
  
  if point.y < self.ymin
    point.y = self.ymin
  elsif point.y > self.ymax
    point.y = self.ymax
  end
  
  #puts "Mid x: #{point.x}, y: #{point.y}"
  
  x = self.left_padding + (point.x - self.xmin) * self.x_pixels_per_unit
  y = self.height - self.bottom_padding - ((point.y - self.ymin) * self.y_pixels_per_unit)
  
  #puts "Out x: #{x}, y: #{y}"
  
  return Point.new( x, y )
end