Sparklines in DataTables

Displaying sparklines with DataTables' data

Customer SparkBar SparkLine SparkPie SparkBox SparkBullet SparkTristate
Vitachrome Inc.
Baane Mini Imports
Euro+ Shopping Channel
Danish Wholesale Imports
Rovelli Gifts

Sometimes you want to display sparklines instead of number in DataTables.

<?php
require_once "MyReport.php";

$report = new MyReport;
$report->run()->render();
<?php
//Step 1: Load KoolReport
require_once "../../../../koolreport/autoload.php";
use \koolreport\cube\processes\Cube;
use \koolreport\processes\Map;
use \koolreport\processes\Limit;
use \koolreport\core\Utility as Util;

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
    function settings()
    {
        return array(
            "dataSources" => array(
                "dollarsales"=>array(
                    'filePath' => '../../../databases/customer_product_dollarsales2.csv',
                    'fieldSeparator' => ';',
                    'class' => "\koolreport\datasources\CSVDataSource"      
                ), 
            )
        );
    }
    protected function setup()
    {
        $node = $this->src('dollarsales')

        ->pipe(new Map(array(
            '{value}' => function($row, $metaData) {
                $row['orderQuarter'] = 'Q ' . $row['orderQuarter'];
                return array($row);
            },
            '{meta}' => function($metaData) {
                $metaData['columns']['dollar_sales'] = array(
                    'type' => 'number',
                    "prefix" => "$",
                );
                $metaData['columns']['orderQuarter'] = array(
                    'type' => 'string',
                );
                return $metaData;
            },
        )))

        ->pipe(new Cube(array(
            "rows" => "customerName",
            "column" => "orderQuarter",
            "sum" => "dollar_sales",
            "sum percent" => "dollar_sales",
        )))

        ->pipe(new Map(array(
            '{value}' => function($row, $meta) {
                $row['H 1'] = Util::get($row, 'Q 1', 0) + Util::get($row, 'Q 2', 0);
                $row['H 2'] = Util::get($row, 'Q 3', 0) + Util::get($row, 'Q 4', 0);
                return $row;
            },
            '{meta}' => function($meta) {
                $cMetas = & $meta['columns'];
                $cMetas['Q 1']['type'] = 'number';
                $cMetas['Q 1']['decimals'] = 2;
                return $meta;
            }
        )))
        
        ->pipe(new Limit(array(
            5, 0
        )))
        ->pipe($this->dataStore('salesQuarterCustomer'));
    }
}
<?php
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\datagrid\DataTables;
    use \koolreport\sparklines;
?>

<div class="report-content">
    <div class="text-center">
        <h1>Sparklines in DataTables</h1>
        <p class="lead">
            Displaying sparklines with DataTables' data
        </p>
    </div>
    <?php
    $ds = $this->dataStore('salesQuarterCustomer');
    DataTables::create(array(
        'name' => 'salesQuarterCustomer',
        "dataSource" => $ds,
        "options" => array(
            "searching" => true,
            "paging" => true,
            "colReorder" => true,
            // "ordering" => false,
            "order" => [],
        ),
        "columns"=>array(
            "customerName" => array(
                "label" => "Customer",
            ),
            'SparkBar' => [
                'formatValue' => function($val, $row) {
                    $data = [$row['Q 1'], $row['Q 2'], 
                        $row['Q 3'], $row['Q 4']];
                    return sparklines\Bar::create(array(
                        "data" => $data,
                    ));
                }
            ],
            'SparkLine' => [
                'formatValue' => function($val, $row) {
                    $data = [$row['Q 1'], $row['Q 2'], 
                        $row['Q 3'], $row['Q 4']];
                    return sparklines\Line::create(array(
                        "data" => $data,
                    ));
                }
            ],
            'SparkPie' => [
                'formatValue' => function($val, $row) {
                    $data = [$row['Q 1'], $row['Q 2'], 
                        $row['Q 3'], $row['Q 4']];
                    return sparklines\Pie::create(array(
                        "data" => $data,
                    ));
                }
            ],
            'SparkBox' => [
                'formatValue' => function($val, $row) {
                    $data = [$row['Q 1'], $row['Q 2'], 
                        $row['Q 3'], $row['Q 4']];
                    return sparklines\Box::create(array(
                        "data" => $data,
                    ));
                }
            ],
            'SparkBullet' => [
                'formatValue' => function($val, $row) {
                    $data = [$row['Q 1'], $row['Q 2'], 
                        $row['Q 3'], $row['Q 4']];
                    return sparklines\Bullet::create(array(
                        "data" => $data,
                    ));
                }
            ],
            'SparkTristate' => [
                'formatValue' => function($val, $row) {
                    $data = [$row['Q 1'], $row['Q 2'], 
                        $row['Q 3'], $row['Q 4']];
                    return sparklines\Tristate::create(array(
                        "data" => $data,
                    ));
                }
            ],
            
        ),
        "showFooter" => true,
        // "paging" => array(
        //   "pageSize" => 2
        // )
    ));
    ?>
</div>
customerNameproductNameproductLineorderDateorderDayorderMonthorderYearorderQuarterdollar_sales
Vitachrome Inc. 1937 Lincoln Berline Vintage Cars 2003-01-10 00:00:00 10 1 2003 1 3726.45
Vitachrome Inc. 1936 Mercedes-Benz 500K Special Roadster Vintage Cars 2003-01-10 00:00:00 10 1 2003 1 1768.3300000000002
Baane Mini Imports 1952 Alpine Renault 1300 Classic Cars 2003-01-29 00:00:00 29 1 2003 1 5571.8
Baane Mini Imports 1962 LanciaA Delta 16V Classic Cars 2003-01-29 00:00:00 29 1 2003 1 5026.14
Baane Mini Imports 1958 Setra Bus Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 3284.28
Baane Mini Imports 1940 Ford Pickup Truck Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 3307.5
Baane Mini Imports 1926 Ford Fire Engine Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 1283.48
Baane Mini Imports 1913 Ford Model T Speedster Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2489.13
Baane Mini Imports 1934 Ford V8 Coupe Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2164.4
Baane Mini Imports 18th Century Vintage Horse Carriage Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2173

What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider

Download KoolReport