PHP Classes

PHP Console Chart: Display bar charts in CLI console from datasets

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 70 All time: 10,172 This week: 106Up
Version License PHP version Categories
cli-graph-ml 1.0.3Custom (specified...5PHP 5, Graphics, Console
Description 

Author

This class can display bar charts in the CLI console from datasets.

It can take parameters, a set of values to display in a bar chart, and the bar columns' names.

The class can output the bar chart as characters in the command line interface console according to parameters that you can configure. Currently, you can configure:

- Graph length
- Bar color
- Title
- Using underlines to draw lines
- Bar width
- Titles in axis
- Padding spaces
- Values explanations

Innovation Award
PHP Programming Innovation award nominee
September 2021
Number 3
Many developers use console terminals to perform specific actions that are necessary to do their work.

Console terminals display information in the form of text. The fact limits the possibilities to display nice graphics.

This package overcomes this limitation by rendering bar charts in console terminals so that the charts are reasonably pleasant to see.

Manuel Lemos
Picture of Rafael Martin Soto
  Performance   Level  
Name: Rafael Martin Soto <contact>
Classes: 13 packages by
Country: Spain Spain
Age: 49
All time rank: 230058 in Spain Spain
Week rank: 81 Up2 in Spain Spain Up
Innovation award
Innovation award
Nominee: 7x

Winner: 4x

Example

<?php

/** example.php of cli-graph-ml.class.php
 *
 * Class for visualize data in bar graph & detect outliers *
 *
 * @author Rafael Martin Soto
 * @author {@link https://www.inatica.com/ Inatica}
 * @blog {@link https://rafamartin10.blogspot.com/ Blog Rafael Martin Soto}
 * @since September 2021
 * @version 1.0.0
 * @license GNU General Public License v3.0
 *
 * @param string $data
 * @param array $axis_x_values
 * @param array $axis_y_values
 * @param string $config
 *
 */

 
require_once( 'cli-graph-ml.class.php' );

 
/* You can define a custom $config
 $config = [
        'graph_length' => 10,
        'bar_color' => 'lightwhite',
        'title' => '',
        'draw_underlines' => true,
        'underlines_every' => 1,
        'bar_width' => 1,
        'show_y_axis_title' => true,
        'show_x_axis_title' => true,
        'x_axis_title' => 'AXIS X',
        'y_axis_title' => 'AXIS Y',
        'padding_left' => 1,
        'padding_right' => 1,
        'padding_top' => 1,
        'padding_bottom' => 1,
        'explain_values' => true,
        'explain_values_same_line' => false
    ]; // /$default_cfg
 */

 
$config = null;
 

$arr_val_example_1 = [ 1,2,5,6,7,9,12,15,18,19,38 ];
$axis_x_values = [ 'Jan', 'Jun', 'Dec' ];

$bar_graph = new cli_graph_ml( $arr_val_example_1, $axis_x_values, $config );
$bar_graph->set_title( 'Months in %' );

// Draw with defaults
echo 'Defaults Bar Graph'.PHP_EOL;
$bar_graph->draw();


// Draw with bar width 2
$bar_width = 2;
echo
'Bar Width '.$bar_width.PHP_EOL;
$bar_graph->set_bar_width( $bar_width );
$bar_graph->set_bar_color( 'blue' );
$bar_graph->set_explain_values_same_line( true );
$bar_graph->draw();


// Draw with bar width 4
$bar_width *= 2;
echo
'Bar Width '.$bar_width.PHP_EOL;
$bar_graph->set_bar_width( $bar_width );
$bar_graph->set_explain_values( false );
$bar_graph->set_bar_color( 'magenta' );
$bar_graph->set_underlines_every( 2 );
$bar_graph->draw();


// Draw with bar width 8
$bar_width *= 2;
echo
'Bar Width '.$bar_width.PHP_EOL;
$bar_graph->set_bar_width( $bar_width );
$bar_graph->set_explain_values( true );
$bar_graph->set_bar_color( 'yellow' );
$bar_graph->set_underlines_every( 3 );
$bar_graph->draw();

// Draw without underlines, Graph Lenght 20 & with bar width 16
$bar_width *= 2;
echo
'Remove underlines'.PHP_EOL;
$bar_graph->set_bar_width( $bar_width );
$bar_graph->set_draw_underlines( false );
$bar_graph->set_bar_color( 'green' );
$bar_graph->set_graph_length( 20 );
$bar_graph->draw();

unset(
$bar_graph );

// draw 3 graphs floating
$arr_val_example_2 = [ 7,7,6,3,5,8,0,10,8,9,3 ];
$arr_val_example_3 = [ 11,22,55,60,70,90,120,150,180,190,380 ];
$axis_x_values = [ 'Jan', 'Jun', 'Dec' ];

$bar_graph = [];

$bar_graph[] = new cli_graph_ml( $arr_val_example_1, $axis_x_values, $config );
$bar_graph[0]->set_title( 'Months 1 in %' );

$bar_graph[] = new cli_graph_ml( $arr_val_example_2, $axis_x_values, $config );
$bar_graph[1]->set_title( 'Months 2 in %' );

$bar_graph[] = new cli_graph_ml( $arr_val_example_3, $axis_x_values, $config );
$bar_graph[2]->set_title( 'Months 3 in %' );

// Prepare on each graph
foreach( $bar_graph as$graph){
   
$graph->prepare_array_output( );
}

// draw on each graph each line
// IMPORTANT: All graphs will need to have the same number of Lines
// We take a counter of lines of the first graph. We assume all have the same
$count_output_lines = $bar_graph[0]->count_output_lines();

for(
$i = 0; $i< $count_output_lines; $i++ ){
    foreach(
$bar_graph as $graph){
       
$graph->draw( $i, false, false); // Draw line $i, dont do line break and do not do prepare
   
}

    echo
PHP_EOL; // for get new line
}

unset(
$graph );
unset(
$i );
unset(
$arr_val_example );
unset(
$bar_graph );?>


Screenshots  
  • 3_chart_bars_side_by_side
  • custom_bar_chart
  Files folder image Files  
File Role Description
Accessible without login Image file bar-graph-cli-php.png Data Auxiliary data
Accessible without login Image file bar-multi-graph-cli-php.png Data Auxiliary data
Plain text file cli-graph-ml.class.php Class Class source
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file releases Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 77%
Total:70
This week:0
All time:10,172
This week:106Up