engelsystem/includes/helper/graph_helper.php

47 lines
1.3 KiB
PHP
Raw Normal View History

2015-08-16 23:08:02 +02:00
<?php
/**
* Renders a bargraph
2017-01-02 15:43:36 +01:00
*
2017-01-03 03:22:48 +01:00
* @param string $dom_id
* @param string $key key name of the x-axis
* @param array $row_names key names for the data rows
* @param array $colors colors for the data rows
* @param array $data the data
* @return string
2015-08-16 23:08:02 +02:00
*/
2017-01-02 03:57:23 +01:00
function bargraph($dom_id, $key, $row_names, $colors, $data)
{
$labels = [];
foreach ($data as $dataset) {
2017-01-02 03:57:23 +01:00
$labels[] = $dataset[$key];
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$datasets = [];
foreach ($row_names as $row_key => $name) {
$values = [];
foreach ($data as $dataset) {
$values[] = $dataset[$row_key];
}
$datasets[] = [
2017-01-02 15:43:36 +01:00
'label' => $name,
'backgroundColor' => $colors[$row_key],
2017-01-02 15:43:36 +01:00
'data' => $values
];
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return '<canvas id="' . $dom_id . '" style="width: 100%; height: 300px;"></canvas>
2020-05-13 18:26:32 +02:00
<script type="text/javascript">
$(function(){
var ctx = $(\'#' . $dom_id . '\').get(0).getContext(\'2d\');
var chart = new Chart(ctx, ' . json_encode([
'type' => 'bar',
'data' => [
'labels' => $labels,
'datasets' => $datasets
]
]) . ');
});
</script>';
2015-08-16 23:08:02 +02:00
}