Changeset 974

Show
Ignore:
Timestamp:
05/12/07 21:37:45 (2 years ago)
Author:
sandal
Message:

Ruport examples

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ruport-www/examples.html

    r972 r974  
    3333      applications, here a few basic examples and sample reports to give  
    3434      you an idea of how Ruport works. 
    35       </p>       
    36     </div> 
    37     <div id="secondaryContent"> 
     35      </p>  
     36 
     37      <a name="csv"><h3>Basic Grouping of CSV data</h3></a> 
     38         
     39      <p> 
     40      Say you want to load in a CSV from file and group it by people's names, 
     41      and get a PDF report back.  This is easy in Ruport. 
     42      </p>     
     43                    
     44      <p>Below is some sample data: (foo.csv)</p> 
     45 
     46<pre> 
     47  name,login time,machine 
     48  Gregory,10:00,bittle 
     49  Joe,11:45,soda 
     50  Jim,9:00,kitten 
     51  Joe,12:15,soda 
     52  Gregory,5:00,kitten 
     53  Joe,12:45,bittle        
     54</pre>   
     55 
     56<p>To do the report we mentioned before, we'd just do something like this:</p> 
     57 
     58<pre> 
     59  t = Table("foo.csv") 
     60  grouping = Grouping(t,:by => "name") 
     61  puts grouping.to_pdf 
     62</pre>       
     63 
     64<p> 
     65Directing the output of that script to a file results in nice output like this: 
     66</p>     
     67<div align="center"> 
     68  <a href="samples/csv_simple.pdf"><img src="samples/csv_simple.png"></a> 
     69</div> 
     70 
     71<p> 
     72There are actually several styles for output, and also, it's trivial to do HTML, 
     73Text, or CSV formatted output as well. 
     74</p> 
     75 
     76<a name="aar"><h3>A trivial dump of an ActiveRecord model to CSV</h3></a> 
     77 
     78<p> 
     79If you're looking to just grab a CSV dump of some model in your Rails app, 
     80Ruport might be the easiest way to do it.  Just add <tt>require "ruport"</tt> 
     81in your <tt>environment.rb</tt> file, and then the following code to your  
     82model: 
     83</p> 
     84 
     85<pre> 
     86  class MyModel < ActiveRecord::Base 
     87    acts_as_reportable 
     88  end 
     89</pre> 
     90 
     91<p> 
     92If you want a full dump, just do this: 
     93</p> 
     94 
     95<pre> 
     96  puts MyModel.report_table.to_csv 
     97</pre> 
     98 
     99<p> 
     100If you want to reduce via an AR find, you can pass along any of those options 
     101as well. 
     102</p> 
     103 
     104<pre> 
     105  puts MyModel.report_table(:all, :conditions => ["name = ?", "gregory"]).to_csv  
     106</pre> 
     107 
     108<p> 
     109Ruport can do a whole lot more tricks with ActiveRecord / Rails, including 
     110handle all types of associations and model methods.  For details, see 
     111the <a href="http://stonecode.svnrepository.com/ruport/trac.cgi/wiki/ActsAsReportable"> 
     112         AAR wiki page.</a> 
     113</p> 
     114      
     115<a name="svg"><h3>SVG Graphs with ruport-util</h3></a>  
     116 
     117<p> 
     118If you're using the ruport-util package, it's very easy to generate beautiful 
     119SVG graphs.  The functionality is still a bit basic, but for the most common 
     120needs, the output is quite nice (Thanks to Scruffy!) 
     121</p> 
     122 
     123<p> 
     124Here is a trivial graphing example:  
     125</p> 
     126<pre> 
     127  require "rubygems" 
     128  require "ruport" 
     129 
     130  require "ruport/util" 
     131   
     132  class GraphReport < Ruport::Report 
     133 
     134    def generate 
     135      graph = Ruport::Graph(:column_names => %w[a b c d e]) 
     136      graph.add_line [1,2,3,4,5], :name => "foo"  
     137      graph.add_line [11,22,70,2,19], :name => "bar" 
     138      graph.to_svg 
     139    end 
     140 
     141  end 
     142 
     143  GraphReport.run { |r| r.write("foo.svg") } 
     144</pre> 
     145 
     146<p>The output ends up looking something like this:</p> 
     147<div align="right"> 
     148        <a href="samples/foo.svg"><img src="samples/graph.png"/></a> 
     149</div>  
     150  
     151<p> 
     152Of course, if you need to display this easily in a browser, you might want 
     153to convert it to another format such as PNG or JPG.  We're looking for patches 
     154to make this easier, but haven't found solutions based on RMagick to be  
     155easy enough to support. 
     156</p> 
     157 
     158<h3>Enough Flashy Examples for Now</h3> 
     159 
     160<p> 
     161Hopefully we've at least sold you on the idea of using <tt>ruport</tt> 
     162for your project. Still, we'd like to take this chance to remind you that 
     163the real gains you'll see from Ruport are not at this high of a level, but  
     164rather deeper in the trenches.  We're aiming to provide a super light weight 
     165foundation to build reporting apps on top of, because we feel that's what 
     166Rubyists will best benefit from.  Hopefully, you'll find that to be true for 
     167your work. 
     168</p> 
     169 
     170<p> 
     171If you're convinced and ready to give Ruport a spin, have a look at some of 
     172the available <a href="resources.html">resources</a>, and then come say hello 
     173on the <a href="http://lists.rubyreports.org">Ruport mailing list</a>. 
     174 
     175Happy Hacking! 
     176</p>  
     177    </div> 
     178    <div id="secondaryContent">  
     179         <p><a href="#csv">Basic Grouping of CSV data</a></p> 
     180         <p><a href="#aar">A trivial dump of an ActiveRecord model to CSV</a></p> 
     181         <p><a href="#svg">SVG Graphs with ruport-util</a></p> 
    38182    </div> 
    39183    <div id="footer">