Class: OpenFlashChart

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOpenFlashChart

Returns a new instance of OpenFlashChart.



2
3
4
5
6
7
8
9
10
11
12
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
# File 'lib/open_flash_chart.rb', line 2

def initialize
  @data = []
  @x_labels = []
  @y_min = 0
  @y_max = 20
  @y_label_steps = 5
  @title = ""
  @title_style = ""
  @title_size = 30
  @x_tick_size = -1
  @y2_max = ''
  @y2_min = ''

  # GRID styles
  @y_axis_color = @x_axis_color = @x_grid_color = @y_grid_color = ""
  @x_axis_3d = ''
  @x_axis_steps = -1
  @y2_axis_color = ''

  # AXIS LABEL styles
  @x_label_style = ''
  @y_label_style = ''
  @y_label_style_right = ''

  # AXIS LEGEND styles
  @x_legend = ''
  @y_legend = ''
  @y_legend_right = ''

  @lines = []
  @y2_lines = []
  @line_default = "&line=3,#87421F" + "& \n"
	
  @bg_color = ''
  @bg_image = ''

  @inner_bg_color = ''
  @inner_bg_color_2 = ''
  @inner_bg_angle = ''

  # PIE chart
  @pie = ''
  @pie_values = ''
  @pie_colors = ''
  @pie_labels = ''

  @tool_tip = ''
end

Class Method Details

.swf_object(width, height, url) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/open_flash_chart.rb', line 298

def self.swf_object(width, height, url)
  url     = CGI::escape(url)
  output  = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"#{width}\" height=\"#{height}\" id=\"graph\" align=\"middle\">"
  output += '<param name="allowScriptAccess" value="sameDomain" />'
  output += "<param name=\"movie\" value=\"/open-flash-chart.swf?width=#{width.to_s} &height=#{height.to_s} &data=#{url}\" /><param name=\"quality\" value=\"high\" /><param name=\"bgcolor\" value=\"#FFFFFF\" />"
  output += '<embed src="/open-flash-chart.swf?width=' + width.to_s + '&height=' + height.to_s + '&data=' + url.to_s + '" quality="high" bgcolor="#FFFFFF" width="' +  width.to_s + '" height="' + height.to_s + '" name="open-flash-chart" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
  output += '</object>'
  return output
end

Instance Method Details

#area_hollow(width, dot_size, color, alpha, text = '', font_size = '') ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/open_flash_chart.rb', line 155

def area_hollow(width, dot_size, color, alpha, text='', font_size='')
  temp  = "&area_hollow"
  temp += '_' + (@lines.size + 1).to_s if @lines.size > 0
  temp += "=#{width},#{dot_size},#{color},#{alpha}"
  temp += ",#{text},#{font_size}" if text.size > 0
  temp += "& \n"

  @lines << temp
end

#attach_to_y_right_axis(data_number) ⇒ Object



81
82
83
# File 'lib/open_flash_chart.rb', line 81

def attach_to_y_right_axis(data_number)
  @y2_lines << data_number
end

#line(width, color = '', text = '', size = -1,, circles = -1)) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/open_flash_chart.rb', line 127

def line(width, color='', text='', size=-1, circles=-1)
  temp  = '&line'
  temp += '_' + (@lines.size + 1).to_s if @lines.size > 0
  temp += '='
  temp += width.to_s			    if width > 0
  temp += ',' + color 		    if width > 0
  temp += ',' + text          if text.size > 0
  temp += ',' + size.to_s     if text.size > 0
  temp += ',' + circles.to_s 	if circles > 0
  temp += "& \n"

  @lines << temp
end

#pie(alpha, line_color, label_color) ⇒ Object



203
204
205
# File 'lib/open_flash_chart.rb', line 203

def pie(alpha, line_color, label_color)
  @pie = "#{alpha},#{line_color},#{label_color}"
end

#pie_slice_colors(colors) ⇒ Object



212
213
214
# File 'lib/open_flash_chart.rb', line 212

def pie_slice_colors(colors)
  @pie_colors = colors.join(",")
end

#pie_values(values, labels) ⇒ Object



207
208
209
210
# File 'lib/open_flash_chart.rb', line 207

def pie_values(values, labels)
  @pie_values = values.join(',')
  @pie_labels = labels.join(',')
end

#renderObject



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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
# File 'lib/open_flash_chart.rb', line 216

