Line Chart - Cubic interpolation mode

The above example shows you how to create LineChart 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 line charts with different geometric line styles using the cubicInterpolationMode and lineTension properties.

The following interpolation modes are supported.

'default'
'monotone'

The 'default' algorithm uses a custom weighted cubic interpolation, which produces pleasant curves for all types of datasets.

The 'monotone' algorithm is more suited to y = f(x) datasets: it preserves monotonicity (or piecewise monotonicity) of the dataset being interpolated, and ensures local extremums (if any) stay at input data points.

If left untouched (undefined), the global options.elements.line.cubicInterpolationMode property is used.

The 'lineTension' property specifies the tension of the bezier curve of the line. Set to 0 to draw a straight line. This option is ignored if using monotone cubic interpolation.

For example:

...
"columns" => array(
    'labels',
    'Cubic interpolation (monotone)' => array(
        ...
        "cubicInterpolationMode" => 'monotone'
    ),
    'Cubic interpolation (default)' => array(
        ...
    ),
    'Linear interpolation' => array(
        ...
        "lineTension" => 0
    )
),
<?php
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>
        Line Chart - Cubic interpolation mode
    </title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>

<body>
    <div>
        <div id='report_render'>
        </div>
    </div>
    <button id="randomizeData" class="btn">Randomize Data</button>

    <script>
        $(document).ready(function() {
            $('#randomizeData').click(function(e) {
                e.preventDefault();
                $.ajax({
                    type: "POST",
                    url:"run.php",
                    data: {
                        command: 'randomizeData',
                    },
                    success: function(response) {
                        $('#report_render').html(response);
                    }
                })
            })
        })
    </script>
</body>

</html>
<?php
class MyReport extends \koolreport\KoolReport
{
    use \koolreport\clients\jQuery;
}
<div>
    <?php
    function randomScalingFactor()
    {
        return mt_rand(0, 100);
    }

    $dataPoints = [0, 20, 20, 60, 60, 120, 'NAN', 180, 120, 125, 105, 110, 170];

    if (isset($_POST['command']) && $_POST['command'] === 'randomizeData') {
        for ($i = 0; $i < count($dataPoints); $i++) {
            $dataPoints[$i] = mt_rand() < 0.05 ? NAN : randomScalingFactor();;
        }
    }

    $data = [
        ['labels' => '0',  'Cubic interpolation (monotone)' => $dataPoints[0],  'Cubic interpolation (default)' => $dataPoints[0],  'Linear interpolation' => $dataPoints[0]],
        ['labels' => '1',  'Cubic interpolation (monotone)' => $dataPoints[1],  'Cubic interpolation (default)' => $dataPoints[1],  'Linear interpolation' => $dataPoints[1]],
        ['labels' => '2',  'Cubic interpolation (monotone)' => $dataPoints[2],  'Cubic interpolation (default)' => $dataPoints[2],  'Linear interpolation' => $dataPoints[2]],
        ['labels' => '3',  'Cubic interpolation (monotone)' => $dataPoints[3],  'Cubic interpolation (default)' => $dataPoints[3],  'Linear interpolation' => $dataPoints[3]],
        ['labels' => '4',  'Cubic interpolation (monotone)' => $dataPoints[4],  'Cubic interpolation (default)' => $dataPoints[4],  'Linear interpolation' => $dataPoints[4]],
        ['labels' => '5',  'Cubic interpolation (monotone)' => $dataPoints[5],  'Cubic interpolation (default)' => $dataPoints[5],  'Linear interpolation' => $dataPoints[5]],
        ['labels' => '6',  'Cubic interpolation (monotone)' => $dataPoints[6],  'Cubic interpolation (default)' => $dataPoints[6],  'Linear interpolation' => $dataPoints[6]],
        ['labels' => '7',  'Cubic interpolation (monotone)' => $dataPoints[7],  'Cubic interpolation (default)' => $dataPoints[7],  'Linear interpolation' => $dataPoints[7]],
        ['labels' => '8',  'Cubic interpolation (monotone)' => $dataPoints[8],  'Cubic interpolation (default)' => $dataPoints[8],  'Linear interpolation' => $dataPoints[8]],
        ['labels' => '9',  'Cubic interpolation (monotone)' => $dataPoints[9],  'Cubic interpolation (default)' => $dataPoints[9],  'Linear interpolation' => $dataPoints[9]],
        ['labels' => '10', 'Cubic interpolation (monotone)' => $dataPoints[10], 'Cubic interpolation (default)' => $dataPoints[10], 'Linear interpolation' => $dataPoints[10]],
        ['labels' => '11', 'Cubic interpolation (monotone)' => $dataPoints[11], 'Cubic interpolation (default)' => $dataPoints[11], 'Linear interpolation' => $dataPoints[11]],
        ['labels' => '12', 'Cubic interpolation (monotone)' => $dataPoints[12], 'Cubic interpolation (default)' => $dataPoints[12], 'Linear interpolation' => $dataPoints[12]],

    ];

    \koolreport\chartjs\LineChart::create(
        array(
            'dataSource' => $data,
            'columns' => array(
                'labels',
                'Cubic interpolation (monotone)' => array(
                    "borderColor" => 'rgb(255, 99, 132)',
                    "backgroundColor" => 'rgba(0, 0, 0, 0)',
                    "fill" => false,
                    "cubicInterpolationMode" => 'monotone'
                ),
                'Cubic interpolation (default)' => array(
                    "borderColor" => 'rgb(54, 162, 235)',
                    "backgroundColor" => 'rgba(0, 0, 0, 0)',
                    "fill" => false
                ),
                'Linear interpolation' => array(
                    "borderColor" => 'rgb(75, 192, 192)',
                    "backgroundColor" => 'rgba(0, 0, 0, 0)',
                    "fill" => false,
                    "lineTension" => 0
                )
            ),
            "options" => array(
                "responsive" => true,
                "title" => array(
                    "display" => true,
                    "text" => 'Chart.js Line Chart - Cubic interpolation mode'
                ),
                "tooltips" => array(
                    "mode" => 'index'
                ),
                "scales" => array(
                    "xAxes" => array(
                        array(
                            "display" => true,
                            "scaleLabel" => array(
                                "display" => true
                            )
                        )
                    ),
                    "yAxes" => array(
                        array(
                            "display" => true,
                            "scaleLabel" => array(
                                "display" => true,
                                "labelString" => 'Value'
                            ),
                            "ticks" => array(
                                "suggestedMin" => -10,
                                "suggestedMax" => 200,
                            )
                        )
                    )
                )
            )
        )
    );
    ?>
</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