Module: Legend

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

Instance Method Summary collapse

Instance Method Details

#draw_legend(x_pos, y_pos, data_description, r, g, b, rs = -1,, gs = -1,, bs = -1,, rt = 0, gt = 0, bt = 0, border = true) ⇒ Object

This function will draw the legend of the graph ( serie color & serie name ) at the specified position. The r,g,bparameters are used to set the background color. You can optionally provide the shadow color using the rs,gs,bs parameters. You can also customize the text color using the rt,gt,bt. Setting Border to false remove the surrounding box.



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
63
64
65
# File 'lib/legend.rb', line 29

def draw_legend(x_pos,y_pos,data_description,r,g,b,rs=-1,gs=-1,bs=-1,rt=0,gt=0,bt=0,border=true)
  #Validate the Data and data_description array
  data_description = validate_data_description("draw_legend",data_description)
  return(-1) if (data_description["description"].nil?)
  c_text_color = allocate_color(@picture, rt, gt, bt)
  # <-10->[8]<-4->Text<-10->
  max_width = 0
  max_height = 8
  data_description["description"].each do |key,value|
    position   = image_ftb_box(@font_size,0,@font_name,value)
    text_width  = position[2]-position[0]
    text_height = position[1]-position[7]
    max_width = text_width if ( text_width > max_width)
    max_height = max_height + text_height + 4
  end
  max_height = max_height - 5
  max_width  = max_width + 32
  if ( rs == -1 || gs == -1 || bs == -1 )
    rs = r-30
    gs = g-30
    bs = b-30
  end
  if ( border )
    draw_filled_rounded_rectangle(x_pos+1,y_pos+1,x_pos+max_width+1,y_pos+max_height+1,5,rs,gs,bs)
    draw_filled_rounded_rectangle(x_pos,y_pos,x_pos+max_width,y_pos+max_height,5,r,g,b)
  end
  y_offset = 4 + @font_size
  id = 0
  data_description["description"].each do |key,value|
    draw_filled_rounded_rectangle(x_pos+10,y_pos+y_offset-4 , x_pos+14, y_pos+y_offset-4, 2, @palette[id]["r"], @palette[id]["g"], @palette[id]["b"])
    image_ttf_text(@picture, @font_size,0, x_pos+22, y_pos+y_offset, c_text_color, @font_name, value)
    position   = image_ftb_box(@font_size,0,@font_name,value)
    text_height = position[1]-position[7]
    y_offset = y_offset + text_height + 4
    id=id+1
  end
end

#draw_pie_legend(x_pos, y_pos, data, data_description, r, g, b) ⇒ Object

This function will draw the legend of a pie graph ( serie color & value name ). Be carrefull, dataset used for pie chart are not the same than for other line / curve / plot graphs. You can specify the position of the legend box and the background color.



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
# File 'lib/legend.rb', line 70

def draw_pie_legend(x_pos,y_pos,data,data_description,r,g,b)
  data_description = validate_data_description("draw_pie_legend",data_description,false)
  validate_data("draw_pie_legend",data)
  return(-1) if (data_description["position"].nil?)
  c_text_color = allocate_color(@picture,0,0,0)

  # <-10->[8]<-4->Text<-10-> */
  max_width = 0
  max_height = 8
  data.each do |key|
    value = key[data_description["position"]]
    position  = image_ftb_box(@font_size,0,@font_name,value)
    text_width = position[2]-position[0]
    text_height = position[1]-position[7]
    max_width = text_width if ( text_width > max_width)
    max_height = max_height + text_height + 4
  end
  max_height = max_height - 3
  max_width  = max_width + 32
  draw_filled_rounded_rectangle(x_pos+1,y_pos+1,x_pos+max_width+1,y_pos+max_height+1,5,r-30,g-30,b-30)
  draw_filled_rounded_rectangle(x_pos,y_pos,x_pos+max_width,y_pos+max_height,5,r,g,b)
  y_offset = 4 + @font_size
  id = 0
  data.each do |key|
    value     = key[data_description["position"]]
    position   = image_ftb_box(@font_size,0,@font_name,value)
    text_height = position[1]-position[7]
    draw_filled_rectangle(x_pos+10,y_pos+y_offset-6,x_pos+14,y_pos+y_offset-2,@palette[id]["r"],@palette[id]["g"],@palette[id]["b"])
    image_ttf_text(@picture,@font_size,0,x_pos+22,y_pos+y_offset,c_text_color,@font_name,value)
    y_offset = y_offset + text_height + 4
    id= id+1
  end
end

