Class: Geoptima::Chart
- Inherits:
-
Object
- Object
- Geoptima::Chart
- Defined in:
- lib/geoptima/chart.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_OPTIONS =
{:show_points => true, :show_lines => true, :title => nil, :width => 800, :margins => 20, :font_size => 14}
Instance Attribute Summary collapse
-
#chart ⇒ Object
Returns the value of attribute chart.
-
#chart_type ⇒ Object
readonly
Returns the value of attribute chart_type.
-
#data ⇒ Object
Returns the value of attribute data.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .available? ⇒ Boolean
- .bar(options = {}) ⇒ Object
- .draw_category_chart(legend, keys, values, options = {}) ⇒ Object
- .draw_grouped_chart(legends, keys, values, options = {}) ⇒ Object
- .draw_histogram_chart(legend, keys, values, options = {}) ⇒ Object
- .draw_line_chart(legend, keys, values, options = {}) ⇒ Object
- .engine ⇒ Object
- .engine=(name) ⇒ Object
- .libs ⇒ Object
- .line(options = {}) ⇒ Object
- .make_chart(chart_type, options = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(chart_type, options = {}) ⇒ Chart
constructor
A new instance of Chart.
Constructor Details
#initialize(chart_type, options = {}) ⇒ Chart
Returns a new instance of Chart.
32 33 34 35 |
# File 'lib/geoptima/chart.rb', line 32 def initialize(chart_type,={}) @chart_type = chart_type @options = DEFAULT_OPTIONS.merge() end |
Instance Attribute Details
#chart ⇒ Object
Returns the value of attribute chart.
31 32 33 |
# File 'lib/geoptima/chart.rb', line 31 def chart @chart end |
#chart_type ⇒ Object (readonly)
Returns the value of attribute chart_type.
30 31 32 |
# File 'lib/geoptima/chart.rb', line 30 def chart_type @chart_type end |
#data ⇒ Object
Returns the value of attribute data.
31 32 33 |
# File 'lib/geoptima/chart.rb', line 31 def data @data end |
#options ⇒ Object
Returns the value of attribute options.
31 32 33 |
# File 'lib/geoptima/chart.rb', line 31 def @options end |
Class Method Details
.available? ⇒ Boolean
26 27 28 |
# File 'lib/geoptima/chart.rb', line 26 def self.available? $chart_libs.length>0 end |
.bar(options = {}) ⇒ Object
96 97 98 |
# File 'lib/geoptima/chart.rb', line 96 def self.(={}) make_chart(:bar,) end |
.draw_category_chart(legend, keys, values, options = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/geoptima/chart.rb', line 84 def self.draw_category_chart(legend,keys,values,={}) puts "Creating category chart with keys: #{keys.join(',')}" puts "Creating category chart with values: #{values.join(',')}" g = make_chart(:bar, ) keys.each_with_index do |key,index| puts "\t Adding category #{key} with value #{values[index]}" g.data(key, values[index]) end g.minimum_value = 0 [:filename] && g.write([:filename]) g end |
.draw_grouped_chart(legends, keys, values, options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/geoptima/chart.rb', line 72 def self.draw_grouped_chart(legends,keys,values,={}) puts "Creating a chart with legends #{legends.inspect} for #{keys.length} keys" chart_type = ([:chart_type]==:line) ? :line : :bar g = make_chart(chart_type, ) legends.each do |legend| g.data(legend, values[legend]) end g.minimum_value = 0 g.labels = keys.inject({}){|a,v| a[a.length] = v.to_s;a} [:filename] && g.write([:filename]) g end |
.draw_histogram_chart(legend, keys, values, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/geoptima/chart.rb', line 54 def self.draw_histogram_chart(legend,keys,values,={}) puts "Creating a chart with legend #{legend} for #{keys.length} keys and #{values.length} values" chart_type = [:side] ? :side_bar : :bar g = make_chart(chart_type, ) g.data(legend, values) g.minimum_value = 0 mod = 1 unless [:side] while(keys.length/mod > 10) do mod+=1 end end labels = {} keys.each_with_index{|v,i| labels[i] = v.to_s if(i%mod == 0)} g.labels = labels [:filename] && g.write([:filename]) g end |
.draw_line_chart(legend, keys, values, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/geoptima/chart.rb', line 45 def self.draw_line_chart(legend,keys,values,={}) g = make_chart(:line,{:show_points => false}.merge()) g.data(legend, values) g.labels= {0=>keys[0].to_s, (keys.length-1)=>keys[-1].to_s} [:maximum_value] && g.maximum_value = [:maximum_value].to_i [:minimum_value] && g.minimum_value = [:minimum_value].to_i [:filename] && g.write([:filename]) g end |
.engine ⇒ Object
36 37 38 |
# File 'lib/geoptima/chart.rb', line 36 def self.engine @@engine ||= $chart_libs[0] || 'not available' end |
.engine=(name) ⇒ Object
39 40 41 |
# File 'lib/geoptima/chart.rb', line 39 def self.engine=(name) @@engine = $chart_libs.grep(/name/)[0] || engine end |
.libs ⇒ Object
23 24 25 |
# File 'lib/geoptima/chart.rb', line 23 def self.libs $chart_libs end |
.line(options = {}) ⇒ Object
42 43 44 |
# File 'lib/geoptima/chart.rb', line 42 def self.line(={}) make_chart(:line,) end |
.make_chart(chart_type, options = {}) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/geoptima/chart.rb', line 99 def self.make_chart(chart_type,={}) case engine when 'gruff' GruffChart.new(chart_type,) else puts "Unsupported chart engine: #{@@engine}" end end |