The above example shows you how to create PolarChart
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 basic polar chart.
<?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>
Polar Area 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>
<button id="addData" class="btn">Add Data</button>
<button id="removeData" class="btn">Remove 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);
}
})
})
$('#addData').click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "run.php",
data: {
command: 'addData'
},
success: function(response) {
$('#report_render').html(response);
}
})
})
$('#removeData').click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "run.php",
data: {
command: 'removeData'
},
success: function(response) {
$('#report_render').html(response);
}
})
})
})
</script>
<div>
<div id='report_render'>
</div>
</div>
</body>
</html>
<?php
class MyReport extends \koolreport\KoolReport
{
}
<div style="width:60%;" id="report_render">
<?php
function randomScalingFactor()
{
return mt_rand(0, 100);
}
$data = [
['name' => 'Red', 'series1' => randomScalingFactor()],
['name' => 'Orange', 'series1' => randomScalingFactor()],
['name' => 'Yellow', 'series1' => randomScalingFactor()],
['name' => 'Green', 'series1' => randomScalingFactor()],
['name' => 'Blue', 'series1' => randomScalingFactor()],
];
if (!isset($_POST['command'])) {
$_SESSION['data'] = $data;
}
if (isset($_POST['command']) && $_POST['command'] === 'randomizeData') {
foreach ($_SESSION['data'] as &$entry) {
$entry['series1'] = randomScalingFactor();
}
}
if (isset($_POST['command']) && $_POST['command'] === 'addData') {
$newData = ['name' => 'data' . count($_SESSION['data'])];
$newData['series1'] = randomScalingFactor();
array_push($_SESSION['data'], $newData);
}
if (isset($_POST['command']) && $_POST['command'] == 'removeData') {
array_pop($_SESSION['data']);
}
\koolreport\chartjs\PolarChart::create(array(
'dataSource' => $_SESSION['data'],
'columns' => array(
"name",
"series1"
),
"options" => array(
"responsive" => true,
"legend" => array(
"position" => 'right'
),
"title" => array(
"display" => true,
"text" => "Chart.js Polar Area Chart"
),
"scale" => array(
"ticks" => array(
"beginAtZero" => true
),
"reverse" => false
),
"animation" => array(
"animateRotate" => false,
"animateScale" => true
)
),
"colorScheme" => array(
"rgb(255, 99, 132, 0.5)",
"rgb(255, 159, 64, 0.5)",
"rgb(255, 205, 86, 0.5)",
"rgb(75, 192, 192, 0.5)",
"rgb(54, 162, 235, 0.5)"
)
));
?>
</div>