public
Description: The ruby on rails plugin for teethgrinder's Open Flash Chart (version 2)
Homepage: http://pullmonkey.com/projects/open_flash_chart2/
Clone URL: git://github.com/pullmonkey/open_flash_chart.git
100644 105 lines (88 sloc) 2.95 kb
1
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
50
51
52
53
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
module OpenFlashChart
  class Base
 
    def initialize(args={})
      # set all the instance variables we want
      # assuming something like this OpenFlashChart.new(:x_axis => 5, :y_axis => 10, :elements => ["one", "two"], ...)
      args.each do |k,v|
        self.instance_variable_set("@#{k}", v)
      end
      yield self if block_given? # magic pen pattern
    end
 
    # same as to_s but won't stack overflow ... use this instead of to_s
    def render
      # need to return the following like this
      # 1) font_size as font-size
      # 2) dot_size as dot-size
      # 3) outline_colour as outline-colour
      # 4) halo_size as halo-size
      # 5) start_angle as start-angle
      # 6) tick_height as tick-height
      # 7) grid_colour as grid-colour
      # 8) threed as 3d
      # 9) tick_length as tick-length
      # 10) visible_steps as visible-steps
      # 11) key_on_click as key-on-click
      returning self.to_json2 do |output|
        output.gsub!("threed","3d")
        %w(font_size dot_size outline_colour halo_size start_angle tick_height grid_colour tick_length no_labels label_colour gradient_fill fill_alpha on_click spoke_labels visible_steps key_on_click).each do |replace|
          output.gsub!(replace, replace.gsub("_", "-"))
        end
      end
    end
 
    def to_json2
      self.instance_values.to_json
    end
 
    alias_method :to_s, :render
 
    def add_element(element)
      @elements ||= []
      @elements << element
    end
 
    def <<(e)
      add_element e
    end
 
    def set_key(text, size)
      @text = text
      @font_size = size
    end
 
    def append_value(v)
      @values ||= []
      @values << v
    end
 
    def set_range(min,max,steps=1)
      @min = min
      @max = max
      @steps = steps
    end
 
    def set_offset(v)
      @offset = v ? true : false
    end
 
    def set_colours(colour, grid_colour)
      @colour = colour
      @grid_colour = grid_colour
    end
 
    def set_tooltip(tip)
      if tip.is_a?(Tooltip)
        #we have a style for our chart's tooltips
        @tooltip = tip
      else
        # the user could just use set_tip(tip) or tip=(tip) to just set the text of the tooltip
        @tip = tip
      end
    end
    alias_method "tooltip=", :set_tooltip
 
 
 
    def method_missing(method_name, *args, &blk)
      case method_name.to_s
      when /(.*)=/ # i.e., if it is something x_legend=
        # if the user wants to set an instance variable then let them
        # the other args (args[0]) are ignored since it is a set method
        self.instance_variable_set("@#{$1}", args[0])
      when /^set_(.*)/
        # backwards compatible ... the user can still use the same set_y_legend methods if they want
        self.instance_variable_set("@#{$1}", args[0])
      else
        # if the method/attribute is missing and it is not a set method then hmmmm better let the user know
        super
      end
    end
 
  end
end