Giter Site home page Giter Site logo

terminal-graphing-barchart's Introduction

Terminal::Graphing::BarChart

Terminal::Graphing::BarChart is a simple library to let you produce bar graphs on the command line. It takes special care to give you good looking output.

example graph

Currently limited to vertical bar charts. See the end of this document for future plans & contribution guidelines.

SYNOPSIS

Common Usage Example

# VERTICAL BAR CHART
use Terminal::Graphing::BarChart::Vertical;

my $x_and_y_axis_graph = Terminal::Graphing::BarChart::Vertical.new(
    data => [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
    bar_length => 10,
    x_axis_labels => <a b c d e f g h i j k>,
    y_axis_labels => <0 1 2 3 4 5 6 7 8 9>
);

# to get the string version of the graph
$x_and_y_axis_graph.generate();

# to print the graph to Standard Out
$x_and_y_axis_graph.print();

Note that for a vertical graph the 0 x 0 point of this graph is the bottom left corner. Data and labels start from there and move outwards. BUT for a horizontal graph the 0 x 0 point is the top left corner.

my $horizontal_graph = Terminal::Graphing::BarChart::Horizontal.new(
    data => [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
    bar_length => 20,
    x_axis_labels => <a b c d e f g h i j>,
    y_axis_labels => <alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo>
);
# to get the string version of the graph
$horizontal_graph.generate();

# to print the graph to Standard Out
$horizontal_graph.print();

There are 3 notable restrictions in the horizontal graph

  1. x axis labels can't be longer than 1 character
  2. bar_length must be evenly divisible by the number of x axis labels.
  3. the x axis labels will be spread evenly across bar_length characters of width.

Folks using Arabic & other Right-to-Left are encouraged to make a PR to support reversed graphs.

Required Attributes

When creating a graph there are two required keys. bar_length and data.

bar_length is how tall or wide you wish the core graph's bars to be (in characters). This does not include the additional lines for labels or the lines separating them from the graph.

data is an array of numbers. Each is expected to be a percentage from 0 to 100. 100 create a vertical bar bar_length lines tall. 0 will create an empty bar.

Note, there will be rounding issues if your bar_length is anything other than an even multiple of 100. This is ok.

Just to set expectations, if for example, you specify a bar_length of 10 that means there are only 10 vertical/horizontal elements to each bar. If one of your data points is 7 you'll end up with an empty bar because that's less than the number needed to activate the 1st element (10).

Optional Attributes

x_axis_labels This is an array of labels for the x axis. In the vertical bar chart it must be equal in length to the number of data points. If you want some of your bars to be unlabeled then specify a space for that "label". In the horizontal bar chart it must be a number that's bar_length can be evenly divided by. In the horizontal bar chart they must also be no longer than 1 character.

For the horizontal graph I recommend just making it the same number of elements as the bar_length. That way you can precisely specify where each label appears.

y_axis_labels This is an array of labels for the y axis. In the vertical bar graph it must be equal in length to the bar_length (one label per row). Again, if you want some of the points to be unlabeled, you should use a space character. In the horizontal graph it can be less.

Note that the 0 x 0 point of the vertical graph is the bottom left. So its list of y_axis_labels will go from bottom up. This corresponds to how the data and x_axis_labels go from left to right. In the horizontal bar graph it's top left so data moves from that column out.

In the Vertical bar graph you can specify space_between_columns This is a Bool which defaults to True. If you set it to False the system will not introduce a space between each column. This works fine, and may be a good choice if you have a large number of data points, but for short graphs it's almost always worse looking.

Advice

From a purely visual perspective it is not recommended that you use full words for your x_axis_labels. In order to not introduce a false sense of time compression or similar meaning, every bar gets spread out by the length of the longest label.

y_axis_labels should be fine regardless of length. They are right-aligned, and just shove the graph farther to the right.

I would not recommend generating a vertical graph that's more than 10 characters high. I would recommend making a horizontal graph that's >=20 characters wide. Otherwise they look too squished.

See vertical-graph-tester.raku and horizontal-graph-tester.raku for examples.

Legend

This library does not support generating a legend for your graph. My advice is to use single letter x_axis_labels and then use Prettier::Table to generate a legend that explains your x axis.

Future + Contributing

Contributing

Please do. All I ask is that you include unit tests that cover whatever changes or additions you make, and that you're fine with your contributions being distributed under the AGPL.

What kind of contributions? New Features, refactored code, more tests, etc.

For best results, please ping me on mastodon (see below) to make sure I see your PR right away.

AUTHOR

web: masukomi
mastodon: @[email protected]

COPYRIGHT AND LICENSE

Copyright 2023 Kay Rhodes (a.k.a. masukomi)

This library is free software; you can redistribute it and/or modify it under the AGPL 3.0 or later. See LICENSE.md for details.

terminal-graphing-barchart's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

melezhik

terminal-graphing-barchart's Issues

Code snippet from the doc fails

Hi! I try to run a code snippet extracted from the module doc and it fails:

19:19:45 :: # VERTICAL BAR CHART
19:19:45 :: use Terminal::Graphing::BarChart::Vertical;
19:19:45 :: 
19:19:45 :: my $x_and_y_axis_graph = Terminal::Graphing::BarChart::Vertical.new(
19:19:45 ::     data => [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
19:19:45 ::     bar_length => 10,
19:19:45 ::     x_axis_labels => <a b c d e f g h i j k>,
19:19:45 ::     y_axis_labels => <0 1 2 3 4 5 6 7 8 9>
19:19:45 :: );
19:19:45 :: 
19:19:45 :: # to get the string version of the graph
19:19:45 :: $x_and_y_axis_graph.generate();
19:19:45 :: 
19:19:45 :: # to print the graph to Standard Out
19:19:45 :: $x_and_y_axis_graph.print();
19:19:46 :: Using rakudo from /var/.sparrowdo/env/main/.sparrowdo/rakudo-moar-2022.12-01-linux-x86_64-gcc:
19:19:46 :: Welcome to Rakudo™ v2022.12.
19:19:46 :: Implementing the Raku® Programming Language v6.d.
19:19:46 :: Built on MoarVM version 2022.12.
19:19:46 :: run raku -I lib /home/worker/.sparrow6/tmp/3122/code.raku ...
19:19:46 :: warn: good_doc failed
[task stderr]
19:19:46 :: ===SORRY!=== Error while compiling /home/worker/.sparrow6/tmp/3122/code.raku
19:19:46 :: ===SORRY!=== Error while compiling /var/.sparrowdo/env/main/.sparrowdo/source/lib/Terminal/Graphing/BarChart/Vertical.rakumod (Terminal::Graphing::BarChart::Vertical)
19:19:46 :: ===SORRY!=== Error while compiling /var/.sparrowdo/env/main/.sparrowdo/source/lib/Terminal/Graphing/BarChart/Core.rakumod (Terminal::Graphing::BarChart::Core)
19:19:46 :: Could not find Listicles in:
19:19:46 ::     /var/.sparrowdo/env/main/.sparrowdo/source/lib
19:19:46 ::     /home/worker/.raku
19:19:46 ::     /var/.sparrowdo/env/main/.sparrowdo/rakudo-moar-2022.12-01-linux-x86_64-gcc/share/perl6/site
19:19:46 ::     /var/.sparrowdo/env/main/.sparrowdo/rakudo-moar-2022.12-01-linux-x86_64-gcc/share/perl6/vendor
19:19:47 ::     /var/.sparrowdo/env/main/.sparrowdo/rakudo-moar-2022.12-01-linux-x86_64-gcc/share/perl6/core
19:19:47 ::     CompUnit::Repository::AbsolutePath<68945232>
19:19:47 ::     CompUnit::Repository::NQP<72095928>
19:19:47 ::     CompUnit::Repository::Perl5<72095968>
19:19:47 :: at /var/.sparrowdo/env/main/.sparrowdo/source/lib/Terminal/Graphing/BarChart/Core.rakumod (Terminal::Graphing::BarChart::Core):2

The full report is available here - https://ci.sparrowhub.io/report/2887

HTH

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.