The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Venn::Chart - Create a Venn diagram using GD.

SYNOPSIS

  #!/usr/bin/perl
  use warnings;
  use Carp;
  use strict;
  
  use Venn::Chart;
  
  # Create the Venn::Chart constructor
  my $venn_chart = Venn::Chart->new( 400, 400 ) or die("error : $!");
  
  # Set a title and a legend for our chart
  $venn_chart->set_options( -title => 'Venn diagram' );
  $venn_chart->set_legends( 'Team 1', 'Team 2', 'Team 3' );
  
  # 3 lists for the Venn diagram
  my @team1 = qw/abel edward momo albert jack julien chris/;
  my @team2 = qw/edward isabel antonio delta albert kevin jake/;
  my @team3 = qw/gerald jake kevin lucia john edward/;
  
  # Create a diagram with gd object
  my $gd_venn = $venn_chart->plot( \@team1, \@team2, \@team3 );
  
  # Create a Venn diagram image in png, gif and jpeg format
  open my $fh_venn, '>', 'VennChart.png' or die("Unable to create png file\n");
  binmode $fh_venn;
  print {$fh_venn} $gd_venn->png;
  close $fh_venn or die('Unable to close file');
  
  # Create an histogram image of Venn diagram (png, gif and jpeg format)
  my $gd_histogram = $venn_chart->plot_histogram;
  open my $fh_histo, '>', 'VennHistogram.png' or die("Unable to create png file\n");
  binmode $fh_histo;
  print {$fh_histo} $gd_histogram->png;
  close $fh_histo or die('Unable to close file');
  
  # Get data list for each intersection or unique region between the 3 lists
  my @ref_lists = $venn_chart->get_list_regions();
  my $list_number = 1;
  foreach my $ref_region ( @ref_lists ) {
    print "List $list_number : @{ $ref_region }\n";
    $list_number++;
  }

DESCRIPTION

Venn::Chart create a Venn diagram image using GD module with 2 or 3 data lists. A title and a legend can be added in the chart. It is possible to create an histogram chart with the different data regions of Venn diagram using GD::Graph module.

CONSTRUCTOR/METHODS

new

This constructor allows you to create a new Venn::Chart object.

$venn_chart = Venn::Chart->new($width, $height)

The new() method is the main constructor for the Venn::Chart module. It creates a new blank image of the specified width and height.

  # Create Venn::Chart constructor
  my $venn_chart = Venn::Chart->new( 400, 400 );

The default width and height size are 500 pixels.

set

Do not use this method. It has been replaced by set_options method.

set_options

Set the image title and colors of the diagrams.

$venn_chart->set_options( -attrib => value, ... )

-title => string

Specifies the title.

  -title => 'Venn diagram',
-colors => array reference

Specifies the RGBA colors of the 2 or 3 lists. This allocates a color with the specified red, green, and blue components, plus the specified alpha channel for each circle. The alpha value may range from 0 (opaque) to 127 (transparent). The alphaBlending function changes the way this alpha channel affects the resulting image.

    -colors => [ [ 98, 66, 238, 0 ], [ 98, 211, 124, 0 ], [ 110, 205, 225, 0 ] ],

Default : [ [ 189, 66, 238, 0 ], [ 255, 133, 0, 0 ], [ 0, 107, 44, 0 ] ]

  $venn_chart->set_options( 
    -title  => 'Venn diagram',
    -colors => [ [ 98, 66, 238, 0 ], [ 98, 211, 124, 0 ], [ 110, 205, 225, 0 ] ],
  );

set_legends

Set the image legends. This method set a legend which represents the title of each 2 or 3 diagrams (circles).

$venn_chart->set_legends( legend1, legend2, legend3 )

  # Set title and a legend for our chart
  $venn_chart->set_legends('Diagram1', 'Diagram2', 'Diagram3');

plot

Plots the chart, and returns the GD::Image object.

$venn_chart->plot( array reference list )

  my $gd = $venn_chart->plot(\@list1, \@list2, \@list3);

To create your image, do whatever your current version of GD allows you to do to save the file. For example:

  open my $fh_image, '>', 'venn.png' or die("Error : $!");
  binmode $fh_image;
  print {$fh_image} $gd->png;
  close $fh_image or die('Unable to close file');

get_list_regions

Get a list of array reference which contains data for each intersection or unique region between the 2 or 3 lists.

$venn_chart->get_list_regions()

