Module: Layout

Included in:
Graph
Defined in:
lib/layout.rb

Instance Method Summary collapse

Instance Method Details

#add_border(size = 3, r = 0, g = 0, b = 0) ⇒ Object

You can specify the size of the border and its color. The width and height of the picture will be modified by 2x the size value.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/layout.rb', line 191

def add_border(size=3,r=0,g=0,b=0)
  width  = @x_size+2*size
  height = @y_size+2*size
  resampled    = image_create_true_color(width,height)
  image_filled_rectangle(resampled,0,0,width,height, r, g, b)
  image_copy(@picture,resampled,size,size,0,0,@x_size,@y_size)
  image_destroy(@picture)
  @x_size = width
  @y_size = height
  @picture = image_create_true_color(@x_size,@y_size)
  image_filled_rectangle(@picture,0,0,@x_size,@y_size,255,255,255)
  image_color_transparent(@picture,255,255,255)
  image_copy(resampled,@picture,0,0,0,0,@x_size,@y_size)
end

#draw_area(data, serie1, serie2, r, g, b, alpha = 50) ⇒ Object

This function will draw an area between two data series. extracting the minimum and maximum value for each X positions. You must specify the two series name and the area color. You can specify the transparency which is set to 50% by default.



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
181
182
183
184
185
186
187
# File 'lib/layout.rb', line 149

def draw_area(data,serie1,serie2,r,g,b,alpha = 50)
  validate_data("draw_area",data)
  layer_width = @g_area_x2-@g_area_x1
  layer_height = @g_area_y2-@g_area_y1

  @layers[0] = image_create_true_color(layer_width,layer_height)
  image_filled_rectangle(@layers[0],0,0,layer_width,layer_height,255,255,255)
  image_color_transparent(@layers[0],255,255,255)

  x_pos    = @g_area_x_offset
  last_x_pos = -1
  last_y_pos1 = nil
  last_y_pos2= nil
  data.each do |key|
    value1 = key[serie1]
    value2 = key[serie2]
    y_pos1  = layer_height - ((value1-@vmin) * @division_ratio)
    y_pos2  = layer_height - ((value2-@vmin) * @division_ratio)

    if ( last_x_pos != -1 )
      points   = []
      points << last_x_pos
      points << last_y_pos1
      points << last_x_pos
      points <<  last_y_pos2
      points << x_pos
      points << y_pos2
      points << x_pos
      points << y_pos1
      image_filled_polygon(@layers[0],points,r,g,b,4)
    end
    last_y_pos1 = y_pos1
    last_y_pos2 = y_pos2
    last_x_pos  = x_pos
    x_pos= x_pos+ @division_width
  end
  image_copy_merge(@layers[0],@picture,@g_area_x1,@g_area_y1,0,0,layer_width,layer_height,alpha)
  image_destroy(@layers[0])
end

#draw_graph_area(r, g, b, stripe = false) ⇒ Object

Prepare the graph area



13
14
15
16
17
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
# File 'lib/layout.rb', line 13

def draw_graph_area(r,g,b,stripe=false)
  draw_filled_rectangle(@g_area_x1,@g_area_y1,@g_area_x2,@g_area_y2,r,g,b,false)
  draw_rectangle(@g_area_x1,@g_area_y1,@g_area_x2,@g_area_y2,r-40,g-40,b-40)
  i=0
  if stripe
    r2 = r-15
    r2 = 0 if r2<0
    g2 = r-15
    g2 = 0  if g2 < 0
    b2 = r-15
    b2 = 0  if b2 < 0
    line_color = allocate_color(@picture,r2,g2,b2)
    skew_width = @g_area_y2-@g_area_y1-1

    i = @g_area_x1-skew_width

    while i.to_f<=@g_area_x2.to_f
      x1 = i
      y1 = @g_area_y2
      x2 = i+skew_width
      y2 = @g_area_y1
      if ( x1 < @g_area_x1 )
        x1 = @g_area_x1
        y1 = @g_area_y1 + x2 - @g_area_x1 + 1
      end
      if ( x2 >= @g_area_x2 )
        y2 = @g_area_y1 + x2 - @g_area_x2 +1
        x2 = @g_area_x2 - 1
      end
      image_line(@picture,x1,y1,x2,y2+1,r2,g2,b2)
      i = i+4
    end

  end
end

#draw_grid(line_width, mosaic = true, r = 220, g = 220, b = 220, alpha = 100) ⇒ Object

This function will draw a grid over the graph area. line_width will be passed to the draw_dotted_line function. The r,g,b 3 parameters are used to set the grid color. Setting mosaic to true will draw grey area between two lines. You can define the transparency factor of the mosaic area playing with the alpha parameter.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/layout.rb', line 54

