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 logarithmic y-axis values using the type
property.
For example:
...
"yAxes" => array(
array(
...
"type" => "logarithmic"
)
)
...
<?php
require_once "../../../load.koolreport.php";
require_once "MyReport.php";
$report = new MyReport;
$report->run();
// $report->render();
?>
<?php
if (isset($_POST['command']) && $_POST['command'] === 'randomizeData') {
?>
<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>
Logarithmic Line Chart
</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()
{
$firstRandom = ceil(mt_rand() / mt_getrandmax() * 10.0);
$secondRandom = ceil(mt_rand() / mt_getrandmax() * 5);
$result = $firstRandom * pow(10, $secondRandom);
return $result;
}
$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()],
];
\koolreport\chartjs\LineChart::create(array(
'dataSource' => $data,
'columns' => array(
"month",
"My First dataset" => array(
"fill" => false,
"borderColor" => 'rgb(255, 99, 132)',
"backgroundColor" => 'rgb(255, 99, 132)'
),
"My Second dataset" => array(
"fill" => false,
"borderColor" => 'rgb(54, 162, 235)',
"backgroundColor" => 'rgb(54, 162, 235)'
)
),
"options" => array(
"responsive" => true,
"title" => array(
"display" => true,
"text" => 'Chart.js Line Chart - Logarithmic'
),
"scales" => array(
"xAxes" => array(
array(
"display" => true
)
),
"yAxes" => array(
array(
"display" => true,
"type" => "logarithmic"
)
)
)
)
));
?>
</div>