App-Basis-ConvertText2

 view release on metacpan or  search on metacpan

lib/App/Basis/ConvertText2/Plugin/Venn.pm  view on Meta::CPAN


=head1 NAME

App::Basis::ConvertText2::Plugin::Venn

=head1 SYNOPSIS

    my $content = "abel edward momo albert jack julien chris
    edward isabel antonio delta albert kevin jake
    gerald jake kevin lucia john edward" ;
    my $params = { 
        title   => "sample venn diagram",
        legends => "team1 team2 team3",
        scheme  => "rgb", 
        explain => '1'
    } ;
    my $obj = App::Basis::ConvertText2::Plugin::Venn->new() ;
    my $out = $obj->process( 'venn', $content, $params) ;

=head1 DESCRIPTION

Convert a text string of comma separated numbers into a Venn diagran image PNG

=cut

# ----------------------------------------------------------------------------

package App::Basis::ConvertText2::Plugin::Venn;
$App::Basis::ConvertText2::Plugin::Venn::VERSION = '0.4';
use 5.10.0;
use strict;
use warnings;
use GD;

# we need to do this to ensure that venn::chart uses the right level of color
GD::Image->trueColor(0);
use Venn::Chart;
use Path::Tiny;
use Moo;
use App::Basis;
use App::Basis::ConvertText2::Support;
use namespace::autoclean;

has handles => (
    is       => 'ro',
    init_arg => undef,
    default  => sub {[qw{venn}]}
);

# -----------------------------------------------------------------------------

my %_colour_schemes = (
    default => [ [ 189,  66, 238, 0 ],   [ 255,  133,  0,    0 ],   [ 0,   107,  44,  0 ] ],
    rgb     => [ [ 0x99, 00, 00,  40 ],  [ 0x33, 0x99, 0xcc, 40 ],  [ 0,   0x66, 0,   40 ] ],
    rgb1    => [ [ 0x99, 00, 00,  240 ], [ 0x33, 0x99, 0xcc, 240 ], [ 0,   0x66, 0,   240 ] ],
    rgb2    => [ [ 0x99, 00, 00,  0 ],   [ 0x33, 0x99, 0xcc, 0 ],   [ 0,   0x66, 0,   0 ] ],
    blue    => [ [ 98,   66, 238, 0 ],   [ 98,   211,  124,  0 ],   [ 110, 205,  225, 0 ] ],
);

# -----------------------------------------------------------------------------

=item venn

create a simple venn diagram image, with some nice defaults, returns some 
markdown explaining the diagram, undex/empty if errors

 parameters
    text   - 2 or 3 space separated lines of items for the venn
    filename - filename to save the created image as 

    hashref params of
        title   - title for the image
        legends - legends to match the lines
        size    - size of image, default 400x400, widthxheight - optional
        scheme - color scheme

=cut

sub process {
    my $self = shift;
    my ( $tag, $content, $params, $cachedir ) = @_;
    $params->{size}    ||= "";
    $params->{title}   ||= "";
    $params->{legends} ||= "";
    $params->{size}    ||= "400x400";
    $params->{scheme}  ||= 'default';
    $params->{scheme} = lc( $params->{scheme} );
    my ( $w, $h ) = ( $params->{size} =~ /^\s*(\d+)\s*x\s*(\d+)\s*$/ );

    if ( !$h ) {
        $w = 400;
        $h = 400;
    }
    return "" if ( !$content );

    # we can use the cache or process everything ourselves
    my $sig = create_sig( $content, $params );
    my $filename = cachefile( $cachedir, "$sig.png" );

    # we will not check for the cachefile as we need to create the venn object
    # each time to get the explaination text, besides not many people will
    # use this plugin, so lets not go to the extra effort



( run in 1.391 second using v1.01-cache-2.11-cpan-39bf76dae61 )