Class: Geoptima::Chart

Inherits:
Object
  • Object
show all
Defined in:
lib/geoptima/chart.rb

Direct Known Subclasses

GruffChart

Constant Summary collapse

DEFAULT_OPTIONS =
{:show_points => true, :show_lines => true, :title => nil, :width => 800, :margins => 20, :font_size => 14}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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,options={})
  @chart_type = chart_type
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#chartObject

Returns the value of attribute chart.



31
32
33
# File 'lib/geoptima/chart.rb', line 31

def chart
  @chart
end

#chart_typeObject (readonly)

Returns the value of attribute chart_type.



30
31
32
# File 'lib/geoptima/chart.rb', line 30

def chart_type
  @chart_type
end

#dataObject

Returns the value of attribute data.



31
32
33
# File 'lib/geoptima/chart.rb', line 31

def data
  @data
end

#optionsObject

Returns the value of attribute options.



31
32
33
# File 'lib/geoptima/chart.rb', line 31

def options
  @options
end

Class Method Details

.available?Boolean

Returns:

  • (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.bar(options={})
  make_chart(:bar,options)
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,options={})
  puts "Creating category chart with keys: #{keys.join(',')}"
  puts "Creating category chart with values: #{values.join(',')}"
  g = make_chart(:bar, options)
  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
  options[:filename] && g.write(options[: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,options={})
  puts "Creating a chart with legends #{legends.inspect} for #{keys.length} keys"
  chart_type = (options[:chart_type]==:line) ? :line : :bar
  g = make_chart(chart_type, options)
  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}
  options[:filename] && g.write(options[: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,options={})
  puts "Creating a chart with legend #{legend} for #{keys.length} keys and #{values.length} values"
  chart_type = options[:side] ? :side_bar : :bar
  g = make_chart(chart_type, options)
  g.data(legend, values)
  g.minimum_value = 0
  mod = 1
  unless options[: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
  options[:filename] && g.write(options[: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,options={})
  g = make_chart(:line,{:show_points => false}.merge(options))
  g.data(legend, values)
  g.labels= {0=>keys[0].to_s, (keys.length-1)=>keys[-1].to_s}
  options[:maximum_value] && g.maximum_value = options[:maximum_value].to_i
  options[:minimum_value] && g.minimum_value = options[:minimum_value].to_i
  options[:filename] && g.write(options[:filename])
  g
end

.engineObject



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

.libsObject



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(options={})
  make_chart(:line,options)
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,options={})
  case engine
  when 'gruff'
    GruffChart.new(chart_type,options)
  else
    puts "Unsupported chart engine: #{@@engine}"
  end
end