Class: ReportBuilder::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/reportbuilder/graph.rb,
lib/reportbuilder/graph/html_flot.rb,
lib/reportbuilder/graph/html_jqplot.rb

Overview

Usage

graph=ReportBuilder::Graph.new(:name=>“Graph”, ) do |g|

g.serie :x1, :data=>[1,2,3,4], :label=>"Data 1" # Automatic asignation of x axis
g.data :x2, :data=>[[1,2],[2,3],[4,5]], :label=>"Data 2" # Manual asignation of x axis
g.xaxis :label=>"X axis", :autoscale=>true # jqPlot like asignation

end

Defined Under Namespace

Classes: HtmlFlot, HtmlJqplot

Constant Summary collapse

@@n =

:nodoc:

1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = Hash.new, &block) ⇒ Graph

Returns a new instance of Graph.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/reportbuilder/graph.rb', line 123

def initialize(options=Hash.new, &block)
  @number=@@n
  @@n+=1
  if !options.has_key? :name
    @name="Graph #{@@n}"
  else
    @name=options.delete(:name)
  end
  @series_data_hash=Hash.new
  @series_options_hash=Hash.new
  @series_defaults=Hash.new
  @xaxis=Hash.new
  @yaxis=Hash.new
  @grid=Hash.new
  @legend=Hash.new
  @axes_defaults=Hash.new
  @title={:text=>@name}
  default_options={
    :width=>600,
    :height=>400,
    :html_engine=>:jqplot,
    :generic_engine=>:rchart
  }
  @options=default_options.merge(options)
  @options.each {|k,v|
    self.send("#{k}=",v) if self.respond_to? k
  }
  
  if block
    block.arity<1 ? self.instance_eval(&block) : block.call(self)
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



118
119
120
# File 'lib/reportbuilder/graph.rb', line 118

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



119
120
121
# File 'lib/reportbuilder/graph.rb', line 119

def number
  @number
end

#series_data_hashObject (readonly)

Returns the value of attribute series_data_hash.



119
120
121
# File 'lib/reportbuilder/graph.rb', line 119

def series_data_hash
  @series_data_hash
end

#series_options_hashObject (readonly)

Returns the value of attribute series_options_hash.



119
120
121
# File 'lib/reportbuilder/graph.rb', line 119

def series_options_hash
  @series_options_hash
end

#xmaxObject (readonly)

Returns the value of attribute xmax.



122
123
124
# File 'lib/reportbuilder/graph.rb', line 122

def xmax
  @xmax
end

#xminObject (readonly)

Returns the value of attribute xmin.



122
123
124
# File 'lib/reportbuilder/graph.rb', line 122

def xmin
  @xmin
end

#ymaxObject (readonly)

Returns the value of attribute ymax.



122
123
124
# File 'lib/reportbuilder/graph.rb', line 122

def ymax
  @ymax
end

#yminObject (readonly)

Returns the value of attribute ymin.



122
123
124
# File 'lib/reportbuilder/graph.rb', line 122

def ymin
  @ymin
end

Class Method Details

.attr_accessor_dsl(*attr) ⇒ Object

Allows to define an reader/writer function Without parameters, retrieve the value for a instance variable With parameters, set the value for instance variable



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/reportbuilder/graph.rb', line 100

def self.attr_accessor_dsl(*attr)
  attr.each  do |sym| 
    sym_w_sm=sym.to_s.gsub(":","")
    define_method(sym) do |*args|
      if args.size==0
        instance_variable_get("@#{sym_w_sm}")
      else
        instance_variable_set("@#{sym_w_sm}", args[0])
      end
    end
    
    define_method(sym.to_s+"=") do |*args|
      instance_variable_set("@#{sym_w_sm}", args[0])
    end
  end
end

Instance Method Details

#actuals_minmaxObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/reportbuilder/graph.rb', line 207

