I'm creating a chart for my booking data but based on the sample code here I declare the value and label myself. So what I'm trying to do is creating a chart based on my database but I'm not sure how to retrieve the label and value from my own database? Is there any example? Or anyone can help me?
<?php
// our data
$chartData = [
['label' => '1st Month', 'value' => 10],
['label' => '2nd Month', 'value' => 6],
['label' => '3rd Month', 'value' => 3],
['label' => '4th Month', 'value' => 20],
['label' => '6th Month', 'value' => 20],
['label' => 'nth Month', 'value' => 15],
];
// preparing data for chart
$values = $labels = [];
foreach ($chartData as $key => $data) {
$values[] = [$key, $data['value']];
$labels[] = [$key, $data['label']];
}
$labelDataInJson = json_encode($labels);
// removing first and last [, ] so it can be valid data for chart
$chartDataInJson = strtr(json_encode($values), ['[[' => '[', ']]' => ']']);
?>
<div class="scoreboard" style="padding: 2rem;padding-top: 0;">
<h3>Booking Report</h3>
<div
data-control="chart-line"
data-time-mode="weeks"
style="height: 200px;width: 100%;"
data-chart-options='xaxis: {mode: "none", ticks: <?= $labelDataInJson ?>}'
>
<span
data-chart="dataset"
data-set-color="#008dc9"
data-set-name="Visits"
data-set-data='<?= $chartDataInJson ?>'
</span>
</div>
</div>
