Line Chart Multiple Axes

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 a multi-axis chart by using yAxisID property and specifying the yAxisID for each column.

For example:

...
"columns"=>array(
    "month",
    "series1"=>array(
        ...
        "yAxisID" => "y-axis-1",
    ),
    "series2"=>array(
        ...
        "yAxisID" => "y-axis-2",
    ),
),
"options"=>array(
    "scales" => array(
        "yAxes" => array(
            array(
                ...
                "id" => "y-axis-1"
            ),
            array(
                ...
                "id" => "y-axis-2",
            )
        )
    )
)
...
<?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($_GET)) {
?>
    <div id='report_render'>
        <?php
        $report->render();
        ?>
    </div>
<?php
}
?>


<html>

<head>
    <title>
        Line Chart Multiple Axes
    </title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>

<body>
    <br>
    <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>
    <div>
        <div id='report_render'>
        </div>
    </div>
</body>

</html>
<?php
class MyReport extends \koolreport\KoolReport
{
    
}
<div id="report_render">
    <?php
    function randomScalingFactor()
    {
        return mt_rand(-100, 100);
    }

    $data = [
        ['month' => 'February',  'series1' => randomScalingFactor(), 'series2' => randomScalingFactor()],
        ['month' => 'January',   'series1' => randomScalingFactor(), 'series2' => randomScalingFactor()],
        ['month' => 'March',     'series1' => randomScalingFactor(), 'series2' => randomScalingFactor()],
        ['month' => 'April',     'series1' => randomScalingFactor(), 'series2' => randomScalingFactor()],
        ['month' => 'May',       'series1' => randomScalingFactor(), 'series2' => randomScalingFactor()],
        ['month' => 'June',      'series1' => randomScalingFactor(), 'series2' => randomScalingFactor()],
        ['month' => 'July',      'series1' => randomScalingFactor(), 'series2' => randomScalingFactor()],
    ];

    if (isset($_POST['command']) && $_POST['command'] === 'randomizeData') {
        foreach ($data as &$entry) {
            for ($i = 1; $i < count($data); $i++) {
                $entry['series' . $i] = randomScalingFactor();
            }
        }
    }

    \koolreport\chartjs\LineChart::create(array(
        'dataSource' => $data,
        'columns' => array(
            "month",
            "series1" => array(
                "label" => "Series 1",
                "yAxisID" => "y-axis-1",
                "backgroundColor" => 'rgb(255, 99, 132)',
                "borderColor" => 'rgb(255, 99, 132)',
            ),
            "series2" => array(
                "label" => "Series 2",
                "yAxisID" => "y-axis-2",
                "backgroundColor" => 'rgb(54, 162, 235)',
                "borderColor" => 'rgb(54, 162, 235)',
            )
        ),
        "options" => array(
            "responsive" => true,
            "hoverMode" => "index",
            "stacked" => false,
            "title" => array(
                "display" => true,
                "text" => 'Chart.js Line Chart - Multi Axis'
            ),
            "scales" => array(
                "yAxes" => array(
                    array(
                        "type" => "linear",
                        "display" => true,
                        "position" => "left",
                        "id" => "y-axis-1"
                    ),
                    array(
                        "type" => "linear",
                        "display" => true,
                        "position" => "right",
                        "id" => "y-axis-2",
                        "gridLines" => array(
                            "drawOnChartArea" => false,
                        )
                    )
                )
            )
        )
    ));
    ?>
</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