Skip to content

Commit

Permalink
Add dynamic column to README example
Browse files Browse the repository at this point in the history
  • Loading branch information
8ctopus committed Dec 20, 2024
1 parent b438096 commit 2c777be
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ $statsTableBuilder->addIndexesAsColumn('date', 'Date');
$statsTable = $statsTableBuilder->build();
```

#### Advanced example with aggregation and multiple column sorting
#### Advanced example with aggregation, dynamic column multiple column sorting

```php
use IgraalOSL\StatsTable\Aggregation\AverageAggregation;
use IgraalOSL\StatsTable\Aggregation\SumAggregation;
use IgraalOSL\StatsTable\Dumper\HTML\HTMLDumper;
use IgraalOSL\StatsTable\Dumper\TXT\TXTDumper;
use IgraalOSL\StatsTable\Dumper\Format;
use IgraalOSL\StatsTable\DynamicColumn\CallbackColumnBuilder;
use IgraalOSL\StatsTable\StatsTableBuilder;

require_once __DIR__ . '/vendor/autoload.php';
Expand All @@ -93,31 +94,37 @@ $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::FLOAT2,
'height' => Format::FLOAT2,
];

Expand All @@ -131,8 +138,15 @@ $aggregationsFormats = [
'height' => Format::FLOAT2,
];

$table = (new StatsTableBuilder($data, $headers, $formats, $aggregations))
->build();
$builder = new StatsTableBuilder($data, $headers, $formats, $aggregations);

$dynamicColumn = new CallbackColumnBuilder(function($row) : float {
return $row['weight'] / ($row['height'] * $row['height']);
});

$builder->addDynamicColumn('BMI', $dynamicColumn, 'BMI', Format::FLOAT2);

$table = $builder->build();

$table->sortMultipleColumn([
'age' => true,
Expand Down

0 comments on commit 2c777be

Please sign in to comment.