Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2009/11/24

Making Graphs with Perl and Chart::Clicker

The Graph


The Code
  1. #!/usr/bin/perl  
  2.   
  3. use 5.010 ;  
  4. use strict ;  
  5. use warnings ;  
  6. use Chart::Clicker;  
  7. use Chart::Clicker::Data::Series;  
  8. use Chart::Clicker::Data::DataSet;  
  9.   
  10. my $pi = 3.14159265358979;  
  11.   
  12. sub deg_to_rad { ($_[0]/180) * $pi }  
  13. sub rad_to_deg { ($_[0]/$pi) * 180 }  
  14.   
  15. my @core = ( 0 .. 1_000 ) ;  
  16. my @sine ;  
  17. my @cosine ;  
  18. for my $a ( @core ) {  
  19.     push @sine   , sin(deg_to_rad($a)) ;  
  20.     push @cosine , cos(deg_to_rad($a)) ;  
  21.     }  
  22.   
  23. my $cc = Chart::Clicker->new;  
  24. my $series1 = Chart::Clicker::Data::Series->new(  
  25.     keys    => \@core ,  
  26.     values  => \@sine  
  27.     );  
  28. my $series2 = Chart::Clicker::Data::Series->new(  
  29.     keys    => \@core ,  
  30.     values  => \@cosine  
  31.     );  
  32. my $dataset = Chart::Clicker::Data::DataSet->new(  
  33.         series  =>      [ $series1 , $series2 ],  
  34.     );  
  35. $cc->add_to_datasets($dataset) ;  
  36. $cc->write_output('graph_test.png');  
  37. exit ;  

There's lots of ways to do graphs. The way we do some graphs here is to do a system call to R. R is powerful, sure, but there's no good way to set up a batch of complex graphs in Perl and send them to R for graphing. There are ways, sure, but not good ways. So, I expect to do a lot more with Chart::Clicker in the future.

No comments:

Post a Comment