Bar Chart Multi Axis

The above example shows you how to create BarChart 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();
// $report->render();
?>
<?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>
        Bar Chart Multi Axis
    </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' => 'January',   'Dataset 1' => randomScalingFactor(), 'Dataset 2' => randomScalingFactor()],
        ['month' => 'February',  'Dataset 1' => randomScalingFactor(), 'Dataset 2' => randomScalingFactor()],
        ['month' => 'March',     'Dataset 1' => randomScalingFactor(), 'Dataset 2' => randomScalingFactor()],
        ['month' => 'April',     'Dataset 1' => randomScalingFactor(), 'Dataset 2' => randomScalingFactor()],
        ['month' => 'May',       'Dataset 1' => randomScalingFactor(), 'Dataset 2' => randomScalingFactor()],
        ['month' => 'June',      'Dataset 1' => randomScalingFactor(), 'Dataset 2' => randomScalingFactor()],
        ['month' => 'July',      'Dataset 1' => randomScalingFactor(), 'Dataset 2' => randomScalingFactor()],
    ];

    \koolreport\chartjs\ColumnChart::create(array(
        'dataSource' => $data,
        'columns' => array(
            "month",
            "Dataset 1" => array(
                "backgroundColor" => array(
                    'rgb(255, 99, 132)',
                    'rgb(54, 162, 235)',
                    'rgb(255, 205, 86)',
                    'rgb(75, 192, 192)',
                    'rgb(153, 102, 255)',
                    'rgb(255, 159, 64)',
                    'rgb(255, 99, 132)',
                ),
                "borderColor" => array(
                    'rgb(255, 99, 132)',
                    'rgb(54, 162, 235)',
                    'rgb(255, 205, 86)',
                    'rgb(75, 192, 192)',
                    'rgb(153, 102, 255)',
                    'rgb(255, 159, 64)',
                    'rgb(255, 99, 132)',
                ),
                "yAxisID" => "y-axis-1"
            ),
            "Dataset 2" => array(
                "backgroundColor" => 'rgb(201, 203, 207)',
                "borderColor" => 'rgb(201, 203, 207)',
                "yAxisID" => "y-axis-2"
            )
        ),
        "options" => array(
            "responsive" => true,
            "title" => array(
                "display" => true,
                "text" => 'Chart.js Bar Chart - Multi Axis'
            ),
            "tooltips" => array(
                "mode" => 'index',
                "intersect" => true
            ),
            "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