area > boundaries | Chart.js sample

The above example shows you how to create AreaChart using ChartJs 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.

This example shows how to build an area chart with different boundaries using the fill property.

The following values are supported for fill.

false
'origin'
'start'
'end'

If no fill property, fill will be true.

For example:

...
'columns' => array(
    'month',
    'Dataset' => array(
        "fill" => 'end'
        ...
    ),
)
...
<?php
if (session_status() !== PHP_SESSION_ACTIVE) session_start();
require_once "../../../load.koolreport.php";
require_once "MyReport.php";
$report = new MyReport;
$report->run();
?>
<?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
}
?>

<html>

<head>
    <title>
        area > boundaries | Chart.js sample
    </title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <style>
        #report_render {
            max-width: 800px;
            margin: auto;
        }

        .content {
            max-width: 800px;
            margin: auto;
            padding: 16px 32px;
        }

        .wrapper {
            min-height: 400px;
            padding: 16px 0;
            position: relative;
        }

        .wrapper.col-2 {
            display: inline-block;
            min-height: 256px;
            min-width: 392px;
        }

        .toolbar {
            display: flex;
        }

        .toolbar>* {
            margin: 0 8px 0 0;
        }

        canvas {
            min-height: 256px;
        }
    </style>
</head>

<body>

    <script>
        $(document).ready(function() {
            $("#smooth").click(function(e) {
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: "run.php",
                    data: {
                        command: "smooth"
                    },
                    success: function(response) {
                        $('#report_render').html(response);
                    }
                })
            })
            $("#randomize").click(function(e) {
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url: 'run.php',
                    data: {
                        command: "randomize"
                    },
                    success: function(response) {
                        $('#report_render').html(response);
                    }
                })
            })
        })
    </script>
    <div class="content">
        <div id="report_render"></div>
        <div class="toolbar">
            <button id="smooth" class="btn">Smooth</button>
            <button id="randomize" class="btn">Randomize</button>
        </div>
    </div>
</body>

</html>
<?php

class MyReport extends \koolreport\KoolReport
{

}
<div style="width: 800px;" id="report_render">
    <?php

    class SamplesUtils
    {
        private $seed;

        public function srand($seed)
        {
            $this->seed = $seed;
        }

        public function rand($min = 0, $max = 1)
        {
            $this->seed = ($this->seed * 9301 + 49297) % 233280;
            $random = $min + ($this->seed / 233280) * ($max - $min);
            return $random;
        }

        public function numbers($config = [])
        {
            $min = isset($config['min']) ? $config['min'] : 0;
            $max = isset($config['max']) ? $config['max'] : 1;
            $from = isset($config['from']) ? $config['from'] : [];
            $count = isset($config['count']) ? $config['count'] : 8;
            $decimals = isset($config['decimals']) ? $config['decimals'] : 8;
            $continuity = isset($config['continuity']) ? $config['continuity'] : 1;
            $dfactor = pow(10, $decimals);
            $data = [];

            for ($i = 0; $i < $count; $i++) {
                $value = (isset($from[$i]) ? $from[$i] : 0) + $this->rand($min, $max);
                if ($this->rand() <= $continuity) {
                    $data[] = round($value * $dfactor) / $dfactor;
                } else {
                    $data[] = null;
                }
            }

            return $data;
        }
    }

    $samples = new SamplesUtils();

    if (!isset($_POST['command'])) {
        $samples->srand(8);
    } else if (isset($_POST['command'])) {
        $samples->srand($_SESSION['seed']);
    }

    $inputs = [
        'min' => -100,
        'max' => 100,
        'count' => 8,
        'decimals' => 2,
        'continuity' => 1
    ];
    $randomNumbers = $samples->numbers($inputs);


    $fill = [false, 'origin', 'start', 'end',];

    $data = [
        ['month' => 'Feb', 'Dataset' => $randomNumbers[0]],
        ['month' => 'Jan', 'Dataset' => $randomNumbers[1]],
        ['month' => 'Mar', 'Dataset' => $randomNumbers[2]],
        ['month' => 'Apr', 'Dataset' => $randomNumbers[3]],
        ['month' => 'May', 'Dataset' => $randomNumbers[4]],
        ['month' => 'Jun', 'Dataset' => $randomNumbers[5]],
        ['month' => 'Jul', 'Dataset' => $randomNumbers[6]],
        ['month' => 'Aug', 'Dataset' => $randomNumbers[7]],
    ];

    if (!isset($_POST['command'])) {
        $seed = $samples->rand();
        $_SESSION['seed'] = $seed;
        $samples->srand($seed);
        $_SESSION['data'] = $data;
        $_SESSION['tension'] = 0.000001;
    }
    if (isset($_POST['command']) && $_POST['command'] === "smooth") {
        if ($_SESSION['tension'] === 0.000001) {
            $_SESSION['tension'] = 0.4;
        } else {
            $_SESSION['tension'] = 0.000001;
        }
    }
    if (isset($_POST['command']) && $_POST['command'] === 'randomize') {
        $seed = $samples->rand();
        $_SESSION['seed'] = $seed;
        $samples->srand($seed);
        $randomNumbers = $samples->numbers($inputs);
        foreach ($_SESSION['data'] as $key => $value) {
            $_SESSION['data'][$key]['Dataset'] = $randomNumbers[$key];
        }
    }

    for ($i = 0; $i < count($fill); $i++) {
    ?>
        <div class="wrapper col-2">
            <?php
            \koolreport\chartjs\AreaChart::create(
                array(
                    'dataSource' => $_SESSION['data'],
                    'columns' => array(
                        'month',
                        'Dataset' => array(
                            "fill" => $fill[$i],
                            "backgroundColor" => "rgba(255, 99, 132, 0.5)",
                            "borderColor" => 'rgb(255, 99, 132)'
                        ),
                    ),
                    "options" => array(
                        "maintainAspectRatio" => false,
                        "spanGaps" => false,
                        "elements" => array(
                            "line" => array(
                                "tension" => $_SESSION['tension']
                            )
                        ),
                        "pulgins" => array(
                            "filler" => array(
                                "propagate" => false
                            )
                        ),
                        "scales" => array(
                            "xAxes" => array(
                                array(
                                    "ticks" => array(
                                        "autoSkip" => false,
                                        "maxRotation" => 0
                                    )
                                )
                            )
                        ),
                        "title" => array(
                            "display" => true,
                            "text" => "fill: " . json_encode($fill[$i])
                        ),
                    ),
                )
            );
            ?>
        </div>
    <?php
    }
    ?>
</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