NAME

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

SYNOPSIS

use Term::Chart;

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

for my $number ( 1 .. 10 ) {

    $tc->add_value( { value => $number } );
}

$tc->print(), "\n";

HTML approximation of the output


═▸
═══▸
═════▸
════════▸
══════════▸
════════════▸
═══════════════▸
═════════════════▸
═══════════════════▸
══════════════════════▸

SOMETHING ELABORATE

use Term::Chart;

my $tc = Term::Chart->new(
    {   default_style => 11,
        color_range   => [ 160 .. 195 ],
    }
);

for ( 1 .. 10 ) {

    for my $n (qw( 1 2 3 4 5 6 5 4 3 2 )) {

        my %datum = (
            value => $n,
            label => ( 'label ' . $n x $n ),
        );
        $tc->add_value( \%datum );
    }
}

$tc->print(), "\n";

HTML approximation of the output

         label :
        label 1:
       label 22:
      label 333:
     label 4444:
    label 55555:
   label 666666:
  label 7777777:
 label 88888888:
label 999999999:

DESCRIPTION

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

The charts can include ANSI color markup and Unicode drawing characters. The characters used and the presence of ANSI color codes is configurable.

METHODS

new

The constructor accepts a hash ref with two optional parameters.

color_range

Use this to offer a range of color values which will be used accross each horiontal bar.

default_style

This controls which characters are used to create the bars in your bar chart. Accepts an integer which is among:

  • '0'

    ═▸
  • '1'

    ═▹
  • '2'

    ═►
  • '3'

    ═▻
  • '4'

    ◻▷
  • '5'

    ◻◻
  • '6'

    ◼▶
  • '7'

    ◼◗
  • '8'

    ◼◼
  • '9'

    ═◆
  • '10'

    ═◇
  • '11'

    ═●
  • '12'

    ═○

add_value

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

label

Optional label will be rendered to the left of each bar.

value

The numeric value being graphed.

color

Optional ANSI foreground color. This will override any colors given in the color_range constructor parameter.

bg_color

Optional ANSI background color.

char

The character to use for the bar. This will override the character related to the style indicated in the default_style constructor parameter.

last_char

The last (right most) character of each bar can be different from the rest of the bar. This will override the last character related to the style indicated in the default_style constructor parameter.

render

This constructs the bar chart as a blob of text and returns it.

print

This will print the rendered chart to STDOUT.

TODO

Consider a vertical bar chart, and perhaps a crude pie chart?

AUTHOR

Dylan Doxey, <dylan@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2012 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.