def render
  temp  = ""

  {"title" 					=> [@title, @title_style],
    "x_legend"				=> [@x_legend, @x_legend_size, @x_legend_color],
    "x_ticks"				=> [@x_tick_size],
    "x_axis_steps"   => [@x_axis_steps, @x_axis_3d],
    "bg_colour" 			=> [@bg_color],
    "tool_tip"				=> [@tool_tip]
  }.each do |k,v|
    next if (v[0].class == String ? v[0].size <= 0 : v[0] && v[0] < 0)
    temp += "&#{k}="
    # added compact to remove nil values from the array
    temp += v.compact.join(",")
    temp += "& \n"
  end

  temp += @x_label_style if @x_label_style.size > 0

  %w(y_legend y_legend_right).each do |x|
    temp += self.instance_variable_get("@#{x}")
    temp += "," + self.instance_variable_get("@#{x}_size").to_s if 
    self.instance_variable_get("@#{x}_size")
    temp += "," + self.instance_variable_get("@#{x}_color") if 
    self.instance_variable_get("@#{x}_color")

  end

  temp += "&y_ticks=5,10," + "#{@y_label_steps}& \n"

  if @lines.size == 0
    temp += @line_default
  else
    temp += @lines.to_s
  end

  temp += @data.to_s

  if @y2_lines.size > 0
    temp += "&y2_lines="
    temp += @y2_lines.join(",")
    temp += "& \n"
    temp += "&show_y2=true& \n"
  end

  temp += "&x_labels=#{@x_labels.join(',')}& \n" if @x_labels.size > 0

  temp += "&y_min=#{@y_min}& \n"
  temp += "&y_max=#{@y_max}& \n"

  if @bg_image.size > 0
    temp += "&bg_image=#{@bg_image}& \n"
    temp += "&bg_image_x=#{@bg_image_x}& \n"
    temp += "&bg_image_y=#{@bg_image_y}& \n"
  end

  %w(x y).each do |axis|
    if self.instance_variable_get("@#{axis}_axis_color").size > 0
      temp += "&#{axis}_axis_colour=#{self.instance_variable_get("@#{axis}_axis_color")}& \n"
      temp += "&#{axis}_grid_colour=#{self.instance_variable_get("@#{axis}_grid_color")}& \n"
      temp += "&#{axis}2_axis_colour=#{self.instance_variable_get("@#{axis}2_axis_color")}& \n" if axis == 'y'
    end
  end
  if @inner_bg_color.size > 0
    temp += "&inner_background=#{@inner_bg_color}"
    if @inner_bg_color_2.size > 0
      temp += "," + @inner_bg_color_2
      temp += "," + @inner_bg_angle
    end
    temp += "& \n"
  end

  if @pie.size > 0
    temp += "&pie=#{@pie}& \n"
    temp += "&values=#{@pie_values}& \n"
    temp += "&pie_labels=#{@pie_labels}& \n"
    temp += "&colours=#{@pie_colors}& \n"
  end

  return temp
end

#set_bg_image(url, x = 'center', y = 'center') ⇒ Object



75
76
77
78
79
# File 'lib/open_flash_chart.rb', line 75

def set_bg_image(url, x='center', y='center')
  @bg_image 	= url
  @bg_image_x = x
  @bg_image_y = y
end

#set_data(data) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/open_flash_chart.rb', line 51

def set_data(data)
  if @data.size == 0
    @data << '&values=' + data.join(',') + "& \n"
  else
    @data << '&values_' + (@data.size + 1).to_s + "=" + data.join(',') + "& \n"
  end
end

#set_inner_background(col, col2 = '', angle = -1)) ⇒ Object



85
86
87
88
89
# File 'lib/open_flash_chart.rb', line 85

def set_inner_background(col, col2='', angle=-1)
  @inner_bg_color  = col
  @inner_bg_color2 = col2  if col2.size > 0
  @inner_bg_angle  = angle if angle != -1
end

#set_x_label_style(size, color = '', orientation = 0, step = -1,, grid_color = '') ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/open_flash_chart.rb', line 66

def set_x_label_style(size, color='', orientation=0, step=-1, grid_color='')
  @x_label_style  = "&x_label_style=#{size}"
  @x_label_style << ",#{color}" 			if color.size > 0
  @x_label_style += ",#{orientation}" if orientation > -1
  @x_label_style += ",#{step}" 				if step > 0
  @x_label_style += ",#{grid_color}"  if grid_color.size > 0

end

#title(title, style = '') ⇒ Object



108
109
110
111
# File 'lib/open_flash_chart.rb', line 108

def title(title, style='')
  @title       = title
  @title_style = style  if style.size > 0
end