Module: GraphHelper

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

Instance Method Summary collapse

Instance Method Details

#add_to_image_map(x1, y1, x2, y2, serie_name, value, caller_function) ⇒ Object

Add a box into the image map Internal function



408
409
410
411
412
413
# File 'lib/graph_helper.rb', line 408

def add_to_image_map(x1,y1,x2,y2,serie_name,value,caller_function)
  if ( @map_function == nil || @map_function == caller_function )
    @image_map  << (x1.round).to_s+","+(y1.round).to_s+","+(x2.round).to_s+","+(y2.round).to_s+","+serie_name+","+value.to_s
    @map_function = caller_function
  end
end

#draw_alpha_pixel(x, y, alpha, r, g, b) ⇒ Object

This function will draw an alpha pixel at position (x,y). alpha is used to specify the transparency factor ( between 0 and 100 ) The last 3 parameters are used to set the pixel color.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/graph_helper.rb', line 304

def draw_alpha_pixel(x,y,alpha,r,g,b)
  b, g, r = validate_color(b, g, r)
  if ( x < 0 || y < 0 || x >= @x_size || y >= @y_size )
    #eturn(-1)
    #TODO check image_color_at method is right?
  else
    rgb2= image_color_at(@picture, x, y)
    r2   = (rgb2 >> 16) & 0xFF
    g2   = (rgb2 >> 8) & 0xFF
    b2   = rgb2 & 0xFF
    i_alpha = (100 - alpha)/100
    alpha  = alpha / 100
    ra   = (r*alpha+r2*i_alpha).floor
    ga   = (g*alpha+g2*i_alpha).floor
    ba   = (b*alpha+b2*i_alpha).floor
    image_set_pixel(@picture,x,y,ra,ga,ba)
  end
end

#draw_antialias_pixel(x, y, r, g, b, alpha = 100, no_fall_back = false) ⇒ Object

Private functions for internal processing Internal function.



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/graph_helper.rb', line 360

def draw_antialias_pixel(x,y,r,g,b,alpha=100,no_fall_back=false)
  #Process shadows
  if(@shadow_active && !no_fall_back)
    draw_antialias_pixel(x+@shadow_x_distance,y+@shadow_y_distance,@shadow_r_color,@shadow_g_color,@shadow_b_color,@shadow_alpha,true)
    if(@shadow_blur != 0)
      alpha_decay = (@shadow_alpha*1.0 / @shadow_blur)
      i=1
      while i<=@shadow_blur
        draw_antialias_pixel(x+@shadow_x_distance-i/2,y+@shadow_y_distance-i/2,@shadow_r_color,@shadow_g_color,@shadow_b_color,@shadow_alpha-alpha_decay*i,true)
        i = i+1
      end
      i =1
      while i<=@shadow_blur
        draw_antialias_pixel(x+@shadow_x_distance+i/2,y+@shadow_y_distance+i/2,@shadow_r_color,@shadow_g_color,@shadow_b_color,@shadow_alpha-alpha_decay*i,true)
        i = i+1
      end
    end
  end
  b, g, r = validate_color(b, g, r)
  plot = ""
  xi   = x.floor rescue 0
  yi   = y.floor rescue 0
  if ( xi == x && yi == y)
    if ( alpha == 100 )
      image_set_pixel(@picture,x,y,r,g,b)
    else
      draw_alpha_pixel(x,y,alpha,r,g,b)
    end
  else
    if xi > 0 || yi > 0 #soe error occures therefor added condtion
      alpha1 = (((1 - (x - x.floor)) * (1 - (y - y.floor)) * 100) / 100) * alpha
      draw_alpha_pixel(xi,yi,alpha1,r,g,b) if alpha1 > @anti_alias_quality
      alpha2 = (((x -  x.floor) * (1 - (y - y.floor)) * 100) / 100) * alpha
      draw_alpha_pixel(xi+1,yi,alpha2,r,g,b) if alpha2 > @anti_alias_quality
      alpha3 = (((1 - (x -  x.floor)) * (y - y.floor) * 100) / 100) * alpha
      draw_alpha_pixel(xi,yi+1,alpha3,r,g,b) if alpha3 > @anti_alias_quality
      alpha4 = (((x -  x.floor) * (y - y.floor) * 100) / 100) * alpha
      draw_alpha_pixel(xi+1,yi+1,alpha4,r,g,b)  if alpha4 > @anti_alias_quality
    end
  end
