Work on StatsPanel (2)

This commit is contained in:
Grégory Soutadé 2012-02-11 10:47:09 +01:00
parent c05fbb95f6
commit 41abf4a827
2 changed files with 116 additions and 98 deletions

View File

@ -20,10 +20,12 @@
#include <QMessageBox> #include <QMessageBox>
#include <KDChartLegend> #include <KDChartLegend>
#include <KDChartPieDiagram> #include <KDChartPieDiagram>
#include <KDChartLineDiagram>
#include <KDChartCartesianAxis>
#include "StatsPanel.hpp" #include "StatsPanel.hpp"
StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent) //, _plot(0), _chart(0) StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent), _plot(0)
{ {
QHBoxLayout *hbox = new QHBoxLayout(); QHBoxLayout *hbox = new QHBoxLayout();
QVBoxLayout *vbox = new QVBoxLayout(); QVBoxLayout *vbox = new QVBoxLayout();
@ -87,6 +89,8 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent)
item->setCheckState (Qt::Checked); item->setCheckState (Qt::Checked);
} }
_account->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
connect(_account, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(OnAccountChange(QListWidgetItem*))); connect(_account, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(OnAccountChange(QListWidgetItem*)));
_categories = new QString[user->GetCategoriesNumber()] ; _categories = new QString[user->GetCategoriesNumber()] ;
@ -140,7 +144,7 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent)
_categoriesValues[i] = 0.0; _categoriesValues[i] = 0.0;
_pie = new KDChart::Widget(); _pie = new KDChart::Widget();
_pie->setType( KDChart::Widget::Pie ); _pie->setType( KDChart::Widget::Pie);
QPen pen; QPen pen;
pen.setWidth(2); pen.setWidth(2);
pen.setColor(Qt::black); pen.setColor(Qt::black);
@ -148,20 +152,21 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent)
_pie->addLegend(KDChart::Position::South); _pie->addLegend(KDChart::Position::South);
KDChart::Legend* legend = _pie->legend(); KDChart::Legend* legend = _pie->legend();
legend->setOrientation( Qt::Vertical ); legend->setOrientation(Qt::Vertical);
legend->setTitleText( _("Cost repartition") ); legend->setTitleText(_("Cost repartition"));
QVector< double > vec; QVector< double > vec;
vec << 0.0; vec << 0.0;
_categoriesValues = new double[nbCategories]; _categoriesValues = new double[nbCategories];
for(i=0; i<nbCategories; i++) for(i=0; i<nbCategories; i++)
{ {
_categoriesValues[i] = 0.0; _categoriesValues[i] = 0.0;
_pie->setDataset( i, vec, _categories[i] ); _pie->setDataset(i, vec, _categories[i]);
_pie->pieDiagram()->setBrush(i, QBrush(wxUI::categoryColors[i])); _pie->pieDiagram()->setBrush(i, QBrush(wxUI::categoryColors[i]));
legend->setText( i, _categories[i] ); legend->setText(i, _categories[i]);
} }
_pie->setMaximumSize( 200, 400 ); _pie->setMinimumSize(200, 400);
_pie->setMaximumSize(200, 400);
KDChart::TextAttributes legendTextAttr(legend->textAttributes()); KDChart::TextAttributes legendTextAttr(legend->textAttributes());
legendTextAttr.setFontSize(64); legendTextAttr.setFontSize(64);
@ -213,25 +218,21 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
std::map<int, std::map<int, double> >::iterator accountYearIt; std::map<int, std::map<int, double> >::iterator accountYearIt;
double total, non_fix; double total, non_fix;
int account, size, i, a, b, percents, nbDays; int account, size, i, a, b, percents, nbDays;
double *amounts;
QString value; QString value;
User* user = _kiss->GetUser(); User* user = _kiss->GetUser();
QDate date; QDate date;
bool failed; bool failed;
QVector<double> vec;
QStringList xlabels;
// if (_chart) if (_plot)
{ {
// _hbox2->removeWidget(_chart);
_hbox2->removeItem(_vbox2); _hbox2->removeItem(_vbox2);
// _hbox2->removeWidget(_chartCategories); //_hbox2->removeItem(_pie);
// delete _chart; delete _plot;
} }
// first step: create plot _plot = new KDChart::Widget();
// _plot = new XYPlot();
// create dataset
// XYSimpleDataset *dataset = new XYSimpleDataset();
if (monthFrom == monthTo && yearFrom == yearTo) if (monthFrom == monthTo && yearFrom == yearTo)
{ {
@ -240,57 +241,47 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
_kiss->GetMonthStats(monthFrom, yearFrom, nbDays, &operations, &categories); _kiss->GetMonthStats(monthFrom, yearFrom, nbDays, &operations, &categories);
// // Line on 0 all over the years // Line on 0 all over the years
// amounts = new double[nbDays*2]; for (a=0; a<nbDays; a++)
// for (a=0; a<nbDays; a++) vec << 0.0;
// {
// amounts[a*2+0] = a;
// amounts[a*2+1] = 0;
// }
// dataset->AddSerie((double *) amounts, nbDays); _plot->setDataset(0, vec, "");
// delete[] amounts;
// for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); vec.clear();
// account++, accountIt++, i++) for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end();
// { account++, accountIt++, i++)
// if (!_account->IsChecked(account)) {
// { if (_account->item(account)->checkState() != Qt::Checked)
// i-- ; {
// continue; i-- ;
// } continue;
}
// amounts = new double[nbDays*2]; for (a=0; a<nbDays; a++)
// size = 0; vec << operations[accountIt->id][a];
// for (a=0; a<nbDays; a++)
// { _plot->setDataset(i+1, vec, user->GetAccountName(accountIt->id));
// amounts[a*2+0] = a; }
// amounts[a*2+1] = operations[accountIt->id][a];
// }
// dataset->AddSerie((double *) amounts, nbDays);
// // set serie names to be displayed on legend
// dataset->SetSerieName(i+1, user->GetAccountName(accountIt->id));
// delete[] amounts;
// }
} }
else else
{ {
_kiss->GetStats(monthFrom, yearFrom, monthTo, yearTo, &accountAmounts, &categories); _kiss->GetStats(monthFrom, yearFrom, monthTo, yearTo, &accountAmounts, &categories);
// // Line on 0 all over the years // Line on 0 all over the years
// size = ((yearTo - yearFrom) + 1) * 12; nbDays = ((yearTo - yearFrom) + 1) * 12;
// amounts = new double[size*2]; for (a=0; a<(nbDays/12); a++)
// for (a=0; a<(size/12); a++) {
// { for(b=0; b<12; b++)
// for(b=0; b<12; b++) {
// { vec << 0.0;
// amounts[a*12*2+b*2+0] = a*12+b; if (b == 0 || b == 5 || (b == 11 && a == ((nbDays/12)-1)))
// amounts[a*12*2+b*2+1] = 0; xlabels << wxUI::months[b] + " " + QString::number(yearFrom+a);
// } else
// } xlabels << "" ;
}
}
// dataset->AddSerie((double *) amounts, size); _plot->setDataset(0, vec, "");
// delete[] amounts;
for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end();
account++, accountIt++, i++) account++, accountIt++, i++)
@ -301,47 +292,75 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
continue; continue;
} }
vec.clear();
// size = accountAmounts[accountIt->id].size(); // size = accountAmounts[accountIt->id].size();
// amounts = new double[size*13*2]; // amounts = new double[size*13*2];
// size = 0; // size = 0;
// for(a = 0, accountYearIt = accountAmounts[accountIt->id].begin(); for(a = 0, accountYearIt = accountAmounts[accountIt->id].begin();
// accountYearIt != accountAmounts[accountIt->id].end(); accountYearIt != accountAmounts[accountIt->id].end();
// accountYearIt++, a++) accountYearIt++, a++)
// { {
// for(b = 0; b<=12; b++) for(b = 0; b<=12; b++)
// { {
// amounts[size*2+0] = a*12+b; // amounts[size*2+0] = a*12+b;
// if (!accountAmounts[accountIt->id][accountYearIt->first].count(b)) if (!accountAmounts[accountIt->id][accountYearIt->first].count(b))
// { {
// /* /*
// If previously afiled, continue to avoid to set If previously failed, continue to avoid to set
// account to 0 (only for display) account to 0 (only for display)
// */ */
// if (!b || failed) continue; if (!b || failed) continue;
// /* /*
// Compute cur month value (if there are operations) Compute cur month value (if there are operations)
// as next month value as next month value
// */ */
// amounts[size*2+1] = vec << accountAmounts[accountIt->id][accountYearIt->first][b-1]
// accountAmounts[accountIt->id][accountYearIt->first][b-1] + _kiss->CalcAccountAmount(accountIt->id, b-1, accountYearIt->first, 0);
// + _kiss->CalcAccountAmount(accountIt->id, b-1, accountYearIt->first, 0); failed = true;
// failed = true; }
// } else
// else {
// { vec << accountAmounts[accountIt->id][accountYearIt->first][b];
// amounts[size*2+1] = accountAmounts[accountIt->id][accountYearIt->first][b]; failed = false;
// failed = false; }
// } size++;
// size++; }
// } }
// } _plot->setDataset(i+1, vec, user->GetAccountName(accountIt->id));
// dataset->AddSerie((double *) amounts, size); // dataset->AddSerie((double *) amounts, size);
// // set serie names to be displayed on legend // set serie names to be displayed on legend
// dataset->SetSerieName(i+1, user->GetAccountName(accountIt->id)); // dataset->SetSerieName(i+1, user->GetAccountName(accountIt->id));
// delete[] amounts; // delete[] amounts;
} }
} }
KDChart::CartesianAxis *xAxis = new KDChart::CartesianAxis(_plot->lineDiagram());
KDChart::CartesianAxis *yAxis = new KDChart::CartesianAxis (_plot->lineDiagram());
xAxis->setPosition (KDChart::CartesianAxis::Bottom);
yAxis->setPosition (KDChart::CartesianAxis::Left);
xAxis->setTitleText (_("Months"));
yAxis->setTitleText (_("Amount"));
xAxis->setLabels(xlabels);
_plot->lineDiagram()->addAxis(xAxis);
_plot->lineDiagram()->addAxis(yAxis);
_plot->addLegend(KDChart::Position::East);
KDChart::Legend* legend = _plot->legend();
legend->setOrientation(Qt::Vertical);
legend->setTitleText(_("Accounts"));
legend->setText(0, _("0 line"));
{
QVector<QString> vec;
i = 1;
for (a=0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end();
a++, accountIt++)
{
if (_account->item(a)->checkState() == Qt::Checked)
legend->setText(i++, accountIt->name);
}
}
// // create line renderer and set it to dataset // // create line renderer and set it to dataset
// XYLineRenderer *renderer = new XYLineRenderer(true, true); // XYLineRenderer *renderer = new XYLineRenderer(true, true);
// dataset->SetRenderer(renderer); // dataset->SetRenderer(renderer);
@ -371,7 +390,7 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
// _chart->SetMinSize(// chart->GetSize() // _chart->SetMinSize(// chart->GetSize()
// wxSize(750,550)); // wxSize(750,550));
// _hbox2->Add(_chart, 0, wxGROW|wxALL, 5); _hbox2->addWidget(_plot);
total = 0.0; total = 0.0;
for(categoriesIt = categories.begin(); categoriesIt != categories.end(); categoriesIt++) for(categoriesIt = categories.begin(); categoriesIt != categories.end(); categoriesIt++)
@ -396,8 +415,8 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT
_statsGrid->item(_categoriesIndexes[categoriesIt->first], 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); _statsGrid->item(_categoriesIndexes[categoriesIt->first], 1)->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
} }
QVector< double > vec; QVector< double > vec;
vec << _categoriesValues[i]; vec << _categoriesValues[_categoriesIndexes[categoriesIt->first]];
_pie->setDataset( i, vec, _categories[i] ); _pie->setDataset(_categoriesIndexes[categoriesIt->first], vec, _categories[_categoriesIndexes[categoriesIt->first]]);
} }
non_fix = total - _categoriesValues[0]; non_fix = total - _categoriesValues[0];

View File

@ -53,7 +53,6 @@ private:
QString* _categories; QString* _categories;
std::map<int, int> _categoriesIndexes; std::map<int, int> _categoriesIndexes;
QBoxLayout *_hbox2, *_vbox2; QBoxLayout *_hbox2, *_vbox2;
//wxChartPanel* _chart, *_chartCategories;
QListWidget* _account; QListWidget* _account;
void UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearTo); void UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearTo);