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 "../../../load.koolreport.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;

// $data = array_slice($this->dataStore("salesQuarterCustomer")->data(), 0, 10);
// echo "<pre>" . var_export($data) . "</pre>";
// $data = array(
//     array('customerName' => 'Vitachrome Inc.', '{{all}}' => 0.7564904477705987, 'Q 1' => 0.05733632960467307, 'Q 2' => 0.23584863503242684, 'Q 3' => 0, 'Q 4' => 0.4633054831334988, 'H 1' => 0.2931849646370999, 'H 2' => 0.4633054831334988,),
//     array('customerName' => 'Baane Mini Imports', '{{all}}' => 1.0875534438899885, 'Q 1' => 0.5240192090676236, 'Q 2' => 0, 'Q 3' => 0, 'Q 4' => 0.5635342348223654, 'H 1' => 0.5240192090676236, 'H 2' => 0.5635342348223654,),
//     array('customerName' => 'Euro+ Shopping Channel', '{{all}}' => 8.563641486746963, 'Q 1' => 2.9767521029191313, 'Q 2' => 2.605268036854042, 'Q 3' => 0.6777266895670365, 'Q 4' => 2.303894657406763, 'H 1' => 5.5820201397731735, 'H 2' => 2.9816213469737995,),
//     array('customerName' => 'Danish Wholesale Imports', '{{all}}' => 1.3469632974165984, 'Q 1' => 0.5630476651963812, 'Q 2' => 0.5693276794297583, 'Q 3' => 0, 'Q 4' => 0.21458795279045872, 'H 1' => 1.1323753446261393, 'H 2' => 0.21458795279045872,),
//     array('customerName' => 'Rovelli Gifts', '{{all}}' => 1.330732866506353, 'Q 1' => 0.5441880052777882, 'Q 2' => 0, 'Q 3' => 0, 'Q 4' => 0.7865448612285648, 'H 1' => 0.5441880052777882, 'H 2' => 0.7865448612285648,),
// );
?>

<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.33
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 Get KoolReport Pro