Rework StatsPanel

This commit is contained in:
Grégory Soutadé 2017-10-29 18:21:54 +01:00 committed by Grégory Soutadé
parent 85338d4b70
commit fea2136e73

View File

@ -163,16 +163,17 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
std::vector<Account>::iterator accountIt; std::vector<Account>::iterator accountIt;
std::map<int, int>::iterator categoriesIt; std::map<int, int>::iterator categoriesIt;
std::map<int, std::map<int, int> >::iterator accountYearIt; std::map<int, std::map<int, int> >::iterator accountYearIt;
std::map<int, int>::iterator accountMonthIt;
int total; int total;
int account, size, i, a, b, nbDays; int account, i, nbDays;
QString value, v; QString value, v;
User* user = _kiss->GetUser(); User* user = _kiss->GetUser();
QDate date; QDate date, start, end;
bool failed;
QLineSeries * series = new QLineSeries(); QLineSeries * series = new QLineSeries();
QLineSeries * series0 = series; QLineSeries * series0 = series;
QDateTimeAxis *axisX = new QDateTimeAxis; QDateTimeAxis *axisX = new QDateTimeAxis;
QDateTime xValue; QDateTime xValue;
double previous;
if (_chartView) if (_chartView)
{ {
@ -189,9 +190,9 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
_kiss->GetMonthStats(monthFrom, yearFrom, nbDays, &operations, &categories, &tags); _kiss->GetMonthStats(monthFrom, yearFrom, nbDays, &operations, &categories, &tags);
// Line on 0 all over the years // Line on 0 all over the years
for (a=0; a<nbDays; a++) for (i=0; i<=nbDays; i++)
{ {
date = QDate(yearFrom, monthFrom+1, a+1); date = QDate(yearFrom, monthFrom+1, i+1);
xValue.setDate(date); xValue.setDate(date);
series->append(xValue.toMSecsSinceEpoch(), 0.0); series->append(xValue.toMSecsSinceEpoch(), 0.0);
} }
@ -201,20 +202,19 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
axisX->setFormat("dd"); axisX->setFormat("dd");
_plot->addSeries(series); _plot->addSeries(series);
i=0; for (account = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end();
for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end();
account++, accountIt++) account++, accountIt++)
{ {
if (_account->item(account)->checkState() != Qt::Checked) if (_account->item(account)->checkState() != Qt::Checked)
continue; continue;
series = new QLineSeries(); series = new QLineSeries();
for (a=0; a<nbDays; a++) for (i=0; i<nbDays; i++)
{ {
date = QDate(yearFrom, monthFrom+1, a+1); date = QDate(yearFrom, monthFrom+1, i+1);
xValue.setDate(date); xValue.setDate(date);
series->append(xValue.toMSecsSinceEpoch(), series->append(xValue.toMSecsSinceEpoch(),
(double) operations[accountIt->id][a] / 100); (double) operations[accountIt->id][i] / 100);
} }
series->setName(user->GetAccountName(accountIt->id)); series->setName(user->GetAccountName(accountIt->id));
@ -226,15 +226,14 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
_kiss->GetStats(monthFrom, yearFrom, monthTo, yearTo, &accountAmounts, &categories, &tags); _kiss->GetStats(monthFrom, yearFrom, monthTo, yearTo, &accountAmounts, &categories, &tags);
// Line on 0 all over the years // Line on 0 all over the years
nbDays = ((yearTo - yearFrom) + 1) * 12; start = QDate(yearFrom, monthFrom+1, 1);
for (a=0; a<(nbDays/12); a++) end = QDate(yearTo, monthTo+1, 1);
end = end.addMonths(1).addMonths(-1);
date = start;
for (date = start; date <= end; date = date.addMonths(1))
{ {
for(b=0; b<12; b++) xValue.setDate(date);
{ series->append(xValue.toMSecsSinceEpoch(), 0.0);
date = QDate(yearFrom+a, monthFrom+b+1, 1);
xValue.setDate(date);
series->append(xValue.toMSecsSinceEpoch(), 0.0);
}
} }
axisX->setLabelsAngle(-60); axisX->setLabelsAngle(-60);
@ -254,38 +253,24 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
} }
series = new QLineSeries(); series = new QLineSeries();
for(a = 0, accountYearIt = accountAmounts[accountIt->id].begin(); previous = 0.0;
for(accountYearIt = accountAmounts[accountIt->id].begin();
accountYearIt != accountAmounts[accountIt->id].end(); accountYearIt != accountAmounts[accountIt->id].end();
accountYearIt++, a++) accountYearIt++)
{ {
for(b = 0; b<=12; b++) for(accountMonthIt = accountAmounts[accountIt->id][accountYearIt->first].begin();
{ accountMonthIt != accountAmounts[accountIt->id][accountYearIt->first].end();
date = QDate(yearFrom+a, b+1, 1); accountMonthIt++)
{
date = QDate(accountYearIt->first, accountMonthIt->first+1, 1);
xValue.setDate(date); xValue.setDate(date);
if (!accountAmounts[accountIt->id][accountYearIt->first].count(b)) previous = accountMonthIt->second / 100;
{ *series << QPointF(xValue.toMSecsSinceEpoch(), previous);
/*
If previously failed, continue to avoid to set
account to 0 (only for display)
*/
if (!b || failed) continue;
/*
Compute cur month value (if there are operations)
as next month value
*/
*series << QPointF(xValue.toMSecsSinceEpoch(),
(double) (accountAmounts[accountIt->id][accountYearIt->first][b-1]
+ _kiss->CalcAccountAmount(accountIt->id, b-1, accountYearIt->first, 0)) / 100);
failed = true;
}
else
{
*series << QPointF(xValue.toMSecsSinceEpoch(),
(double) accountAmounts[accountIt->id][accountYearIt->first][b] / 100);
failed = false;
}
size++;
} }
previous += _kiss->CalcAccountAmount(accountIt->id, date.month()-1, date.year(), 0)/100;
date = date.addMonths(1);
xValue.setDate(date);
*series << QPointF(xValue.toMSecsSinceEpoch(), previous) ;
} }
series->setName(user->GetAccountName(accountIt->id)); series->setName(user->GetAccountName(accountIt->id));
_plot->addSeries(series); _plot->addSeries(series);