end

#draw_background(r, g, b) ⇒ Object

This function can be used to set the background color. The default graph background color is set to white.



4
5
6
7
# File 'lib/graph_helper.rb', line 4

def draw_background(r,g,b)
  b,g,r= validate_color(b, g, r)
  image_filled_rectangle(@picture,0,0,@x_size,@y_size,r,g,b)
end

#draw_circle(xc, yc, height, r, g, b, width = 0) ⇒ Object

This function draw an aliased circle at position (xc,yc) with the specified radius. The last 3 parameters are used to set the border color. Width is used to draw ellipses.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/graph_helper.rb', line 215

def draw_circle(xc,yc,height,r,g,b,width=0)
  width = height if ( width == 0 )
  b, g, r = validate_color(b, g, r)

  c_circle = allocate_color(@picture,r,g,b)
  step     = 360 / (2 * 3.1418 * [width,height].max)
  i =0
  while(i<=360)
    x= Math.cos(i*3.1418/180) * height + xc
    y = Math.sin(i*3.1418/180) * width + yc
    draw_antialias_pixel(x,y,r,g,b)
    i = i+step
  end
end

#draw_dotted_line(x1, y1, x2, y2, dot_size, r, g, b, graph_function = false) ⇒ Object

This function will draw an aliased dotted line between points (x1,y1) and (x2,y2). Parameter #5 is used to specify the dot size ( 2 will draw 1 point every 2 points ) The last 3 parameters are used to set the line color.



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/graph_helper.rb', line 326

def draw_dotted_line(x1,y1,x2,y2,dot_size,r,g,b,graph_function=false)
  b, g, r = validate_color(b, g, r)
  distance = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
  x_step = (x2-x1) / distance
  y_step = (y2-y1) / distance
  dot_index = 0
  i = 0
  start_offset = 0

  while(i<=distance)
    x = i * x_step  + x1
    y = i * y_step + y1
    if ( dot_index <= dot_size)
      if ( (x >= @g_area_x1 && x <= @g_area_x2 && y >= @g_area_y1 && y <= @g_area_y2) || !graph_function )
        if (@line_width == 1 )
          draw_antialias_pixel(x,y,r,g,b)
        else
          start_offset = start_offset -(@line_width/2)
          end_offset = (@line_width/2)
          j = start_offset
          while(j<= end_offset)
            draw_antialias_pixel(x+j,y+j,r,g,b)
            j= j+1
          end
        end
      end
    end
    dot_index = dot_index+1
    dot_index = 0 if (dot_index == dot_size * 2)
    i= i+1
  end

end

#draw_ellipse(xc, yc, height, width, r, g, b) ⇒ Object

This function draw an aliased ellipse at position (xc,yc) with the specified height and width. The last 3 parameters are used to set the border color.



252
253
254
# File 'lib/graph_helper.rb', line 252

def draw_ellipse(xc,yc,height,width,r,g,b)
  draw_circle(xc,yc,height,r,g,b,width)
end

#draw_filled_circle(xc, yc, height, r, g, b, width = 0) ⇒ Object

This function draw a filled aliased circle at position (xc,yc) with the specified radius. The last 3 parameters are used to set the border and filling color. Width is used to draw ellipses.



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/graph_helper.rb', line 233

def draw_filled_circle(xc,yc,height,r,g,b,width=0)
  width = height if ( width == 0 )
  b, g, r = validate_color(b, g, r)
  step     = 360 / (2 * 3.1418 * [width,height].max)
  i =90
  while i<=270
    x1 = Math.cos(i*3.1418/180) * height + xc
    y1 = Math.sin(i*3.1418/180) * width + yc
    x2 = Math.cos((180-i)*3.1418/180) * height + xc
    y2 = Math.sin((180-i)*3.1418/180) * width + yc
    draw_antialias_pixel(x1-1,y1-1,r,g,b)
    draw_antialias_pixel(x2-1,y2-1,r,g,b)
    image_line(@picture,x1,y1-1,x2-1,y2-1,r,g,b) if ( (y1-1) > yc - [width,height].max )
    i= i+step
  end
