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

NAME

Term::Chart - Create attractive bar charts for the terminal interface.

SYNOPSIS

A Simple Horizontal Bar Chart

  my $tc = Term::Chart->new();

  $tc->add_values(map { $_**2 } 1 .. 9);

  print "$tc\n";

HTML approximation


▅▅▅▅
▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅

Vertical Dot Chart w/ Border and Labels

  my $tc = Term::Chart->new(
      {   orientation => 'vertical',
          style       => 'dot',
          border      => 1,
          tip         => "\x{29BF}",
      }
  );

  my @values = 0 .. 8;
  push @values, reverse 0 .. 7;

  for my $n (@values)
  {
      my %datum = (
          value => $n,
          label => $n,
      );
      $tc->add_value( \%datum );
  }

  print "$tc\n";

HTML approximation

┏━━━━━━━━━━━━━━━━━┓
┃       ⦿         
┃                 ┃
┃                 ┃
┃                 ┃
┃      ⦿ ⦿      
┃                 ┃
┃                 ┃
┃                 ┃
┃                 ┃
┃      ⦿  ⦿     
┃                 ┃
┃                 ┃
┃                 ┃
┃                 ┃
┃     ⦿    ⦿    
┃                 ┃
┃                 ┃
┃                 ┃
┃                 ┃
┃    ⦿      ⦿   
┃                 ┃
┃                 ┃
┃                 ┃
┃                 ┃
┃   ⦿        ⦿  
┃                 ┃
┃                 ┃
┃                 ┃
┃                 ┃
┃  ⦿          ⦿ 
┃                 ┃
┃                 ┃
┃                 ┃
┃                 ┃
┃ ⦿            ⦿
┃                 ┃
┃                 ┃
┃                 ┃
┃                 ┃
⦿              ⦿
܅܅܅܅܅܅܅܅܅܅܅܅܅܅܅܅܅
┃01234567876543210┃
┗━━━━━━━━━━━━━━━━━┛

Horizontal Bar Chart w/ Border and Color Gradients

  my $tc = Term::Chart->new(
      {   orientation => 'horizontal',
          border      => 1,
          bar         => "\x{2015}",
          tip         => "\x{29BF}",
          color_range => [ 160 .. 165 ],
      }
  );

  for my $n (qw( 0 1 2 3 4 5 6 5 4 3 2 1 0 ))
  {
      my %datum = (
          value => $n,
          label => "label $n",
      );
      $tc->add_value( \%datum );
  }

  print "$tc\n";

HTML approximation

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃label 0:⦿                                                                                                                                               ┃
┃label 1:⦿                                                                                                                       ┃
┃label 2:⦿                                                                                               ┃
┃label 3:⦿                                                                       ┃
┃label 4:⦿                                               ┃
┃label 5:⦿                       ┃
┃label 6:⦿
┃label 5:⦿                       ┃
┃label 4:⦿                                               ┃
┃label 3:⦿                                                                       ┃
┃label 2:⦿                                                                                               ┃
┃label 1:⦿                                                                                                                       ┃
┃label 0:⦿                                                                                                                                               ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Vertical Bar Chart w/ ANSI Colors

  my $tc = Term::Chart->new(
      {   orientation => 'vertical',
          tip         => 'x',
          color_range => [ 160 .. 165 ],
      }
  );

  for my $n (qw( 0 1 2 3 4 5 6 5 4 3 2 1 0 ))
  {
      my %datum = (
          value => $n,
          label => $n,
      );
      $tc->add_value( \%datum );
  }

  print "$tc\n";

HTML approximation

            
            
            
            
            
            
     xx     
          
          
          
          
          
          
    xx    
        
        
        
        
        
        
   xx   
      
      
      
      
      
      
  xx  
    
    
    
    
    
    
 xx 
  
  
  
  
  
  
xx
܅܅܅܅܅܅܅܅܅܅܅܅܅
0123456543210

Horizontal w/ a Border

  my $tc = Term::Chart->new( { border => 2 } );

  for ( 0 .. 2 )
  {
      for my $n (qw( 0 1 2 3 2 1 0 ))
      {
          $tc->add_value( { value => $n**2 } );
      }
  }

  print "$tc\n";

HTML approximation


▅                                                                                                                                                       
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                                                                       
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                    
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                    
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                                                                       
▅                                                                                                                                                       
▅                                                                                                                                                       
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                                                                       
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                    
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                    
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                                                                       
▅                                                                                                                                                       
▅                                                                                                                                                       
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                                                                       
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                    
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                    
▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅                                                                                                                                       
▅                                                                                                                                                       

Horizontal w/ Color Gradient

  my $tc = Term::Chart->new(
      {   border      => 0,
          color_range => [ 172 .. 177 ],
      }
  );

  for ( 0 .. 2 )
  {
      for my $n (qw( 0 1 2 3 2 1 0 ))
      {
          $tc->add_value( { value => $n**2 } );
      }
  }

  print "$tc\n";

HTML approximation

                                                                                                                                                         
                                                                                                                                        
                                                                                     

                                                                                     
                                                                                                                                        
                                                                                                                                                         
                                                                                                                                                         
                                                                                                                                        
                                                                                     

                                                                                     
                                                                                                                                        
                                                                                                                                                         
                                                                                                                                                         
                                                                                                                                        
                                                                                     

                                                                                     
                                                                                                                                        
                                                                                                                                                         

DESCRIPTION

This module offers you a convenient way to create bar & dot charts and for your command line programs.

The charts can include ANSI color markup and Unicode drawing characters.

METHODS

new

The constructor accepts a hash ref with the following optional parameters.

bar

The character used for the bars. The default is U+2585 for horizontal, and U+2589 for vertical.

tip

The last character on each bar, at the tip. The default is U+2585 for horizontal, and U+2589 for vertical.

height

The row height. Default is determined at run time by the Term::ReadKey module.

width

The column width. Default is determined at run time by the Term::ReadKey module.

color_range

Pass an array ref with a range (0 to 255) of color values which will be used accross each bar. The default is no colors at all.

style

Either 'dot' or 'bar'. Default is 'bar'.

orientation

Either 'vertical' or 'horizontal'. Default is 'horizontal'.

border

0 is no border, 1 to 255 to indicate the color of the border. The default is 0.

add_value(<hash ref>)

Add a value to be graphed. Accepts a hash ref with:

label

The optional text label.

value

The numeric value being charted. (If a non-numeric value is supplied, then its string length will be used as the value.)

add_values(<list>)

Add a list of values (without labels) all at once.

print()

This will print the rendered chart to STDOUT.

AUTHOR

Dylan Doxey, <dylan@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2017 by Dylan Doxey

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.