#draw_text_box(x1, y1, x2, y2, text, angle = 0, r = 255, g = 255, b = 255, align = Rchart::ALIGN_LEFT, shadow = true, bgr = -1,, bgg = -1,, bgb = -1,, alpha = 100) ⇒ Object

You must specify the coordinate of the box where the text will be written using the (x1,y1)-(x2,y2) parameters, the text angle and the text color with the r,g,b parameters. You can choose how the text will be aligned with the align parameter :

  • Rchart

    ALIGN_TOP_LEFT Use the box top left corner.

  • Rchart

    ALIGN_TOP_CENTER Use the box top center corner.

  • Rchart

    ALIGN_TOP_RIGHT Use the box top right corner.

  • Rchart

    ALIGN_LEFT Use the center left.

  • Rchart

    ALIGN_CENTER Use the center.

  • Rchart

    ALIGN_RIGHT Use the center right.

  • Rchart

    ALIGN_BOTTOM_LEFT Use the box bottom left corner.

  • Rchart

    ALIGN_BOTTOM_CENTER Use the box bottom center corner.

  • Rchart

    ALIGN_BOTTOM_RIGHT Use the box bottom right corner.



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
166
167
168
169
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
# File 'lib/legend.rb', line 140

def draw_text_box(x1,y1,x2,y2,text,angle=0,r=255,g=255,b=255,align=Rchart::ALIGN_LEFT,shadow=true,bgr=-1,bgg=-1,bgb=-1,alpha=100)
  position   = image_ftb_box(@font_size,angle,@font_name,text)
  text_width  = position[2]-position[0]
  text_height = position[5]-position[3]
  area_width  = x2 - x1
  area_height = y2 - y1
  x =nil
  y = nil

  if ( bgr != -1 && bgg != -1 && bgb != -1 )
    draw_filled_rectangle(x1,y1,x2,y2,bgr,bgg,bgb,false,alpha)
  end

  if ( align == Rchart::ALIGN_TOP_LEFT )
    x = x1+1
    y = y1+@font_size+1
  end

  if ( align == Rchart::ALIGN_TOP_CENTER )
    x = x1+(area_width/2)-(text_width/2)
    y = y1+@font_size+1
  end

  if ( align == Rchart::ALIGN_TOP_RIGHT )
    x = x2-text_width-1
    y = y1+@font_size+1
  end
  if ( align == Rchart::ALIGN_LEFT )
    x = x1+1
    y = y1+(area_height/2)-(text_height/2)
  end
  if ( align == Rchart::ALIGN_CENTER )
    x = x1+(area_width/2)-(text_width/2)
    y = y1+(area_height/2)-(text_height/2)
  end
  if ( align == Rchart::ALIGN_RIGHT )
    x = x2-text_width-1
    y = y1+(area_height/2)-(text_height/2)
  end
  if ( align == Rchart::ALIGN_BOTTOM_LEFT )
    x = x1+1
    y = y2-1
  end
  if ( align == Rchart::ALIGN_BOTTOM_CENTER )
    x = x1+(area_width/2)-(text_width/2)
    y = y2-1
  end
  if ( align == Rchart::ALIGN_BOTTOM_RIGHT )
    x = x2-text_width-1
    y = y2-1
  end
  c_text_color   =allocate_color(@picture,r,g,b)
  c_shadow_color =allocate_color(@picture,0,0,0)
  if ( shadow )
    image_ttf_text(@picture,@font_size,angle,x+1,y+1,c_shadow_color,@font_name,text)
  end

  image_ttf_text(@picture,@font_size,angle,x,y,c_text_color,@font_name,text)
end

#draw_title(x_pos, y_pos, value, r, g, b, x_pos2 = -1,, y_pos2 = -1,, shadow = false) ⇒ Object

This function is used to write the graph title. Used with default parameters you must specify the bottom left position of the text. if you are specifying x2 and y2 the text will be centered horizontaly and verticaly in the box of coordinates (x1,y1)-(x2,y2). value correspond to the text that will be written on the graph. r, g and b are used to set the text color. Setting shadow to true will makes a shadow behind the text.



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