end

#draw_filled_ellipse(xc, yc, height, width, r, g, b) ⇒ Object

This function draw a filled aliased ellipse at position (xc,yc) with the specified height and width. The last 3 parameters are used to set the border and filling color.



259
260
261
# File 'lib/graph_helper.rb', line 259

def draw_filled_ellipse(xc,yc,height,width,r,g,b)
  draw_filled_circle(xc,yc,height,r,g,b,width)
end

#draw_filled_rectangle(x1, y1, x2, y2, r, g, b, draw_border = true, alpha = 100, no_fall_back = false) ⇒ Object

This function draw an aliased filled rectangle The upper left and bottom right border positions are used as first 4 arguments. The last R,G,B parameters are used to set the border color. You can specify if the aliased border will be drawn and the transparency.



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/graph_helper.rb', line 83

def draw_filled_rectangle(x1, y1, x2, y2, r, g, b, draw_border=true, alpha=100,no_fall_back=false)
  x1, x2 = x2, x1  if x2.to_f < x1.to_f
  y1, y2 = y2, y1   if y2.to_f < y1.to_f
  b,g,r=validate_color(b, g, r)

  if(alpha == 100)
    #Process shadows
    if(@shadow_active && no_fall_back)
      draw_filled_rectangle(x1+@shadow_x_distance,y1+@shadow_y_distance,x2+@shadow_x_distance,y2+@shadow_y_distance,@shadow_r_color,@shadow_g_color,@shadow_b_color,false,@shadow_alpha,true)
      if(@shadow_blur != 0)
        alpha_decay = (@shadow_alpha/ @shadow_blur)
        i =1
        while i<=@shadow_blur
         draw_filled_rectangle(x1+@shadow_x_distance-i/2,y1+@shadow_y_distance-i/2,x2+@shadow_x_distance-i/2,y2+@shadow_y_distance-i/2,@shadow_r_color,@shadow_g_color,@shadow_b_color,false,@shadow_alpha-alpha_decay*i,true)
          i = i+1
        end
        i = 1
        while i<=@shadow_blur
         draw_filled_rectangle(x1+@shadow_x_distance+i/2,y1+@shadow_y_distance+i/2,x2+@shadow_x_distance+i/2,y2+@shadow_y_distance+i/2,@shadow_r_color,@shadow_g_color,@shadow_b_color,false,@shadow_alpha-alpha_decay*i,true)
          i = i+1
        end
      end
    end
    image_filled_rectangle(@picture,x1.to_f.round,y1.to_f.round,x2.to_f.round,y2.to_f.round,r,g,b)
  else
    layer_width  = (x2-x1).abs+2
    layer_height = (y2-y1).abs+2
    @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)
    image_filled_rectangle(@layers[0],1.round,1.round,(layer_width-1).round,(layer_height-1).round,r,g,b)
    image_copy_merge(@layers[0],@picture,([x1,x2].min-1).round,([y1,y2].min-1).round,0,0,layer_width,layer_height,alpha)
    #TODO Find out equivalent method
    image_destroy(@layers[0])
  end
  if (draw_border )
    shadow_settings = @shadow_active
    @shadow_active = false
    draw_rectangle(x1,y1,x2,y2,r,g,b)
    @shadow_active = shadow_settings
  end
end

#draw_filled_rounded_rectangle(x1, y1, x2, y2, radius, r, g, b, draw_border = true, alpha = 100) ⇒ Object

This function draw an aliased filled rectangle with rounded corners The upper left and bottom right border positions are used as first 4 arguments. Argument #5 represents the radius of the rounded corner. The last 3 parameters are used to set the border color.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/graph_helper.rb', line 170

