Class: ReportBuilder::Graph::HtmlFlot

Inherits:
ElementBuilder show all
Defined in:
lib/reportbuilder/graph/html_flot.rb

Overview

Flot Wrapper

Instance Method Summary collapse

Methods inherited from ElementBuilder

#initialize

Constructor Details

This class inherits a constructor from ReportBuilder::ElementBuilder

Instance Method Details

#axes_defaultsObject



67
68
69
# File 'lib/reportbuilder/graph/html_flot.rb', line 67

def axes_defaults
  hash_to_camel(@element.axes_defaults)
end

#flot_optionsObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/reportbuilder/graph/html_flot.rb', line 53

def flot_options
  opts=Hash.new
  opts[:title]=@element.title
  opts[:axesDefaults]=axes_defaults if @element.axes_defaults.size>0
  opts[:xaxis]=xaxis
  opts[:yaxis]=yaxis
  opts[:series]= series_defaults if @element.series_defaults.size>0
  opts[:legend]=legend  if @element.legend.size>0
  opts[:grid]=grid if @element.grid.size>0
  opts
end

#flot_seriesObject

Transform data options on jqPlot ones



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/reportbuilder/graph/html_flot.rb', line 135

def flot_series
  dv=@element.series_data
  i=0
  own_options=@element.series_options.map do |in_opt|
    out_opt=Hash.new
    out_opt[:data]=dv[i]
    i+=1
    out_opt.merge(series_js(in_opt))
  end
  own_options
end

#generateObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/reportbuilder/graph/html_flot.rb', line 5

def generate()
  @builder.js(ReportBuilder::DATA_DIR+"/flot/excanvas.min.js")

  @builder.js(ReportBuilder::DATA_DIR+"/flot/jquery.min.js")
  @builder.js(ReportBuilder::DATA_DIR+"/flot/jquery.flot.min.js")
  
  anchor=@builder.graph_entry(@element.name)
  out="<a name='#{anchor}'></a><div id='graph_#{anchor}' style='width:#{@element.width}px; height:#{@element.height}px'> </div>"
  out << "<script>\n$.plot($('#graph_#{anchor}'),"
  out << @builder.parse_js(flot_series)+",\n" 
  out << @builder.parse_js(flot_options)+"\n);"
out+="</script>"
  
  @builder.html(out)
end

#gridObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reportbuilder/graph/html_flot.rb', line 84

def grid
  replace={}
  out=Hash.new
  @element.grid.each do |k,v|
    if replace.include? k
      out[replace[k]]=v
    else
      out[to_camel(k)]=v
    end
  end
  out
end

#hash_to_camel(h) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/reportbuilder/graph/html_flot.rb', line 42

def hash_to_camel(h)
  out=Hash.new
  h.each do |k,v|
    out[to_camel(k)]=v
  end
  out
end

#legendObject



76
77
78
79
80
# File 'lib/reportbuilder/graph/html_flot.rb', line 76

def legend
  lo=@element.legend
  lo.delete(:position) if lo[:position] and !["ne","nw","se","sw"].include? lo[:position]
  hash_to_camel(lo)
end

#process_type(out_opt, k, v) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/reportbuilder/graph/html_flot.rb', line 97

def process_type(out_opt, k,v)
  out_opt[:color]=v[:color] if !out_opt[:color]
  out_opt[:shadowSize]=v[:shadow_depth] if v[:shadow_depth]
  prev=out_opt[k]
  if prev and prev.is_a? Hash
    out_opt[k]=out_opt[k].merge(hash_to_camel(v))
  else
    out_opt[k]=hash_to_camel(v)
  end
end

#series_defaultsObject



81
82
83
# File 'lib/reportbuilder/graph/html_flot.rb', line 81

def series_defaults
  series_js(@element.series_defaults)
end

#series_js(in_opt) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/reportbuilder/graph/html_flot.rb', line 107

def series_js(in_opt)
  out_opt=Hash.new        
  out_opt[:lines]=Hash.new
  out_opt[:points]=Hash.new
  out_opt[:bars]=Hash.new
  in_opt.each_pair do |k,v|
    case k
      when :type
        set_type(out_opt, v)
      when :bars
        v.delete(:width) # Uses width as number of ticks
        process_type(out_opt, k,v)
      when :markers
        process_type(out_opt, k,v)
        out_opt[:points]=out_opt.delete(:markers)
      when :lines
        v[:line_width]=v[:width] if v[:width]
        process_type(out_opt, k,v)
      else
        out_opt[to_camel(k)]=v
    end
  end
  [:lines, :points, :bars].each do |v|
    out_opt.delete(v) if out_opt[v].size==0
  end
  out_opt
end

#set_type(opt, v) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/reportbuilder/graph/html_flot.rb', line 20

def set_type(opt,v)
  case v
  when :line
    opt[:lines][:show]=true        
  when :pie
    raise "doesn't supported on flot"
  when :scatter
    opt[:lines][:show]=false
    opt[:points][:show]=true
  when :bar
    opt[:lines][:show]=false
    opt[:bars][:show]=true
  when :histogram
    opt[:lines][:show]=false
    opt[:bars][:show]=true
    opt[:bars][:align]="center"
  else
    raise "Type doesn't exists"
  end
end

#to_camel(k) ⇒ Object



49
50
51
# File 'lib/reportbuilder/graph/html_flot.rb', line 49

def to_camel(k)
  k.to_s.gsub(/_([a-z])/) {|s| s.upcase[1]}.to_sym
end

#xaxisObject



70
71
72
# File 'lib/reportbuilder/graph/html_flot.rb', line 70

def xaxis
  hash_to_camel(@element.xaxis)
end

#yaxisObject



73
74
75
# File 'lib/reportbuilder/graph/html_flot.rb', line 73

def yaxis
  hash_to_camel(@element.yaxis)
end