Module: Grapher

Extended by:
Grapher
Included in:
Grapher
Defined in:
lib/grapher.rb

Instance Method Summary collapse

Instance Method Details

#generate_report(report_type, csv_file, outfile) ⇒ Object

Generates a single report given by name. Uses the yml file for

report names


44
45
46
47
48
# File 'lib/grapher.rb', line 44

def generate_report(report_type, csv_file, outfile)
  puts "Generating #{report_type} to #{outfile}..."
  columns = (reports[report_type] or reports[reports.keys.first])
  save_graph(csv_file, columns, outfile, :title => report_type) 
end

#generate_reports(options) ⇒ Object

Parses command line options and creates one or a bunch of

reports, stores them in the given directory, and advises
the user to go ahead and open them


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/grapher.rb', line 15

def generate_reports(options)

  # Let's keep things clean
  prefix = Time.now.strftime("%Y_%m_%d_%H_%M")

  # Generate a single report or all of them?
  report_keys = reports(options[:report_definitions]).keys
  report_keys = [options[:report]] if report_keys.include?(options[:report])

  # Generate report(s)
  report_keys.each do |report|
    begin
      outfile = File.join(options[:output_dir], "#{prefix}_#{report}.png")
      generate_report(report, options[:csv_file], outfile)
    rescue => e
      puts "Error generating #{report}: #{e.inspect}"
    end
  end    

  # Tell user what to do next
  puts "~"*80
  puts "Great, now open the images with"
  puts "  open #{File.join(options[:output_dir], prefix)}*.png"
end

#graph(csv_file, columns, options = {}) ⇒ Object

Creates a graph from a csv file



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/grapher.rb', line 64

def graph(csv_file, columns, options = {})
  table = Table(csv_file)

  # Prepare data structure
  data = Hash.new
  labels = table.column "rate"
  columns.each_index do |i|
    next unless i%2==0
    data[columns[i]] = table.column columns[i+1]
  end

  # Draw graph
  g = line_graph( options[:title], data, labels )
end

#reports(report = nil, yaml_file = File.join(File.dirname(__FILE__), "reports.yml")) ⇒ Object

Reads a YAML file that defines how reports are built



82
83
84
# File 'lib/grapher.rb', line 82

def reports(report = nil, yaml_file = File.join(File.dirname(__FILE__), "reports.yml"))
  y = YAML.load(File.read(yaml_file)) 
end

#save_graph(csv_file, columns, outfile, options = {}) ⇒ Object

Creates and saves a graph



53
54
55
56
57
58
59
# File 'lib/grapher.rb', line 53

def save_graph(csv_file, columns, outfile, options = {})
  # Draw graph
  g = graph(csv_file, columns, :title => options[:title] )

  # Save graph
  g.write(outfile)
end