def draw_filled_rounded_rectangle(x1, y1, x2, y2, radius,r, g, b, draw_border=true, alpha=100)
  b, g, r = validate_color(b, g, r)
  c_rectangle = allocate_color(@picture,r,g,b)

  step = 90 / ((3.1418 * radius)/2)
  i=0
  while i<=90
    xi1 = Math.cos((i+180)*3.1418/180) * radius + x1 + radius
    yi1 = Math.sin((i+180)*3.1418/180) * radius + y1 + radius

    xi2 = Math.cos((i-90)*3.1418/180) * radius + x2 - radius
    yi2 = Math.sin((i-90)*3.1418/180) * radius + y1 + radius

    xi3 = Math.cos((i)*3.1418/180) * radius + x2 - radius
    yi3 = Math.sin((i)*3.1418/180) * radius + y2 - radius

    xi4 = Math.cos((i+90)*3.1418/180) * radius + x1 + radius
    yi4 = Math.sin((i+90)*3.1418/180) * radius + y2 - radius

    image_line(@picture,xi1,yi1,x1+radius,yi1,r,g,b)
    image_line(@picture,x2-radius,yi2,xi2,yi2,r,g,b)
    image_line(@picture,x2-radius,yi3,xi3,yi3,r,g,b)
    image_line(@picture,xi4,yi4,x1+radius,yi4,r,g,b)

    draw_antialias_pixel(xi1,yi1,r,g,b)
    draw_antialias_pixel(xi2,yi2,r,g,b)
    draw_antialias_pixel(xi3,yi3,r,g,b)
    draw_antialias_pixel(xi4,yi4,r,g,b)
    i=i+step
  end
  image_filled_rectangle(@picture,x1,y1+radius,x2,y2-radius,r,g,b)
  image_filled_rectangle(@picture,x1+radius,y1,x2-radius,y2,r,g,b)

  x1=x1-0.2
  y1=y1-0.2
  x2=x2+0.2
  y2=y2+0.2
  draw_line(x1+radius,y1,x2-radius,y1,r,g,b)
  draw_line(x2,y1+radius,x2,y2-radius,r,g,b)
  draw_line(x2-radius,y2,x1+radius,y2,r,g,b)
  draw_line(x1,y2-radius,x1,y1+radius,r,g,b)
end

#draw_graph_area_gradient(r, g, b, decay, target = Rchart::TARGET_GRAPHAREA) ⇒ Object

You can use this function to fill the background of the picture or of the graph area with a color gradient pattern. You must specify the starting color with its r,g,b values, the number of shades to apply with the decay parameter and optionnaly the target that can be :

  • Rchart

    TARGET_GRAPHAREA The currently defined graph area

  • Rchart

    TARGET_BACKGROUND The whole picture background



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/graph_helper.rb', line 13

def draw_graph_area_gradient(r,g,b,decay,target=Rchart::TARGET_GRAPHAREA)
  b, g, r = validate_color(b, g, r)
  x1,y1,x2,y2 = 0,0,0,0
  if ( target == Rchart::TARGET_GRAPHAREA )
    x1 = @g_area_x1+1
    x2 = @g_area_x2-1
    y1 = @g_area_y1+1
    y2 = @g_area_y2
  end

  if ( target == Rchart::TARGET_BACKGROUND )
    x1 = 0
    x2 = @x_size
    y1 = 0
    y2 = @y_size
  end
  #Positive gradient
  if ( decay > 0 )
    y_step = (y2 - y1 - 2) / decay
    i=0
    while i<=decay
      r-=1
      g-=1
      b-=1
      yi1 = y1 + ( i * y_step )
      yi2 = ( yi1 + ( i * y_step ) + y_step ).ceil
      yi2 = y2-1 if ( yi2 >= yi2 )
      image_filled_rectangle(@picture,x1,yi1,x2,yi2,r,g,b)
      i=i+1
    end
  end
  # Negative gradient
  if ( decay < 0 )
    y_step = (y2 - y1 - 2) / -decay
    yi1   = y1
    yi2   = y1+y_step
    i= -decay
    while i>=0
      r+=1
      g+=1
      b+=1
      image_filled_rectangle(@picture,x1,yi1,x2,yi2,r,g,b)
      yi1+= y_step
      yi2+= y_step
      yi2 = y2-1 if ( yi2 >= yi2 )
      i= i-1
    end

  end
end

#draw_line(x1, y1, x2, y2, r, g, b, graph_function = false) ⇒ Object

This function will draw an aliased line between points (x1,y1) and (x2,y2). The last 3 parameters are used to set the line color. The last optional parameter is used for internal calls made by graphing function.If set to true, only portions of line inside the graph area will be drawn.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/graph_helper.rb', line 267

