rchart 1.2.2 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +3 -3
- data/VERSION +1 -1
- data/lib/bar_chart.rb +166 -0
- data/lib/color_palette.rb +56 -0
- data/lib/cubic_chart.rb +271 -0
- data/lib/gd2_helper.rb +161 -0
- data/lib/graph.rb +24 -0
- data/lib/graph_helper.rb +414 -0
- data/lib/layout.rb +247 -0
- data/lib/legend.rb +243 -0
- data/lib/line_chart.rb +164 -0
- data/lib/pie_chart.rb +479 -0
- data/lib/plot_chart.rb +192 -0
- data/lib/rchart.rb +13 -3189
- data/lib/rchart_helper.rb +265 -0
- data/lib/rdata.rb +15 -14
- data/lib/scale.rb +538 -0
- metadata +49 -16
data/lib/layout.rb
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
module Layout
|
2
|
+
# A call to this function is mandatory when creating a graph.
|
3
|
+
# The upper left and bottom right border positions are used as arguments.
|
4
|
+
# This area will be used to draw graphs, grid, axis & more.
|
5
|
+
# Calling this function will not draw anything this will only set the graph area boundaries.
|
6
|
+
def set_graph_area(x1,y1,x2,y2)
|
7
|
+
@g_area_x1 = x1
|
8
|
+
@g_area_y1 = y1
|
9
|
+
@g_area_x2 = x2
|
10
|
+
@g_area_y2 = y2
|
11
|
+
end
|
12
|
+
# Prepare the graph area
|
13
|
+
def draw_graph_area(r,g,b,stripe=false)
|
14
|
+
draw_filled_rectangle(@g_area_x1,@g_area_y1,@g_area_x2,@g_area_y2,r,g,b,false)
|
15
|
+
draw_rectangle(@g_area_x1,@g_area_y1,@g_area_x2,@g_area_y2,r-40,g-40,b-40)
|
16
|
+
i=0
|
17
|
+
if stripe
|
18
|
+
r2 = r-15
|
19
|
+
r2 = 0 if r2<0
|
20
|
+
g2 = r-15
|
21
|
+
g2 = 0 if g2 < 0
|
22
|
+
b2 = r-15
|
23
|
+
b2 = 0 if b2 < 0
|
24
|
+
line_color = allocate_color(@picture,r2,g2,b2)
|
25
|
+
skew_width = @g_area_y2-@g_area_y1-1
|
26
|
+
|
27
|
+
i = @g_area_x1-skew_width
|
28
|
+
|
29
|
+
while i.to_f<=@g_area_x2.to_f
|
30
|
+
x1 = i
|
31
|
+
y1 = @g_area_y2
|
32
|
+
x2 = i+skew_width
|
33
|
+
y2 = @g_area_y1
|
34
|
+
if ( x1 < @g_area_x1 )
|
35
|
+
x1 = @g_area_x1
|
36
|
+
y1 = @g_area_y1 + x2 - @g_area_x1 + 1
|
37
|
+
end
|
38
|
+
if ( x2 >= @g_area_x2 )
|
39
|
+
y2 = @g_area_y1 + x2 - @g_area_x2 +1
|
40
|
+
x2 = @g_area_x2 - 1
|
41
|
+
end
|
42
|
+
image_line(@picture,x1,y1,x2,y2+1,r2,g2,b2)
|
43
|
+
i = i+4
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
# This function will draw a grid over the graph area.
|
49
|
+
# line_width will be passed to the draw_dotted_line function.
|
50
|
+
# The r,g,b 3 parameters are used to set the grid color.
|
51
|
+
# Setting mosaic to true will draw grey area between two lines.
|
52
|
+
# You can define the transparency factor of the mosaic area playing with the alpha parameter.
|
53
|
+
|
54
|
+
def draw_grid(line_width,mosaic=true,r=220,g=220,b=220,alpha=100)
|
55
|
+
# Draw mosaic */
|
56
|
+
if (mosaic)
|
57
|
+
layer_width = @g_area_x2-@g_area_x1
|
58
|
+
layer_height = @g_area_y2-@g_area_y1
|
59
|
+
|
60
|
+
@layers[0] = image_create_true_color(layer_width,layer_height)
|
61
|
+
#c_white = allocate_color(@layers[0],255,255,255)
|
62
|
+
image_filled_rectangle(@layers[0],0,0,layer_width,layer_height,255,255,255)
|
63
|
+
image_color_transparent(@layers[0],255,255,255)
|
64
|
+
|
65
|
+
#c_rectangle =allocate_color(@layers[0],250,250,250)
|
66
|
+
|
67
|
+
y_pos = layer_height #@g_area_y2-1
|
68
|
+
last_y = y_pos
|
69
|
+
i =0
|
70
|
+
while(i<=@division_count)
|
71
|
+
last_y= y_pos
|
72
|
+
y_pos = y_pos - @division_height
|
73
|
+
y_pos = 1 if ( y_pos <= 0 )
|
74
|
+
image_filled_rectangle(@layers[0],1, y_pos,layer_width-1,last_y,250,250,250) if ( i % 2 == 0 )
|
75
|
+
i = i+1
|
76
|
+
end
|
77
|
+
|
78
|
+
image_copy_merge(@layers[0],@picture,@g_area_x1,@g_area_y1,0,0,layer_width,layer_height,alpha)
|
79
|
+
#image_destroy(@layers[0])
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
#Horizontal lines
|
84
|
+
y_pos = @g_area_y2 - @division_height
|
85
|
+
i=1
|
86
|
+
while(i<=@division_count)
|
87
|
+
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 )
|
88
|
+
y_pos = y_pos - @division_height
|
89
|
+
i = i+1
|
90
|
+
end
|
91
|
+
# Vertical lines
|
92
|
+
if (@g_area_x_offset == 0 )
|
93
|
+
x_pos = @g_area_x1 + (@division_width) +@g_area_x_offset
|
94
|
+
col_count = (@data_count.to_f-2).floor
|
95
|
+
else
|
96
|
+
|
97
|
+
x_pos = @g_area_x1 +@g_area_x_offset
|
98
|
+
col_count = ( (@g_area_x2 - @g_area_x1) / @division_width )
|
99
|
+
end
|
100
|
+
i= 1
|
101
|
+
|
102
|
+
while (i<=col_count)
|
103
|
+
if ( x_pos > @g_area_x1 && x_pos < @g_area_x2 )
|
104
|
+
self.draw_dotted_line((x_pos).floor,@g_area_y1,(x_pos).floor,@g_area_y2,line_width,r,g,b)
|
105
|
+
end
|
106
|
+
x_pos = x_pos + @division_width
|
107
|
+
i= i+1
|
108
|
+
end
|
109
|
+
end
|
110
|
+
# This function will draw an horizontal treshold ( this is an easy way to draw the 0 line ).
|
111
|
+
# If show_label is set to true, the value of the treshold will be written over the graph.
|
112
|
+
# If show_on_right is set to true, the value will be written on the right side of the graph.
|
113
|
+
# r, g and b are used to set the line and text color.
|
114
|
+
# Use tick_width to set the width of the ticks, if set to 0 this will draw a solid line.
|
115
|
+
# You can optionnaly provide the caption of the treshold (by default the treshold value is used)
|
116
|
+
|
117
|
+
def draw_treshold(value,r,g,b,show_label=false,show_on_right=false,tick_width=4,free_text=nil)
|
118
|
+
b, g, r = validate_color(b, g, r)
|
119
|
+
|
120
|
+
c_text_color =allocate_color(@picture,r,g,b)
|
121
|
+
# c_text_color = GD2::Color.new(r,g,b)
|
122
|
+
y = @g_area_y2 - (value - @vmin.to_f) * @division_ratio.to_f
|
123
|
+
|
124
|
+
return(-1) if ( y <= @g_area_y1 || y >= @g_area_y2 )
|
125
|
+
if ( tick_width == 0 )
|
126
|
+
self.draw_line(@g_area_x1,y,@g_area_x2,y,r,g,b)
|
127
|
+
else
|
128
|
+
self.draw_dotted_line(@g_area_x1,y,@g_area_x2,y,tick_width,r,g,b)
|
129
|
+
end
|
130
|
+
if (show_label )
|
131
|
+
if ( free_text.nil? )
|
132
|
+
label = value
|
133
|
+
else
|
134
|
+
label = free_text
|
135
|
+
end
|
136
|
+
|
137
|
+
if ( show_on_right )
|
138
|
+
image_ttf_text(@picture,@font_size,0,@g_area_x2+2,y+(@font_size/2),c_text_color,@font_name,label.to_s)
|
139
|
+
else
|
140
|
+
image_ttf_text(@picture,@font_size,0,@g_area_x1+2,y-(@font_size/2),c_text_color,@font_name,label.to_s)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
# This function will draw an area between two data series.
|
145
|
+
# extracting the minimum and maximum value for each X positions.
|
146
|
+
# You must specify the two series name and the area color.
|
147
|
+
# You can specify the transparency which is set to 50% by default.
|
148
|
+
|
149
|
+
def draw_area(data,serie1,serie2,r,g,b,alpha = 50)
|
150
|
+
validate_data("draw_area",data)
|
151
|
+
layer_width = @g_area_x2-@g_area_x1
|
152
|
+
layer_height = @g_area_y2-@g_area_y1
|
153
|
+
|
154
|
+
@layers[0] = image_create_true_color(layer_width,layer_height)
|
155
|
+
image_filled_rectangle(@layers[0],0,0,layer_width,layer_height,255,255,255)
|
156
|
+
image_color_transparent(@layers[0],255,255,255)
|
157
|
+
|
158
|
+
x_pos = @g_area_x_offset
|
159
|
+
last_x_pos = -1
|
160
|
+
last_y_pos1 = nil
|
161
|
+
last_y_pos2= nil
|
162
|
+
data.each do |key|
|
163
|
+
value1 = key[serie1]
|
164
|
+
value2 = key[serie2]
|
165
|
+
y_pos1 = layer_height - ((value1-@vmin) * @division_ratio)
|
166
|
+
y_pos2 = layer_height - ((value2-@vmin) * @division_ratio)
|
167
|
+
|
168
|
+
if ( last_x_pos != -1 )
|
169
|
+
points = []
|
170
|
+
points << last_x_pos
|
171
|
+
points << last_y_pos1
|
172
|
+
points << last_x_pos
|
173
|
+
points << last_y_pos2
|
174
|
+
points << x_pos
|
175
|
+
points << y_pos2
|
176
|
+
points << x_pos
|
177
|
+
points << y_pos1
|
178
|
+
image_filled_polygon(@layers[0],points,r,g,b,4)
|
179
|
+
end
|
180
|
+
last_y_pos1 = y_pos1
|
181
|
+
last_y_pos2 = y_pos2
|
182
|
+
last_x_pos = x_pos
|
183
|
+
x_pos= x_pos+ @division_width
|
184
|
+
end
|
185
|
+
image_copy_merge(@layers[0],@picture,@g_area_x1,@g_area_y1,0,0,layer_width,layer_height,alpha)
|
186
|
+
image_destroy(@layers[0])
|
187
|
+
end
|
188
|
+
# Use this function to add a border to your picture. Be carefull, drawing a border will change all the chart components positions, thus this call must be the last one before one of the rendering methods!!!
|
189
|
+
# You can specify the size of the border and its color.
|
190
|
+
# The width and height of the picture will be modified by 2x the size value.
|
191
|
+
def add_border(size=3,r=0,g=0,b=0)
|
192
|
+
width = @x_size+2*size
|
193
|
+
height = @y_size+2*size
|
194
|
+
resampled = image_create_true_color(width,height)
|
195
|
+
image_filled_rectangle(resampled,0,0,width,height, r, g, b)
|
196
|
+
image_copy(@picture,resampled,size,size,0,0,@x_size,@y_size)
|
197
|
+
image_destroy(@picture)
|
198
|
+
@x_size = width
|
199
|
+
@y_size = height
|
200
|
+
@picture = image_create_true_color(@x_size,@y_size)
|
201
|
+
image_filled_rectangle(@picture,0,0,@x_size,@y_size,255,255,255)
|
202
|
+
image_color_transparent(@picture,255,255,255)
|
203
|
+
image_copy(resampled,@picture,0,0,0,0,@x_size,@y_size)
|
204
|
+
end
|
205
|
+
# You can use this function to display the values contained in the series on top of the charts.
|
206
|
+
# It is possible to specify one or multiple series to display using and array.
|
207
|
+
def write_values(data,data_description,series)
|
208
|
+
|
209
|
+
data_description = self.validate_data_description("write_values",data_description)
|
210
|
+
validate_data("write_values",data)
|
211
|
+
series = [series] if ( !series.is_a?(Array))
|
212
|
+
id = 0
|
213
|
+
color_id =0
|
214
|
+
series.each do |col_name|
|
215
|
+
data_description["description"].each do |key_i,value_i|
|
216
|
+
if ( key_i == col_name )
|
217
|
+
color_id = id
|
218
|
+
id = id+1
|
219
|
+
end
|
220
|
+
end
|
221
|
+
xpos = @g_area_x1 + @g_area_x_offset
|
222
|
+
xlast = -1
|
223
|
+
data.each do |key|
|
224
|
+
if ((!key[col_name].nil?) && (key[col_name].is_a?(Numeric)))
|
225
|
+
value = key[col_name]
|
226
|
+
ypos = @g_area_y2 - ((value-@vmin) * @division_ratio)
|
227
|
+
positions = image_ftb_box(@font_size,0,@font_name,value.to_s)
|
228
|
+
width = positions[2] - positions[6]
|
229
|
+
x_offset = xpos - (width/2)
|
230
|
+
height = positions[3] - positions[7]
|
231
|
+
y_offset = ypos - 4
|
232
|
+
|
233
|
+
c_text_color = allocate_color(@picture,@palette[color_id]["r"],@palette[color_id]["g"],@palette[color_id]["b"]);
|
234
|
+
image_ttf_text(@picture,@font_size,0,x_offset,y_offset,c_text_color,@font_name,value.to_s)
|
235
|
+
end
|
236
|
+
xpos = xpos + @division_width
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
# Use this function to enable error reporting during the chart rendering.
|
241
|
+
# By default messages are redirected to the console while using the render command and using GD while using the stroke command.
|
242
|
+
# You can force the errors to be redirected to either cli or gd specifying it as parameter.
|
243
|
+
def report_warnings(interface="cli")
|
244
|
+
@error_reporting = true
|
245
|
+
@error_interface = interface
|
246
|
+
end
|
247
|
+
end
|
data/lib/legend.rb
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
module Legend
|
2
|
+
# This function evaluate the width and height of the box generated by the draw_legend.
|
3
|
+
# This will help you to calculate dynamicaly the position where you want to print it (eg top-right).
|
4
|
+
# You must provide the data_description array as only parameter.
|
5
|
+
# 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.
|
6
|
+
|
7
|
+
def get_legend_box_size(data_description)
|
8
|
+
return(-1) if data_description["description"].nil?
|
9
|
+
# <-10->[8]<-4->Text<-10->
|
10
|
+
max_width = 0
|
11
|
+
max_height = 8
|
12
|
+
data_description["description"].each do |key,value|
|
13
|
+
position = image_ftb_box(@font_size,0,@font_name,value)
|
14
|
+
text_width = position[2]-position[0]
|
15
|
+
text_height = position[1]-position[7]
|
16
|
+
max_width = text_width if (text_width > max_width)
|
17
|
+
max_height = max_height + text_height + 4
|
18
|
+
end
|
19
|
+
max_height = max_height - 3
|
20
|
+
max_width = max_width + 32
|
21
|
+
|
22
|
+
[max_width,max_height]
|
23
|
+
end
|
24
|
+
# This function will draw the legend of the graph ( serie color & serie name ) at the specified position.
|
25
|
+
# The r,g,bparameters are used to set the background color. You can optionally provide the shadow color using the rs,gs,bs parameters.
|
26
|
+
# You can also customize the text color using the rt,gt,bt.
|
27
|
+
# Setting Border to false remove the surrounding box.
|
28
|
+
|
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)
|
30
|
+
#Validate the Data and data_description array
|
31
|
+
data_description = validate_data_description("draw_legend",data_description)
|
32
|
+
return(-1) if (data_description["description"].nil?)
|
33
|
+
c_text_color = allocate_color(@picture, rt, gt, bt)
|
34
|
+
# <-10->[8]<-4->Text<-10->
|
35
|
+
max_width = 0
|
36
|
+
max_height = 8
|
37
|
+
data_description["description"].each do |key,value|
|
38
|
+
position = image_ftb_box(@font_size,0,@font_name,value)
|
39
|
+
text_width = position[2]-position[0]
|
40
|
+
text_height = position[1]-position[7]
|
41
|
+
max_width = text_width if ( text_width > max_width)
|
42
|
+
max_height = max_height + text_height + 4
|
43
|
+
end
|
44
|
+
max_height = max_height - 5
|
45
|
+
max_width = max_width + 32
|
46
|
+
if ( rs == -1 || gs == -1 || bs == -1 )
|
47
|
+
rs = r-30
|
48
|
+
gs = g-30
|
49
|
+
bs = b-30
|
50
|
+
end
|
51
|
+
if ( border )
|
52
|
+
draw_filled_rounded_rectangle(x_pos+1,y_pos+1,x_pos+max_width+1,y_pos+max_height+1,5,rs,gs,bs)
|
53
|
+
draw_filled_rounded_rectangle(x_pos,y_pos,x_pos+max_width,y_pos+max_height,5,r,g,b)
|
54
|
+
end
|
55
|
+
y_offset = 4 + @font_size
|
56
|
+
id = 0
|
57
|
+
data_description["description"].each do |key,value|
|
58
|
+
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"])
|
59
|
+
image_ttf_text(@picture, @font_size,0, x_pos+22, y_pos+y_offset, c_text_color, @font_name, value)
|
60
|
+
position = image_ftb_box(@font_size,0,@font_name,value)
|
61
|
+
text_height = position[1]-position[7]
|
62
|
+
y_offset = y_offset + text_height + 4
|
63
|
+
id=id+1
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# This function will draw the legend of a pie graph ( serie color & value name ).
|
68
|
+
# Be carrefull, dataset used for pie chart are not the same than for other line / curve / plot graphs.
|
69
|
+
# You can specify the position of the legend box and the background color.
|
70
|
+
def draw_pie_legend(x_pos,y_pos,data,data_description,r,g,b)
|
71
|
+
data_description = validate_data_description("draw_pie_legend",data_description,false)
|
72
|
+
validate_data("draw_pie_legend",data)
|
73
|
+
return(-1) if (data_description["position"].nil?)
|
74
|
+
c_text_color = allocate_color(@picture,0,0,0)
|
75
|
+
|
76
|
+
# <-10->[8]<-4->Text<-10-> */
|
77
|
+
max_width = 0
|
78
|
+
max_height = 8
|
79
|
+
data.each do |key|
|
80
|
+
value = key[data_description["position"]]
|
81
|
+
position = image_ftb_box(@font_size,0,@font_name,value)
|
82
|
+
text_width = position[2]-position[0]
|
83
|
+
text_height = position[1]-position[7]
|
84
|
+
max_width = text_width if ( text_width > max_width)
|
85
|
+
max_height = max_height + text_height + 4
|
86
|
+
end
|
87
|
+
max_height = max_height - 3
|
88
|
+
max_width = max_width + 32
|
89
|
+
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)
|
90
|
+
draw_filled_rounded_rectangle(x_pos,y_pos,x_pos+max_width,y_pos+max_height,5,r,g,b)
|
91
|
+
y_offset = 4 + @font_size
|
92
|
+
id = 0
|
93
|
+
data.each do |key|
|
94
|
+
value = key[data_description["position"]]
|
95
|
+
position = image_ftb_box(@font_size,0,@font_name,value)
|
96
|
+
text_height = position[1]-position[7]
|
97
|
+
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"])
|
98
|
+
image_ttf_text(@picture,@font_size,0,x_pos+22,y_pos+y_offset,c_text_color,@font_name,value)
|
99
|
+
y_offset = y_offset + text_height + 4
|
100
|
+
id= id+1
|
101
|
+
end
|
102
|
+
end
|
103
|
+
# This function is used to write the graph title.
|
104
|
+
# Used with default parameters you must specify the bottom left position of the text.
|
105
|
+
# if you are specifying x2 and y2 the text will be centered horizontaly and verticaly in the box of coordinates (x1,y1)-(x2,y2).
|
106
|
+
# value correspond to the text that will be written on the graph.
|
107
|
+
# r, g and b are used to set the text color.
|
108
|
+
# Setting shadow to true will makes a shadow behind the text.
|
109
|
+
def draw_title(x_pos,y_pos,value,r,g,b,x_pos2=-1,y_pos2=-1,shadow=false)
|
110
|
+
c_text_color = allocate_color(@picture, r, g, b)
|
111
|
+
if ( x_pos2 != -1 )
|
112
|
+
position = image_ftb_box(@font_size,0,@font_name,value)
|
113
|
+
text_width = position[2]-position[0]
|
114
|
+
x_pos =(( x_pos2 - x_pos -text_width ) / 2 ).floor + x_pos
|
115
|
+
end
|
116
|
+
if ( y_pos2 != -1 )
|
117
|
+
position = image_ftb_box(@font_size,0,@font_name,value)
|
118
|
+
text_height = position[5]-position[3]
|
119
|
+
y_pos =(( y_pos2 - y_pos - text_height ) / 2 ).floor + y_pos
|
120
|
+
end
|
121
|
+
if ( shadow )
|
122
|
+
c_shadow_color = allocate_color(@picture,@shadow_r_color,@shadow_g_color,@shadow_b_color)
|
123
|
+
image_ttf_text(@picture,@font_size,0,x_pos+@shadow_x_distance,y_pos+@shadow_y_distance, c_shadow_color ,@font_name,value)
|
124
|
+
end
|
125
|
+
image_ttf_text(@picture,@font_size,0,x_pos,y_pos,c_text_color,@font_name,value)
|
126
|
+
end
|
127
|
+
# Use this function to write text over the picture.
|
128
|
+
# 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.
|
129
|
+
# You can choose how the text will be aligned with the align parameter :
|
130
|
+
# * Rchart:: ALIGN_TOP_LEFT Use the box top left corner.
|
131
|
+
# * Rchart:: ALIGN_TOP_CENTER Use the box top center corner.
|
132
|
+
# * Rchart:: ALIGN_TOP_RIGHT Use the box top right corner.
|
133
|
+
# * Rchart:: ALIGN_LEFT Use the center left.
|
134
|
+
# * Rchart:: ALIGN_CENTER Use the center.
|
135
|
+
# * Rchart:: ALIGN_RIGHT Use the center right.
|
136
|
+
# * Rchart:: ALIGN_BOTTOM_LEFT Use the box bottom left corner.
|
137
|
+
# * Rchart:: ALIGN_BOTTOM_CENTER Use the box bottom center corner.
|
138
|
+
# * Rchart:: ALIGN_BOTTOM_RIGHT Use the box bottom right corner.
|
139
|
+
|
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)
|
141
|
+
position = image_ftb_box(@font_size,angle,@font_name,text)
|
142
|
+
text_width = position[2]-position[0]
|
143
|
+
text_height = position[5]-position[3]
|
144
|
+
area_width = x2 - x1
|
145
|
+
area_height = y2 - y1
|
146
|
+
x =nil
|
147
|
+
y = nil
|
148
|
+
|
149
|
+
if ( bgr != -1 && bgg != -1 && bgb != -1 )
|
150
|
+
draw_filled_rectangle(x1,y1,x2,y2,bgr,bgg,bgb,false,alpha)
|
151
|
+
end
|
152
|
+
|
153
|
+
if ( align == Rchart::ALIGN_TOP_LEFT )
|
154
|
+
x = x1+1
|
155
|
+
y = y1+@font_size+1
|
156
|
+
end
|
157
|
+
|
158
|
+
if ( align == Rchart::ALIGN_TOP_CENTER )
|
159
|
+
x = x1+(area_width/2)-(text_width/2)
|
160
|
+
y = y1+@font_size+1
|
161
|
+
end
|
162
|
+
|
163
|
+
if ( align == Rchart::ALIGN_TOP_RIGHT )
|
164
|
+
x = x2-text_width-1
|
165
|
+
y = y1+@font_size+1
|
166
|
+
end
|
167
|
+
if ( align == Rchart::ALIGN_LEFT )
|
168
|
+
x = x1+1
|
169
|
+
y = y1+(area_height/2)-(text_height/2)
|
170
|
+
end
|
171
|
+
if ( align == Rchart::ALIGN_CENTER )
|
172
|
+
x = x1+(area_width/2)-(text_width/2)
|
173
|
+
y = y1+(area_height/2)-(text_height/2)
|
174
|
+
end
|
175
|
+
if ( align == Rchart::ALIGN_RIGHT )
|
176
|
+
x = x2-text_width-1
|
177
|
+
y = y1+(area_height/2)-(text_height/2)
|
178
|
+
end
|
179
|
+
if ( align == Rchart::ALIGN_BOTTOM_LEFT )
|
180
|
+
x = x1+1
|
181
|
+
y = y2-1
|
182
|
+
end
|
183
|
+
if ( align == Rchart::ALIGN_BOTTOM_CENTER )
|
184
|
+
x = x1+(area_width/2)-(text_width/2)
|
185
|
+
y = y2-1
|
186
|
+
end
|
187
|
+
if ( align == Rchart::ALIGN_BOTTOM_RIGHT )
|
188
|
+
x = x2-text_width-1
|
189
|
+
y = y2-1
|
190
|
+
end
|
191
|
+
c_text_color =allocate_color(@picture,r,g,b)
|
192
|
+
c_shadow_color =allocate_color(@picture,0,0,0)
|
193
|
+
if ( shadow )
|
194
|
+
image_ttf_text(@picture,@font_size,angle,x+1,y+1,c_shadow_color,@font_name,text)
|
195
|
+
end
|
196
|
+
|
197
|
+
image_ttf_text(@picture,@font_size,angle,x,y,c_text_color,@font_name,text)
|
198
|
+
end
|
199
|
+
# This function will draw a label over the graph.
|
200
|
+
# You must specify the data & data_description structures, the serie name ( "Serie1" by default if only one ),
|
201
|
+
# 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
|
202
|
+
|
203
|
+
def set_label(data,data_description,serie_name,value_name,caption,r=210,g=210,b=210)
|
204
|
+
data_description = validate_data_description("set_label",data_description)
|
205
|
+
validate_data("set_label",data)
|
206
|
+
shadow_factor = 100
|
207
|
+
c_label =allocate_color(@picture,r,g,b)
|
208
|
+
c_shadow =allocate_color(@picture,r-shadow_factor,g-shadow_factor,b-shadow_factor)
|
209
|
+
c_text_color =allocate_color(@picture,0,0,0)
|
210
|
+
cp = 0
|
211
|
+
found = false
|
212
|
+
numerical_value = 0
|
213
|
+
data.each do |key|
|
214
|
+
if key[data_description["position"]].to_s == value_name.to_s
|
215
|
+
numerical_value = key[serie_name]
|
216
|
+
found = true
|
217
|
+
end
|
218
|
+
cp +=1 if !found
|
219
|
+
end
|
220
|
+
|
221
|
+
xpos = @g_area_x1 + @g_area_x_offset + ( @division_width * cp ) + 2
|
222
|
+
ypos = @g_area_y2 - (numerical_value - @vmin) *@division_ratio
|
223
|
+
position = image_ftb_box(@font_size,0,@font_name,caption)
|
224
|
+
text_height = position[3] - position[5]
|
225
|
+
text_width = position[2]-position[0] + 2
|
226
|
+
text_offset = (text_height/2).floor
|
227
|
+
# Shadow
|
228
|
+
poly = [xpos+1,ypos+1,xpos + 9,ypos - text_offset,xpos + 8,ypos + text_offset + 2]
|
229
|
+
image_filled_polygon(@picture,poly,r-shadow_factor,g-shadow_factor,b-shadow_factor,3)
|
230
|
+
draw_line(xpos,ypos+1,xpos + 9,ypos - text_offset - 0.2,r-shadow_factor,g-shadow_factor,b-shadow_factor)
|
231
|
+
draw_line(xpos,ypos+1,xpos + 9,ypos + text_offset + 2.2,r-shadow_factor,g-shadow_factor,b-shadow_factor)
|
232
|
+
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)
|
233
|
+
|
234
|
+
#Label background
|
235
|
+
poly = [xpos,ypos,xpos + 8,ypos - text_offset - 1,xpos + 8,ypos + text_offset + 1]
|
236
|
+
image_filled_polygon(@picture,poly,r,g,b,3)
|
237
|
+
draw_line(xpos-1,ypos,xpos + 8,ypos - text_offset - 1.2,r,g,b)
|
238
|
+
draw_line(xpos-1,ypos,xpos + 8,ypos + text_offset + 1.2,r,g,b)
|
239
|
+
draw_filled_rectangle(xpos + 8,ypos - text_offset - 1.2,xpos + 12 + text_width,ypos + text_offset + 1.2,r,g,b)
|
240
|
+
|
241
|
+
image_ttf_text(@picture,@font_size,0,xpos + 10,ypos + text_offset,c_text_color,@font_name,caption)
|
242
|
+
end
|
243
|
+
end
|