Class: ReportBuilder::Graph::HtmlJqplot

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

Instance Method Summary collapse

Methods inherited from ElementBuilder

#initialize

Constructor Details

This class inherits a constructor from ReportBuilder::ElementBuilder

Instance Method Details

#axes_defaultsObject



52
53
54
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 52

def axes_defaults
  hash_to_camel(@element.axes_defaults)
end

#generateObject



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

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

  @builder.js(ReportBuilder::DATA_DIR+"/jqplot/jquery-1.4.2.min.js")
  @builder.js(ReportBuilder::DATA_DIR+"/jqplot/jquery.jqplot.min.js")
  @builder.css(ReportBuilder::DATA_DIR+"/jqplot/jquery.jqplot.css")
  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>
$.jqplot('graph_#{anchor}', #{@builder.parse_js(@element.series_data)},
"+@builder.parse_js(jqplot_options)+");
</script>"
  
  @builder.html(out)
end

#gridObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 69

def grid
  replace={:show=>:drawGridLine, :color=>:gridLineColor, :background_color=>:background}
  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



81
82
83
84
85
86
87
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 81

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

#jqplot_optionsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 21

def jqplot_options
  opts=Hash.new
  opts[:title]=@element.title
  opts[:series]=series_options
  opts[:axesDefaults]=axes_defaults if @element.axes_defaults.size>0
  if @element.xaxis.size>0 or @element.yaxis.size>0
    opts[:axes]={:xaxis=>xaxis, :yaxis=>yaxis}
  end
  opts[:seriesDefaults]= 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

#legendObject



61
62
63
64
65
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 61

def legend
  lo=@element.legend
  lo[:location]=lo.delete(:position) if lo[:position] 
  hash_to_camel(lo)
end

#series_defaultsObject



66
67
68
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 66

def series_defaults
  series_js(@element.series_defaults)
end

#series_js(in_opt) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 91

def series_js(in_opt)
  out_opt=Hash.new
  out_opt[:markerOptions]=Hash.new
  out_opt[:rendererOptions]=Hash.new
  in_opt.each_pair do |k,v|
    case k
      when :type
        set_type(out_opt, v)
      when :lines
        out_opt[:showLines]=v[:show] if v[:show]              
        v.each do |k1,v1|
          out_opt[to_camel(k1)]=v1
        end
      when :markers
        out_opt[:showMarker]=v[:show] if v[:show]
        v.each do |k1,v1|
          out_opt[:markerOptions][to_camel(k1)]=v1
        end
      when :bars
        v.each do |k1,v1|
          k1=("bar_"+k1.to_s) if [:padding, :margin, :direction, :width].include? k1
          out_opt[:rendererOptions][to_camel(k1)]=v1
        end              
      when :color
        if in_opt[:type]
          set_color(in_opt[:type], out_opt,v)
        else
          out_opt[k]=v
        end
      else
        out_opt[k]=v
    end
  end
  out_opt
end

#series_optionsObject

Transform data options on jqPlot ones



138
139
140
141
142
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 138

def series_options
  @element.series_options.map do |in_opt|
    series_js(in_opt)
  end
end

#set_color(t, out_opt, v) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 126

def set_color(t,out_opt,v)
  case t
    when :line
      out_opt[:color]=v
    when :scatter
      out_opt[:markerOptions][:color]=v
    when :bar
      out_opt[:rendererOptions][:color]=v
  end
end

#set_type(opt, v) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 35

def set_type(opt,v)
  case v
  when :line
  when :pie
    @builder.js(ReportBuilder::DATA_DIR+"/jqplot/plugins/jqplot.pieRenderer.min.js")
    opt[:renderer]="$.jqplot.PieRenderer".to_sym
  when :scatter
    opt[:showLine]=false
    opt[:showMarker]=true
  when :bar
    @builder.js(ReportBuilder::DATA_DIR+"/jqplot/plugins/jqplot.categoryAxisRenderer.min.js")
    @builder.js(ReportBuilder::DATA_DIR+"/jqplot/plugins/jqplot.barRenderer.min.js")
    opt[:renderer]="$.jqplot.BarRenderer".to_sym
  else
    raise "Type doesn't exists"
  end
end

#to_camel(k) ⇒ Object



88
89
90
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 88

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

#xaxisObject



55
56
57
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 55

def xaxis
  hash_to_camel(@element.xaxis)
end

#yaxisObject



58
59
60
# File 'lib/reportbuilder/graph/html_jqplot.rb', line 58

def yaxis
  hash_to_camel(@element.yaxis)
end