Data Order

The above example shows you how to create ColumnChart using D3 package. In this example, for purpose of chart demonstration only, we do use mock-up data from array. As you can see, the KoolReport's widget in general support dataSource could be DataStore, Process, DataSource or even simple array.

The example show how to create column chart with custom data order.

Define the order of the data.

This option changes the order of stacking the data and pieces of pie/donut. If null specified, it will be the order the data loaded. If function specified, it will be used to sort the data and it will receive the data as argument.

Available Values:

desc

asc

function (data1, data2) { ... }

null

For example:

...
"options" => array(
        "data" => array(
           ...
            "order" => 'desc'
        ),
        ...
    )
<?php
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
require_once "../../../../load.koolreport.php";
require_once "MyReport.php";
$report = new MyReport;
$report->run();
// $report->render();
?>
<?php
if (isset($_POST['command'])) {
?>
    <div id="report_render">
        <?php
        $report->render();
        ?>
    </div>
<?php
    exit;
}
?>
<?php
if (!isset($_POST['command'])) {
?>
    <div id="report_render">
        <?php
        $report->render();
        ?>
    </div>
<?php
}
?>

<script>
    setTimeout(function() {
        $.ajax({
            type: "POST",
            url: 'run.php',
            data: {
                command: "second"
            },
            success: function(response) {
                $('#report_render').html(response);
            },
        });
    }, 1000);

    setTimeout(function() {
        $.ajax({
            type: "POST",
            url: 'run.php',
            data: {
                command: "third"
            },
            success: function(response) {
                $('#report_render').html(response);
            },
        });
    }, 2000);

    setTimeout(function() {
        $.ajax({
            type: "POST",
            url: 'run.php',
            data: {
                command: "final"
            },
            success: function(response) {
                $('#report_render').html(response);
            },
        });
    }, 3000);
</script>
<?php

class MyReport extends \koolreport\KoolReport
{

}
<div id="report_render">
    <h1 class='title'>Data Order</h1>
    <?php
    $data = [
        ['data1' => 130, 'data2' => -130, 'data3' => -130],
        ['data1' => 200, 'data2' =>   10, 'data3' =>  -50],
        ['data1' => 320, 'data2' =>  130, 'data3' =>  -10],
        ['data1' => 400, 'data2' =>  200, 'data3' => -200],
        ['data1' => 530, 'data2' =>  150, 'data3' => -250],
        ['data1' => 750, 'data2' =>  250, 'data3' => -150]
    ];

    $series = array();
    if (!isset($_POST['command'])) {
        $_SESSION['data'] = $data;
        $_SESSION['groups'] = array(
            'data1',
            'data2',
            'data3'
        );
        $series = array(
            'data1',
            'data2',
            'data3'
        );
    }

    if (isset($_POST['command']) && $_POST['command'] === 'second') {
        $data4 = [1200, 1300, 1450, 1600, 1520, 1820];
        for ($i = 0; $i < count($_SESSION['data']); $i++) {
            $_SESSION['data'][$i]['data4'] = $data4[$i];
        }
        $series = array(
            'data1',
            'data2',
            'data3',
            'data4'
        );
    }

    if (isset($_POST['command']) && $_POST['command'] === 'third') {
        $data5 = [200, 300, 450, 600, 520, 820];
        for ($i = 0; $i < count($_SESSION['data']); $i++) {
            $_SESSION['data'][$i]['data5'] = $data5[$i];
        }
        $series = array(
            'data1',
            'data2',
            'data3',
            'data4',
            'data5'
        );
    }

    if (isset($_POST['command']) && $_POST['command'] === 'final') {
        $_SESSION['groups'] = array(
            'data1',
            'data2',
            'data3',
            'data4',
            'data5'
        );
        $series = array(
            'data1',
            'data2',
            'data3',
            'data4',
            'data5'
        );
    }


    \koolreport\d3\ColumnChart::create(array(
        "dataSource" => $_SESSION['data'],
        "columns" => $series,
        "options" => array(
            "data" => array(
                "groups" => array(
                    $_SESSION['groups']
                ),
                "order" => 'desc'
            ),
            "grid" => array(
                "y" => array(
                    "lines" => array(
                        array(
                            "value" => 0
                        )
                    )
                )
            )
        )
    ));
    ?>
</div>

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