def actuals_minmax
  if @actuals_minmax.nil?
    @actuals_minmax=Hash.new
    series_data.each do |data|
      data.each do |v|
        @actuals_minmax[:xmin]||=v[0]
        @actuals_minmax[:xmax]||=v[0]
        @actuals_minmax[:ymin]||=v[1]
        @actuals_minmax[:ymax]||=v[1]
        @actuals_minmax[:xmin]=v[0] if @actuals_minmax[:xmin]>v[0]
        @actuals_minmax[:xmax]=v[0] if @actuals_minmax[:xmax]<v[0]
        @actuals_minmax[:ymin]=v[1] if @actuals_minmax[:ymin]>v[1]
        @actuals_minmax[:ymax]=v[1] if @actuals_minmax[:ymax]<v[1]
      end
    end
  end
  @actuals_minmax
end

#data(name, *d) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/reportbuilder/graph.rb', line 173

def data(name, *d)
  i=0
  if d.size==0
    @series_data_hash[name]
  else
    @series_data_hash[name]=d.map {|v|
      i+=1
      if v.is_a? Array
        v
      else
        [i,v]
      end
    }
  end
end

#define_xy_rangesObject



226
227
228
229
230
231
232
233
234
235
# File 'lib/reportbuilder/graph.rb', line 226

def define_xy_ranges
  @xmin=xaxis[:min] if xaxis[:min]
  @xmax=xaxis[:max] if xaxis[:max]
  @ymin=yaxis[:min] if yaxis[:min]
  @ymin=yaxis[:max] if yaxis[:max]
  @xmin||=actuals_minmax[:xmin]
  @xmax||=actuals_minmax[:xmax]
  @ymin||=actuals_minmax[:ymin]
  @ymax||=actuals_minmax[:ymax]
end

#report_building(builder) ⇒ Object



245
246
247
248
249
250
# File 'lib/reportbuilder/graph.rb', line 245

def report_building(builder)
  require "reportbuilder/graph/#{generic_engine}"
  klass=(generic_engine.capitalize).to_sym
  graph_builder = ReportBuilder::Graph.const_get(klass).new(builder, self)
  graph_builder.generate
end

#report_building_html(builder) ⇒ Object



239
240
241
242
243
244
# File 'lib/reportbuilder/graph.rb', line 239

def report_building_html(builder)
  require "reportbuilder/graph/html_#{html_engine}"
  klass=("Html"+html_engine.to_s.capitalize).to_sym
  graph_builder=ReportBuilder::Graph.const_get(klass).new(builder, self)
  graph_builder.generate
end

#serie(name, d) ⇒ Object



200
201
202
203
204
205
# File 'lib/reportbuilder/graph.rb', line 200

def serie(name,d)
  raise "You should define :data key" unless d[:data]
  da=d.delete(:data)
  data(name,*da)
  serie_options(name,d)
end

#serie_options(name, h) ⇒ Object



196
197
198
199
# File 'lib/reportbuilder/graph.rb', line 196

def serie_options(name, h)
  set_marker_size(h) if h[:markers] and (h[:markers].keys & [:size, :radius, :diameter]).size > 0
  @series_options_hash[name]=h
end

#series_dataObject



162
163
164
165
166
# File 'lib/reportbuilder/graph.rb', line 162

def series_data
  series_id.map {|v|
    @series_data_hash[v]
  }
end

#series_idObject



159
160
161
# File 'lib/reportbuilder/graph.rb', line 159

def series_id
  @series_data_hash.keys.sort {|a,b| a.to_s<=>b.to_s}
end

#series_optionsObject



167
168
169
170
171
# File 'lib/reportbuilder/graph.rb', line 167

def series_options
  series_id.map {|v|
    @series_options_hash[v]
  }
end

#set_marker_size(h) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/reportbuilder/graph.rb', line 188

def set_marker_size(h)
  d=h[:markers][:diameter]  if h[:markers][:diameter]
  d=h[:markers][:size]      if h[:markers][:size]
  d=2*h[:markers][:radius]  if h[:markers][:radius]
  h[:markers][:diameter]=d
  h[:markers][:radius]=d/2.0
  h[:markers][:size]=d
end