PHP code example of 8ctopus / stats-table

1. Go to this page and download the library: Download 8ctopus/stats-table library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

8ctopus / stats-table example snippets



$data = [
    [
        'name' => 'Pierre',
        'age' => 32,
        'weight' => 100,
        'height' => 1.87,
    ], [
        'name' => 'Jacques',
        'age' => 28,
        'weight' => 60,
        'height' => 1.67,
    ], [
        'name' => 'Jean',
        'age' => 32,
        'weight' => 80,
        'height' => 1.98,
    ], [
        'name' => 'Paul',
        'age' => 25,
        'weight' => 75,
        'height' => 1.82,
    ],
];

$headers = [
    'name' => 'Name',
    'age' => 'Age',
    'weight' => 'Weight',
    'height' => 'Height',
];

$formats = [
    'name' => Format::String,
    'age' => Format::Integer,
    'weight' => Format::Float,
    'height' => Format::Float,
];

$aggregations = [
    'name' => new CountAggregation('name', Format::Integer),
    'age' => new AverageAggregation('age', Format::Integer),
    'weight' => new AverageAggregation('weight', Format::Integer),
    'height' => new AverageAggregation('height', Format::Float),
];

$builder = new StatsTableBuilder($data, $headers, $formats, $aggregations);

// add body mass index row to table
$dynamicColumn = new CallbackColumnBuilder(function (array $row) : float {
    return $row['weight'] / ($row['height'] * $row['height']);
});

$table = $builder
    ->addDynamicColumn('BMI', $dynamicColumn, 'BMI', Format::Float, new AverageAggregation('BMI', Format::Float))
    ->build();

$table->sortByColumns([
    'age' => Direction::Ascending,
    'height' => Direction::Ascending,
]);

$dumper = new TextDumper();
echo $dumper->dump($table);

$data = [
    [
        'status' => 'active',
        'count' => 80,
    ], [
        'status' => 'cancelled',
        'count' => 20,
    ],
];

$headers = [];

$formats = [
    'status' => Format::String,
    'count' => Format::Integer,
];

$aggregations = [
    'count' => new SumAggregation('count', Format::Integer),
];

$builder = new StatsTableBuilder($data, $headers, $formats, $aggregations);

// get count column total
$total = $aggregations['count']->aggregate($builder);

// add percentage column
$dynamicColumn = new CallbackColumnBuilder(function (array $row) use ($total) : float {
    return $row['count'] / $total;
});

$table = $builder
    ->addDynamicColumn('percentage', $dynamicColumn, 'percentage', Format::Percent, new SumAggregation('percentage', Format::Percent))
    ->build();

echo (new TextDumper())
    ->dump($table);



use Oct8pus\StatsTable\Aggregation\SumAggregation;
use Oct8pus\StatsTable\Dumper\TextDumper;
use Oct8pus\StatsTable\DynamicColumn\CallbackColumnBuilder;
use Oct8pus\StatsTable\Format;
use Oct8pus\StatsTable\StatsTableBuilder;

$data = [
    [
        'date' => '2025-01',
        'currency' => 'USD',
        'amount' => 80,
    ], [
        'date' => '2025-01',
        'currency' => 'USD',
        'amount' => 40,
    ], [
        'date' => '2025-01',
        'currency' => 'EUR',
        'amount' => 80,
    ], [
        'date' => '2025-01',
        'currency' => 'EUR',
        'amount' => 40,
    ], [
        'date' => '2024-12',
        'currency' => 'USD',
        'amount' => 80,
    ], [
        'date' => '2024-12',
        'currency' => 'USD',
        'amount' => 20,
    ], [
        'date' => '2024-12',
        'currency' => 'EUR',
        'amount' => 20,
    ], [
        'date' => '2024-12',
        'currency' => 'EUR',
        'amount' => 40,
    ],
];

$headers = [];

$formats = [
    'date' => Format::String,
    'currency' => Format::String,
    'amount' => Format::Integer,
];

$aggregations = [];

$builder = new StatsTableBuilder($data, $headers, $formats, $aggregations);

// dynamic column with consolidated revenue in USD
$dynamicColumn = new CallbackColumnBuilder(function (array $row) : float {
    if ($row['currency'] === 'USD') {
        return $row['amount'];
    }

    $EURtoUSD = 1.0295998;
    return $row['amount'] * $EURtoUSD;
});

$builder->addDynamicColumn('consolidated', $dynamicColumn, 'consolidated', Format::Integer, new SumAggregation('consolidated', Format::Integer));

$dumper = new TextDumper();

$table = $builder
    ->groupBy(['date'], ['currency', 'amount'])
    ->build();

echo $dumper->dump($table);
txt
     date  consolidated
  2025-01           243
  2024-12           161
                    405