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 line chart with the y-axis type
can be changed to linear or logarithm.
<?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>
Toggle Scale Type
</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
</head>
<body>
<div>
<button id="toggleScale" class="btn" style="margin: 0px 0px 10px 10px;"> Toggle Scale Type</button>
</div>
<script>
$(document).ready(function() {
$('#toggleScale').click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: 'run.php',
data: {
command: "toggleScale"
},
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 ceil(mt_rand() / mt_getrandmax() * 10.0) * pow(10, ceil(mt_rand() / mt_getrandmax() * 5));
}
$data = [
['month' => 'January', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
['month' => 'February', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
['month' => 'March', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
['month' => 'April', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
['month' => 'May', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
['month' => 'June', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
['month' => 'July', 'My First dataset' => randomScalingFactor(), 'My Second dataset' => randomScalingFactor()],
];
if (!isset($_POST['command'])) {
$_SESSION['data'] = $data;
$_SESSION['type'] = 'linear';
}
if (isset($_POST['command'])) {
$_SESSION['type'] = $_SESSION['type'] === 'linear' ? 'logarithmic' : 'linear';
}
\koolreport\chartjs\LineChart::create(array(
'dataSource' => $_SESSION['data'],
'columns' => array(
"month",
"My First dataset" => array(
"fill" => false,
"backgroundColor" => 'rgb(255, 99, 132)',
"borderColor" => 'rgb(255, 99, 132)'
),
"My Second dataset" => array(
"fill" => false,
"backgroundColor" => 'rgb(54, 162, 235)',
"borderColor" => 'rgb(54, 162, 235)'
)
),
"options" => array(
"responsive" => true,
"title" => array(
"display" => true,
"text" => 'Chart.js Line Chart - ' . $_SESSION['type']
),
"scales" => array(
"xAxes" => array(
array(
"display" => true
)
),
"yAxes" => array(
array(
'display' => true,
"type" => $_SESSION['type']
)
)
)
)
));
?>
</div>
</div>