def draw_line(x1,y1,x2,y2,r,g,b,graph_function=false)
  if ( @line_dot_size > 1 )
    draw_dotted_line(x1,y1,x2,y2,@line_dot_size,r,g,b,graph_function)
  else
    b, g, r = validate_color(b, g, r)
    distance = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)) rescue 0
    if ( distance == 0 )
      return -1
    else
      x_step = (x2-x1) / distance
      y_step = (y2-y1) / distance
      i =0
      while i<=distance
        x = i * x_step + x1
        y = i * y_step + y1
        if((x >= @g_area_x1.to_f && x <= @g_area_x2.to_f && y >= @g_area_y1.to_f && y <= @g_area_y2.to_f) || !graph_function )
          if ( @line_width == 1 )
            draw_antialias_pixel(x,y,r,g,b)
          else
            start_offset = -(@line_width/2)
            end_offset = (@line_width/2)
            j = start_offset

            while j<=end_offset
              draw_antialias_pixel(x+j,y+j,r,g,b)
              j+=1
            end
          end
        end
        i =i+1
      end
    end
  end
end

#draw_rectangle(x1, y1, x2, y2, r, g, b) ⇒ Object

This function draw an aliased rectangle The upper left and bottom right border positions are used as first 4 arguments. The last 3 parameters are used to set the border color



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/graph_helper.rb', line 67

def draw_rectangle(x1, y1, x2, y2, r, g, b)
  b, g, r = validate_color(b, g, r)
  c_rectangle =  allocate_color(@picture,r, g, b)
  x1=x1-0.2
  y1=y1-0.2
  x2=x2+0.2
  y2=y2+0.2
  draw_line(x1,y1,x2,y1,r,g,b)
  draw_line(x2,y1,x2,y2,r,g,b)
  draw_line(x2,y2,x1,y2,r,g,b)
  draw_line(x1,y2,x1,y1,r,g,b)
end

#draw_rounded_rectangle(x1, y1, x2, y2, radius, r, g, b) ⇒ Object

This function draw an aliased rectangle with rounded corners The upper left and bottom right border positions are used as first 4 arguments. Argument #5 represents the radius of the rounded corner. The last 3 parameters are used to set the border color.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/graph_helper.rb', line 131

def draw_rounded_rectangle(x1, y1, x2, y2, radius,r, g, b)
  b, g, r = validate_color(b, g, r)

  c_rectangle = allocate_color(@picture,r,g,b)

  step = 90 / ((3.1418 * radius)/2)
  i=0
  while i<=90
    x = Math.cos((i+180)*3.1418/180) * radius + x1 + radius
    y = Math.sin((i+180)*3.1418/180) * radius + y1 + radius
   draw_antialias_pixel(x,y,r,g,b)

    x = Math.cos((i-90)*3.1418/180) * radius + x2 - radius
    y = Math.sin((i-90)*3.1418/180) * radius + y1 + radius
    draw_antialias_pixel(x,y,r,g,b)

    x = Math.cos((i)*3.1418/180) * radius + x2 - radius
    y = Math.sin((i)*3.1418/180) * radius + y2 - radius
    draw_antialias_pixel(x,y,r,g,b)

    x = Math.cos((i+90)*3.1418/180) * radius + x1 + radius
    y = Math.sin((i+90)*3.1418/180) * radius + y2 - radius
    draw_antialias_pixel(x,y,r,g,b)
    i=i+step
  end

  x1=x1-0.2
  y1=y1-0.2
  x2=x2+0.2
  y2=y2+0.2
  draw_line(x1+radius,y1,x2-radius,y1,r,g,b)
  draw_line(x2,y1+radius,x2,y2-radius,r,g,b)
  draw_line(x2-radius,y2,x1+radius,y2,r,g,b)
  draw_line(x1,y2-radius,x1,y1+radius,r,g,b)
end

#set_image_map(mode = true, graph_id = "MyGraph") ⇒ Object

Activate the image map creation process Internal function



403
404
405
406
# File 'lib/graph_helper.rb', line 403

def set_image_map(mode=true,graph_id="MyGraph")
  @build_map = mode
  @map_id    = graph_id
end