1
0

Grafiek fix en sneller statistiek algorithme

This commit is contained in:
JelmerHinssen 2017-04-03 18:21:36 +02:00
parent 8552933a52
commit 4a20995bc3
4 changed files with 57 additions and 25 deletions

7
assets/moment.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -14,30 +14,39 @@ class Statistieken {
}
$xas = array();
for($i = $dagen; $i >= 0; $i--){
$xas[] = date("d M", strtotime("-$i day"));
$xas[] = date("Y-m-d", strtotime("now") - $i * 86400);
}
$container = Application::getContainer();
$user = $container->auth->getUserID();
$db = $container->db;
$saldo = $db->rekeningen->where("rekeningnr", $rekeningNummer)[array("rekeningnr" => $rekeningNummer)]['saldo'];
$eraf = $db->transacties->select("datum, SUM(bedrag) as bedr")->where("van", $rekeningNummer)
->group("datum");
->and("`datum` >= '" . date("Y-m-d", strtotime("now") - $dagen * 86400) . "'")->group("datum")->order("datum DESC");
$erbij = $db->transacties->select("datum, SUM(bedrag) as bedr")->where("naar", $rekeningNummer)
->group("datum");
->group("datum")->order("datum DESC");
$huidigSaldo = (double)$saldo;
$yas = array($huidigSaldo);
for($i = 1; $i <= $dagen; $i++){
$af = (double)$eraf[array("datum" => date("Y-m-d", strtotime("-" . $i + 1 . " day")))]['bedr'];
$bij = (double)$erbij[array("datum" => date("Y-m-d", strtotime("-" . $i + 1 . " day")))]['bedr'];
if(!isset($af)){
$af = 0;
$vandaag = strtotime("now");
$yas = array_fill(0, $dagen + 1, 0);
foreach($eraf as $af){
$dag = strtotime($af['datum']);
$diff = floor(($vandaag - $dag)/86400);
if($diff <= $dagen){
$yas[$dagen - $diff] = (double)$af['bedr'];
}
if(!isset($bij)){
$bij = 0;
}
$huidigSaldo += $af;
$huidigSaldo -= $bij;
array_unshift($yas, $huidigSaldo);
foreach($erbij as $bij){
$dag = strtotime($bij['datum']);
$diff = floor(($vandaag - $dag)/86400);
if($diff <= $dagen){
$yas[$dagen - $diff + 1] -= (double)$bij['bedr'];
}else{
break;
}
}
$verandering = 0;
for($i = $dagen; $i >= 0; $i--){
$huidigSaldo += $verandering;
$verandering = $yas[$i];
$yas[$i] = $huidigSaldo;
}
return [
"x-as" => $xas,

View File

@ -5,6 +5,7 @@
<title>Inforbank</title>
<link rel="stylesheet" href="<?= $header['base'] ?>assets/css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="<?= $header['base']?>assets/moment.min.js"></script>
<script src="<?= $header['base']?>assets/chart.min.js"></script>
<base href="<?= $header['base'] ?>">
</head>

View File

@ -1,27 +1,42 @@
<?php include '__header.phtml'; ?>
<a class="btn btn-default pull-left" href="rekeningen/<?= $rekeningnr ?>" role="button">Terug</a>
<canvas id="myChart" height="100"></canvas>
<canvas id="grafiek" height="100"></canvas>
<script>
function newDate(days) {
return moment().add(days, 'd');
}
Chart.defaults.global.legend.display = false;//Geen legenda
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
var grafiek = document.getElementById("grafiek");
var myChart = new Chart(grafiek, {
type: "line",
options: {
title: {
display: true,
text: "Saldo van de afgelopen maand"
},
scales: {
xAxes: [{
type: "time",
unit: "day",
time: {
displayFormats: {
"day": "D MMM"
}
}
}],
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
},
data: {
//Laad de dagen in
labels: <?php echo json_encode($statistieken['x-as']); ?>,
datasets: [{
label: "Saldo",
//Laad de saldo's in
data: <?php echo json_encode($statistieken['y-as']); ?>,
borderWidth: 2,
lineTension: 0,
lineTension: 0, //Rechte lijnen
fill: false,
borderColor: "black",
pointRadius: 5,