Alex Gutteridge
8/16/2007 7:41:00 AM
On 16 Aug 2007, at 15:21, Michael Linfield wrote:
> this is rather a followup from my other post...in the attempt to
> try and
> graph a quick sample data i get richeous errors lol.
>
> require 'rubygems'
> require 'ruport'
> require 'ruport/util'
> require 'tempfile'
>
> class GraphReport < Ruport::Report
>
> renders_as_graph
> isp1r = []
> isp1 = Tempfile.new('tempisp')
> isp1.puts File.readlines('maindata.csv').grep(/Blah1/)
> isp1.each do |row|
> isp1r << row["AVERAGE"]
> isp1.close
>
>
> def generate
> graph = Ruport::Graph(:column_names =>
> %w[24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0])
> graph.add_line [isp1r], :name => "Blah1"
> graph.add_line [14,54,22,54,12], :name => "bar" # Just an extra
> line,no
> reason
> return graph
> end
> end
> end
>
> puts GraphReport.generate{|r| r.save_as("filename.svg")}
>
>
> ######
>
> theoretically this should have generated 2 lines, 0 - 24 on the bottom
> of the graph, and the Blah1 grep data up the side.
>
> instead i get some awesome errors
No offence, but there are a lot of problems with the script you
posted! Check your do/end pairs and your usage of tempfile. I still
don't think you need a temporary file though, because Ruport/
FasterCSV can parse from a string.
Using this csv file:
Name,Average
Bill,2
Bob,1
Blah1,10
Jill,3
Jane,4
Blah1,20
Ada,6
Blah1,30
Ruby,7
Blah1,40
Sigfried,2
Blah1,50
Does this code help?:
require 'rubygems'
require 'ruport'
require 'ruport/util'
require 'tempfile'
class GraphReport < Ruport::Report
renders_as_graph
def generate
data = File.readlines('filename.csv') #Read in csv (into Array)
head = data[0] #Take header line
data = data.grep(/Blah/) #Grep out lines we are interested in
csv = ([head] + data).join #Combine the header and lines back
into a String
table = Ruport::Data::Table.parse(csv) #Parse into ruport
graph = Ruport::Graph() #Plot
graph.add_line table.column('Average').map{|x|x.to_i}, :name =>
"Blah1"
graph.add_line [14,54,22,54,12], :name => "bar" # Just an extra
line,no
return graph
end
end
puts GraphReport.generate{|r| r.save_as("filename.svg")}
Alex Gutteridge
Bioinformatics Center
Kyoto University