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 = [];
|
2016-09-29 09:49:25 +02:00
|
|
|
foreach ($data as $dataset) {
|
2017-01-02 03:57:23 +01:00
|
|
|
$labels[] = $dataset[$key];
|
2016-09-29 09:49:25 +02:00
|
|
|
}
|
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,
|
|
|
|
'fillColor' => $colors[$row_key],
|
|
|
|
'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>
|
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([
|
2017-01-02 15:43:36 +01:00
|
|
|
'labels' => $labels,
|
|
|
|
'datasets' => $datasets
|
|
|
|
]) . ');
|
2015-08-16 23:08:02 +02:00
|
|
|
});
|
|
|
|
</script>';
|
|
|
|
}
|