Case : 2 lists
  my $gd_venn   = $venn_chart->plot( \@team1, \@team2 );
  my @ref_lists = $venn_chart->get_list_regions();

@ref_lists will contain 3 array references.

  @{ $ref_lists[0] } => unique elements of @team1 between @team1 and @team2    
  @{ $ref_lists[1] } => unique elements of @team2 between @team1 and @team2
  @{ $ref_lists[2] } => intersection elements between @team1 and @team2   
Case : 3 lists
  my $gd_venn   = $venn_chart->plot( \@team1, \@team2, \@team3 );
  my @ref_lists = $venn_chart->get_list_regions();

@ref_lists will contain 7 array references.

  @{ $ref_lists[0] } => unique elements of @team1 between @team1, @team2 and @team3    
  @{ $ref_lists[1] } => unique elements of @team2 between @team1, @team2 and @team3  
  @{ $ref_lists[2] } => intersection elements between @team1 and @team2   
  @{ $ref_lists[3] } => unique elements of @team3 between @team1, @team2 and @team3  
  @{ $ref_lists[4] } => intersection elements between @team3 and @team1  
  @{ $ref_lists[5] } => intersection elements between @team3 and @team2  
  @{ $ref_lists[6] } => intersection elements between @team1, @team2 and @team3

Example :

  my @team1 = qw/abel edward momo albert jack julien chris/;
  my @team2 = qw/edward isabel antonio delta albert kevin jake/;
  my @team3 = qw/gerald jake kevin lucia john edward/;
    
  my $gd_venn = $venn_chart->plot( \@team1, \@team2, \@team3 );
  my @lists   = $venn_chart->get_list_regions();

  Result of @lists
  [ 'jack', 'momo', 'chris', 'abel', 'julien' ], # Unique of @team1
  [ 'delta', 'isabel', 'antonio' ],              # Unique of @team2
  [ 'albert' ],                                  # Intersection between @team1 and @team2
  [ 'john', 'gerald', 'lucia' ],                 # Unique of @team3
  [],                                            # Intersection between @team3 and @team1
  [ 'jake', 'kevin' ],                           # Intersection between @team3 and @team2
  [ 'edward' ]                                   # Intersection between @team1, @team2 and @team3

get_regions

Get an array displaying the object number of each region of the Venn diagram.

$venn_chart->get_regions()

  my $gd_venn = $venn_chart->plot( \@team1, \@team2, \@team3 );
  my @regions = $venn_chart->get_regions;

  @regions contains 5, 3, 1, 3, 0, 2, 1

get_colors_regions

Get an array contains the colors (in an array reference) used for each region in Venn diagram.

$venn_chart->get_colors_regions()

  my @colors_regions = $venn_chart->get_colors_regions;

  @colors_regions = (
    [R, G, B, A], [R, G, B, A], [R, G, B, A],
    [R, G, B, A], [R, G, B, A], [R, G, B, A],
    [R, G, B, A]
  ); 


  @{ $colors_regions[0] } => color of @{ $ref_lists[0] }    
  @{ $colors_regions[1] } => color of @{ $ref_lists[1] }
  @{ $colors_regions[2] } => color of @{ $ref_lists[2] }   
  @{ $colors_regions[3] } => color of @{ $ref_lists[3] }  
  @{ $colors_regions[4] } => color of @{ $ref_lists[4] }
  @{ $colors_regions[5] } => color of @{ $ref_lists[5] }
  @{ $colors_regions[6] } => color of @{ $ref_lists[6] }

plot_histogram

Plots an histogram displaying each region of the Venn diagram which returns the GD::Image object.

$venn_chart->plot_histogram

To create the histogram, the Venn diagram have to be already created.

  # Create histogram of Venn diagram image in png, gif and jpeg format
  my $gd_histogram = $venn_chart->plot_histogram;
  
  open my $fh_histo, '>', 'VennHistogram.png' or die('Unable to create file');
  binmode $fh_histo;
  print {$fh_histo} $gd_histogram->png;
  close $fh_histo or die('Unable to close file');

If you want to create and design the histogram yourself, use GD::Graph module and play with data obtained with "get_regions" methods.

AUTHOR

Djibril Ousmanou, <djibel at cpan.org>

BUGS

Please report any bugs or feature requests to bug-venn-chart at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Venn-Chart. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SEE ALSO

See GD, GD::Graph

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Venn::Chart

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2011 Djibril Ousmanou.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.