def draw_title(x_pos,y_pos,value,r,g,b,x_pos2=-1,y_pos2=-1,shadow=false)
  c_text_color =   allocate_color(@picture, r, g, b)
  if ( x_pos2 != -1 )
    position  = image_ftb_box(@font_size,0,@font_name,value)
    text_width = position[2]-position[0]
    x_pos      =(( x_pos2 - x_pos -text_width ) / 2 ).floor + x_pos
  end
  if ( y_pos2 != -1 )
    position   = image_ftb_box(@font_size,0,@font_name,value)
    text_height = position[5]-position[3]
    y_pos       =(( y_pos2 - y_pos - text_height ) / 2 ).floor + y_pos
  end
  if ( shadow )
    c_shadow_color = allocate_color(@picture,@shadow_r_color,@shadow_g_color,@shadow_b_color)
    image_ttf_text(@picture,@font_size,0,x_pos+@shadow_x_distance,y_pos+@shadow_y_distance, c_shadow_color ,@font_name,value)
  end
  image_ttf_text(@picture,@font_size,0,x_pos,y_pos,c_text_color,@font_name,value)
end

#get_legend_box_size(data_description) ⇒ Object

This will help you to calculate dynamicaly the position where you want to print it (eg top-right). You must provide the data_description array as only parameter. This function will return and array containing in the first row the width of the box and in the second row the height of the box.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legend.rb', line 7

def get_legend_box_size(data_description)
  return(-1) if data_description["description"].nil?
  # <-10->[8]<-4->Text<-10->
  max_width = 0
  max_height = 8
  data_description["description"].each do |key,value|
    position   = image_ftb_box(@font_size,0,@font_name,value)
    text_width  = position[2]-position[0]
    text_height = position[1]-position[7]
    max_width = text_width if (text_width > max_width)
    max_height = max_height + text_height + 4
  end
  max_height = max_height - 3
  max_width  = max_width + 32

  [max_width,max_height]
end

#set_label(data, data_description, serie_name, value_name, caption, r = 210, g = 210, b = 210) ⇒ Object

This function will draw a label over the graph. You must specify the data & data_description structures, the serie name ( “Serie1” by default if only one ), the x position of the value in the data array (will be numeric starting at 0 if no abscise_label are defined or the value of the selected abscise serie if specified), the caption that will displayed and optionally the color of the label



203
204
205
206
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
240
241
242
# File 'lib/legend.rb', line 203

def set_label(data,data_description,serie_name,value_name,caption,r=210,g=210,b=210)
  data_description = validate_data_description("set_label",data_description)
  validate_data("set_label",data)
  shadow_factor = 100
  c_label   =allocate_color(@picture,r,g,b)
  c_shadow     =allocate_color(@picture,r-shadow_factor,g-shadow_factor,b-shadow_factor)
  c_text_color   =allocate_color(@picture,0,0,0)
  cp = 0
  found = false
  numerical_value = 0
  data.each do |key|
    if key[data_description["position"]].to_s == value_name.to_s
      numerical_value = key[serie_name]
      found = true
    end
    cp +=1 if !found
  end

  xpos = @g_area_x1 + @g_area_x_offset + ( @division_width * cp ) + 2
  ypos = @g_area_y2 - (numerical_value - @vmin) *@division_ratio
  position  = image_ftb_box(@font_size,0,@font_name,caption)
  text_height = position[3] - position[5]
  text_width  = position[2]-position[0] + 2
  text_offset = (text_height/2).floor
  # Shadow
  poly = [xpos+1,ypos+1,xpos + 9,ypos - text_offset,xpos + 8,ypos + text_offset + 2]
  image_filled_polygon(@picture,poly,r-shadow_factor,g-shadow_factor,b-shadow_factor,3)
  draw_line(xpos,ypos+1,xpos + 9,ypos - text_offset - 0.2,r-shadow_factor,g-shadow_factor,b-shadow_factor)
  draw_line(xpos,ypos+1,xpos + 9,ypos + text_offset + 2.2,r-shadow_factor,g-shadow_factor,b-shadow_factor)
  draw_filled_rectangle(xpos + 9,ypos - text_offset-0.2,xpos + 13 + text_width,ypos + text_offset + 2.2,r-shadow_factor,g-shadow_factor,b-shadow_factor)

  #Label background
  poly = [xpos,ypos,xpos + 8,ypos - text_offset - 1,xpos + 8,ypos + text_offset + 1]
  image_filled_polygon(@picture,poly,r,g,b,3)
  draw_line(xpos-1,ypos,xpos + 8,ypos - text_offset - 1.2,r,g,b)
  draw_line(xpos-1,ypos,xpos + 8,ypos + text_offset + 1.2,r,g,b)
  draw_filled_rectangle(xpos + 8,ypos - text_offset - 1.2,xpos + 12 + text_width,ypos + text_offset + 1.2,r,g,b)

  image_ttf_text(@picture,@font_size,0,xpos + 10,ypos + text_offset,c_text_color,@font_name,caption)
end