engelsystem/includes/helper/graph_helper.php

41 lines
1.0 KiB
PHP
Raw Normal View History

2015-08-16 23:08:02 +02:00
<?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
*/
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 03:57:23 +01:00
$datasets = [];
foreach ($row_names as $row_key => $name) {
$values = [];
foreach ($data as $dataset) {
$values[] = $dataset[$row_key];
}
$datasets[] = [
2015-08-16 23:08:02 +02:00
'label' => $name,
'fillColor' => $colors[$row_key],
2017-01-02 03:57:23 +01:00
'data' => $values
2015-08-16 23:08:02 +02:00
];
2017-01-02 03:57:23 +01:00
}
2015-08-16 23:08:02 +02:00
2017-01-02 03:57:23 +01:00
return '<canvas id="' . $dom_id . '" style="width: 100%; height: 300px;"></canvas>
2015-08-16 23:08:02 +02:00
<script type="text/javascript">
$(function(){
2016-09-29 12:45:06 +02:00
var ctx = $("#' . $dom_id . '").get(0).getContext("2d");
2015-08-16 23:08:02 +02:00
var chart = new Chart(ctx).Bar(' . json_encode([
'labels' => $labels,
2017-01-02 03:57:23 +01:00
'datasets' => $datasets
2015-08-16 23:08:02 +02:00
]) . ');
});
</script>';
}