def draw_grid(line_width,mosaic=true,r=220,g=220,b=220,alpha=100)
  # Draw mosaic */
  if (mosaic)
    layer_width  = @g_area_x2-@g_area_x1
    layer_height = @g_area_y2-@g_area_y1

    @layers[0] = image_create_true_color(layer_width,layer_height)
    #c_white         = allocate_color(@layers[0],255,255,255)
    image_filled_rectangle(@layers[0],0,0,layer_width,layer_height,255,255,255)
    image_color_transparent(@layers[0],255,255,255)

    #c_rectangle =allocate_color(@layers[0],250,250,250)

    y_pos  = layer_height #@g_area_y2-1
    last_y = y_pos
    i =0
    while(i<=@division_count)
      last_y=  y_pos
      y_pos  =  y_pos - @division_height
      y_pos = 1 if (  y_pos <= 0 )
      image_filled_rectangle(@layers[0],1, y_pos,layer_width-1,last_y,250,250,250) if ( i % 2 == 0 )
      i = i+1
    end

    image_copy_merge(@layers[0],@picture,@g_area_x1,@g_area_y1,0,0,layer_width,layer_height,alpha)
    #image_destroy(@layers[0])

  end

  #Horizontal lines
  y_pos = @g_area_y2 - @division_height
  i=1
  while(i<=@division_count)
    self.draw_dotted_line(@g_area_x1,y_pos,@g_area_x2,y_pos,line_width,r,g,b) if ( y_pos > @g_area_y1 && y_pos < @g_area_y2 )
    y_pos = y_pos - @division_height
    i = i+1
  end
  # Vertical lines
  if (@g_area_x_offset == 0 )
    x_pos = @g_area_x1 + (@division_width) +@g_area_x_offset
    col_count = (@data_count.to_f-2).floor
  else

    x_pos = @g_area_x1 +@g_area_x_offset
    col_count = ( (@g_area_x2 - @g_area_x1) / @division_width )
  end
  i= 1

  while (i<=col_count)
    if ( x_pos > @g_area_x1 && x_pos < @g_area_x2 )
      self.draw_dotted_line((x_pos).floor,@g_area_y1,(x_pos).floor,@g_area_y2,line_width,r,g,b)
    end
    x_pos = x_pos + @division_width
    i= i+1
  end
end

#draw_treshold(value, r, g, b, show_label = false, show_on_right = false, tick_width = 4, free_text = nil) ⇒ Object

This function will draw an horizontal treshold ( this is an easy way to draw the 0 line ). If show_label is set to true, the value of the treshold will be written over the graph. If show_on_right is set to true, the value will be written on the right side of the graph. r, g and b are used to set the line and text color. Use tick_width to set the width of the ticks, if set to 0 this will draw a solid line. You can optionnaly provide the caption of the treshold (by default the treshold value is used)



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/layout.rb', line 117

def draw_treshold(value,r,g,b,show_label=false,show_on_right=false,tick_width=4,free_text=nil)
  b, g, r = validate_color(b, g, r)

  c_text_color =allocate_color(@picture,r,g,b)
  # c_text_color = GD2::Color.new(r,g,b)
  y = @g_area_y2 - (value - @vmin.to_f) * @division_ratio.to_f

  return(-1) if ( y <= @g_area_y1 || y >= @g_area_y2 )
  if ( tick_width == 0 )
    self.draw_line(@g_area_x1,y,@g_area_x2,y,r,g,b)
  else
    self.draw_dotted_line(@g_area_x1,y,@g_area_x2,y,tick_width,r,g,b)
  end
  if (show_label )
    if ( free_text.nil? )
      label = value
    else
      label = free_text
    end

    if ( show_on_right )
      image_ttf_text(@picture,@font_size,0,@g_area_x2+2,y+(@font_size/2),c_text_color,@font_name,label.to_s)
    else
      image_ttf_text(@picture,@font_size,0,@g_area_x1+2,y-(@font_size/2),c_text_color,@font_name,label.to_s)
    end
  end
end

#report_warnings(interface = "cli") ⇒ Object

By default messages are redirected to the console while using the render command and using GD while using the stroke command. You can force the errors to be redirected to either cli or gd specifying it as parameter.



243
244
245
246
# File 'lib/layout.rb', line 243

def report_warnings(interface="cli")
	@error_reporting = true
	@error_interface = interface
end

#set_graph_area(x1, y1, x2, y2) ⇒ Object

A call to this function is mandatory when creating a graph. The upper left and bottom right border positions are used as arguments. This area will be used to draw graphs, grid, axis & more. Calling this function will not draw anything this will only set the graph area boundaries.



6
7
8
9
10
11
# File 'lib/layout.rb', line 6

def set_graph_area(x1,y1,x2,y2)
  @g_area_x1 = x1
  @g_area_y1 = y1
  @g_area_x2 = x2
  @g_area_y2 = y2
end

#write_values(data, data_description, series) ⇒ Object

It is possible to specify one or multiple series to display using and array.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/layout.rb', line 207

def write_values(data,data_description,series)

data_description = self.validate_data_description("write_values",data_description)
 validate_data("write_values",data)
series = [series]    if ( !series.is_a?(Array))
id = 0
color_id =0
series.each do |col_name|
	data_description["description"].each do |key_i,value_i|
		if ( key_i == col_name )
			color_id = id
			id = id+1
		end
	end
	xpos  = @g_area_x1 + @g_area_x_offset
	xlast = -1
	data.each do |key|
		if ((!key[col_name].nil?) && (key[col_name].is_a?(Numeric)))
			value = key[col_name]
			ypos = @g_area_y2 - ((value-@vmin) * @division_ratio)
			positions = image_ftb_box(@font_size,0,@font_name,value.to_s)
			width  = positions[2] - positions[6]
			x_offset = xpos - (width/2)
			height = positions[3] - positions[7]
			y_offset = ypos - 4

			c_text_color = allocate_color(@picture,@palette[color_id]["r"],@palette[color_id]["g"],@palette[color_id]["b"]);
			image_ttf_text(@picture,@font_size,0,x_offset,y_offset,c_text_color,@font_name,value.to_s)
		end
		xpos = xpos + @division_width
	end
end
end