You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB
PHTML

<?php
/**
* Renders a bargraph
* @param string $key keyname of the x-axis
* @param array $row_names keynames for the data rows
* @param unknown $colors colors for the data rows
* @param unknown $data the data
*/
8 years ago
function bargraph($dom_id, $key, $row_names, $colors, $data)
{
$labels = [];
foreach ($data as $dataset) {
8 years ago
$labels[] = $dataset[$key];
}
8 years ago
$datasets = [];
foreach ($row_names as $row_key => $name) {
$values = [];
foreach ($data as $dataset) {
$values[] = $dataset[$row_key];
}
$datasets[] = [
'label' => $name,
'fillColor' => $colors[$row_key],
8 years ago
'data' => $values
];
8 years ago
}
8 years ago
return '<canvas id="' . $dom_id . '" style="width: 100%; height: 300px;"></canvas>
<script type="text/javascript">
$(function(){
var ctx = $("#' . $dom_id . '").get(0).getContext("2d");
var chart = new Chart(ctx).Bar(' . json_encode([
'labels' => $labels,
8 years ago
'datasets' => $datasets
]) . ');
});
</script>';
}