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

NAME

Text::Chart - Generate text-based chart

VERSION

This document describes version 0.042 of Text::Chart (from Perl distribution Text-Chart), released on 2024-02-06.

SYNOPSIS

 use Text::Chart qw(gen_text_chart);

Bar chart:

 my $res = gen_text_chart(
     data => [1, 5, 3, 9, 2],
     type => 'bar',
 );

will produce this:

 *
 *****
 ***
 *********
 **

Adding data labels and showing data values:

 my $res = gen_text_chart(
     data => [["Andi",1], ["Budi",5], ["Cinta",3], ["Dewi",9], ["Edi",2]],
     type => 'bar',
     show_data_label => 1,
     show_data_value => 1,
 );

Result:

 Andi |*         (1)
 Budi |*****     (5)
 Cinta|***       (3)
 Dewi |********* (9)
 Edi  |**        (2)

Column chart:

 my $res = gen_text_chart(
     data => [["Andi",1], ["Budi",5], ["Cinta",3], ["Dewi",9], ["Edi",2]],
     type => 'column',
     show_data_label => 1,
 );

Result:

                     *
                     *
                     *
                     *
        *            *
        *            *
  *     *      *     *
  *     *      *     *     *
  *     *      *     *     *
 Andi  Budi  Cinta  Dewi  Edi

Sparkline chart:

Via tchart (from App::tchart) CLI:

 % tchart -d sales -t sparkline < celine-dion-album-sales.json
 ▂▂▅██▄▄▂▁▂▁

Horizontal sparkline chart:

 XXX

Plotting multiple data columns:

 XXX

DESCRIPTION

THIS IS AN EARLY RELEASE, MANY FEATURES ARE NOT YET IMPLEMENTED.

This module lets you generate text-based charts.

FUNCTIONS

gen_text_chart

Usage:

 gen_text_chart(%args) -> str

Generate text-based chart.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • chart_height => float

    (No description)

  • chart_width => float

    (No description)

  • data* => array[num]|array[array]|array[hash]

    (Table) data to chart.

    Either in the form of array of numbers, example:

     [1366,1248,319,252]

    or an array of arrays (there must be at least one number columns), example:

     [["China",1366],["India",1248],["United Status",319], ["Indonesia",252]]

    or an array of hashes (there must be at least one key which consistently contain numbers), example:

     [{country=>"China"        , population=>1366},
      {country=>"India"        , population=>1248},
      {country=>"United Status", population=> 319},
      {country=>"Indonesia"    , population=> 252}]

    All data needs to be in table form (where there are notions of rows and columns/fields). Array data is assumed to be a single-column table with the column named data. Array of arrays will have columns named column0, column1 and so on. Array of hashes will have columns named according to the hash keys.

  • data_column => str_or_aos1::arrayified

    Which column(s) contain data to plot.

    Multiple data columns are supported.

  • label_column => str_or_aos1::arrayified

    Which column(s) contain data labels.

    If not specified, the first non-numeric column will be selected.

    The number of label columns must match that of data columns.

  • show_data_label => bool

    (No description)

  • show_data_value => bool

    (No description)

  • spec => hash

    Table specification, according to TableDef.

  • type* => str

    Chart type.

Return value: (str)

FAQ

Why am I getting 'Wide character in print/say' warning?

You are probably printing Unicode characters to STDOUT without doing something like this beforehand:

 binmode(STDOUT, ":utf8");

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Text-Chart.

SOURCE

Source repository is at https://github.com/perlancar/perl-Text-Chart.

SEE ALSO

Text::Graph, a mature CPAN module for doing text-based graphs. Before writing Text::Chart I used this module for a while, but ran into the problem of weird generated graphs. In addition, I don't like the way Text::Graph draws things, e.g. a data value of 1 is drawn as zero-width bar, or the label separator : is always drawn. So I decided to write an alternative charting module instead. Compared to Text::Graph, here are the things I want to add or do differently as well: functional (non-OO) interface, colors, Unicode, resampling, more chart types like sparkline, animation and some interactivity (perhaps).

App::tchart, a CLI for Text::Chart.

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTOR

Steven Haryanto <stevenharyanto@gmail.com>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

 % prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2024, 2017, 2015, 2014 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Text-Chart

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.