diff --git a/src/view/StatsPanel.cpp b/src/view/StatsPanel.cpp index e92e7aa..3c55154 100644 --- a/src/view/StatsPanel.cpp +++ b/src/view/StatsPanel.cpp @@ -20,10 +20,12 @@ #include #include #include +#include +#include #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(); QVBoxLayout *vbox = new QVBoxLayout(); @@ -87,6 +89,8 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent) item->setCheckState (Qt::Checked); } + _account->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + connect(_account, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(OnAccountChange(QListWidgetItem*))); _categories = new QString[user->GetCategoriesNumber()] ; @@ -140,7 +144,7 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent) _categoriesValues[i] = 0.0; _pie = new KDChart::Widget(); - _pie->setType( KDChart::Widget::Pie ); + _pie->setType( KDChart::Widget::Pie); QPen pen; pen.setWidth(2); pen.setColor(Qt::black); @@ -148,20 +152,21 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : KissPanel(kiss, parent) _pie->addLegend(KDChart::Position::South); KDChart::Legend* legend = _pie->legend(); - legend->setOrientation( Qt::Vertical ); - legend->setTitleText( _("Cost repartition") ); + legend->setOrientation(Qt::Vertical); + legend->setTitleText(_("Cost repartition")); QVector< double > vec; vec << 0.0; _categoriesValues = new double[nbCategories]; for(i=0; isetDataset( i, vec, _categories[i] ); + _pie->setDataset(i, vec, _categories[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()); legendTextAttr.setFontSize(64); @@ -213,25 +218,21 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT std::map >::iterator accountYearIt; double total, non_fix; int account, size, i, a, b, percents, nbDays; - double *amounts; QString value; User* user = _kiss->GetUser(); QDate date; bool failed; + QVector vec; + QStringList xlabels; - // if (_chart) + if (_plot) { - // _hbox2->removeWidget(_chart); _hbox2->removeItem(_vbox2); - // _hbox2->removeWidget(_chartCategories); - // delete _chart; + //_hbox2->removeItem(_pie); + delete _plot; } - // first step: create plot - // _plot = new XYPlot(); - - // create dataset - // XYSimpleDataset *dataset = new XYSimpleDataset(); + _plot = new KDChart::Widget(); 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); - // // Line on 0 all over the years - // amounts = new double[nbDays*2]; - // for (a=0; aAddSerie((double *) amounts, nbDays); - // delete[] amounts; + _plot->setDataset(0, vec, ""); - // for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); - // account++, accountIt++, i++) - // { - // if (!_account->IsChecked(account)) - // { - // i-- ; - // continue; - // } + vec.clear(); + for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); + account++, accountIt++, i++) + { + if (_account->item(account)->checkState() != Qt::Checked) + { + i-- ; + continue; + } - // amounts = new double[nbDays*2]; - // size = 0; - // for (a=0; aid][a]; - // } - // dataset->AddSerie((double *) amounts, nbDays); - // // set serie names to be displayed on legend - // dataset->SetSerieName(i+1, user->GetAccountName(accountIt->id)); - // delete[] amounts; - // } + for (a=0; aid][a]; + + _plot->setDataset(i+1, vec, user->GetAccountName(accountIt->id)); + } } else { _kiss->GetStats(monthFrom, yearFrom, monthTo, yearTo, &accountAmounts, &categories); - // // Line on 0 all over the years - // size = ((yearTo - yearFrom) + 1) * 12; - // amounts = new double[size*2]; - // for (a=0; a<(size/12); a++) - // { - // for(b=0; b<12; b++) - // { - // amounts[a*12*2+b*2+0] = a*12+b; - // amounts[a*12*2+b*2+1] = 0; - // } - // } + // Line on 0 all over the years + nbDays = ((yearTo - yearFrom) + 1) * 12; + for (a=0; a<(nbDays/12); a++) + { + for(b=0; b<12; b++) + { + vec << 0.0; + if (b == 0 || b == 5 || (b == 11 && a == ((nbDays/12)-1))) + xlabels << wxUI::months[b] + " " + QString::number(yearFrom+a); + else + xlabels << "" ; + } + } - // dataset->AddSerie((double *) amounts, size); - // delete[] amounts; + _plot->setDataset(0, vec, ""); for (account = 0, i = 0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); account++, accountIt++, i++) @@ -301,47 +292,75 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT continue; } + vec.clear(); // size = accountAmounts[accountIt->id].size(); // amounts = new double[size*13*2]; // size = 0; - // for(a = 0, accountYearIt = accountAmounts[accountIt->id].begin(); - // accountYearIt != accountAmounts[accountIt->id].end(); - // accountYearIt++, a++) - // { - // for(b = 0; b<=12; b++) - // { - // amounts[size*2+0] = a*12+b; - // if (!accountAmounts[accountIt->id][accountYearIt->first].count(b)) - // { - // /* - // If previously afiled, 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 - // */ - // amounts[size*2+1] = - // accountAmounts[accountIt->id][accountYearIt->first][b-1] - // + _kiss->CalcAccountAmount(accountIt->id, b-1, accountYearIt->first, 0); - // failed = true; - // } - // else - // { - // amounts[size*2+1] = accountAmounts[accountIt->id][accountYearIt->first][b]; - // failed = false; - // } - // size++; - // } - // } - // dataset->AddSerie((double *) amounts, size); - // // set serie names to be displayed on legend - // dataset->SetSerieName(i+1, user->GetAccountName(accountIt->id)); - // delete[] amounts; + for(a = 0, accountYearIt = accountAmounts[accountIt->id].begin(); + accountYearIt != accountAmounts[accountIt->id].end(); + accountYearIt++, a++) + { + for(b = 0; b<=12; b++) + { + // amounts[size*2+0] = a*12+b; + if (!accountAmounts[accountIt->id][accountYearIt->first].count(b)) + { + /* + 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 + */ + vec << accountAmounts[accountIt->id][accountYearIt->first][b-1] + + _kiss->CalcAccountAmount(accountIt->id, b-1, accountYearIt->first, 0); + failed = true; + } + else + { + vec << accountAmounts[accountIt->id][accountYearIt->first][b]; + failed = false; + } + size++; + } + } + _plot->setDataset(i+1, vec, user->GetAccountName(accountIt->id)); + // dataset->AddSerie((double *) amounts, size); + // set serie names to be displayed on legend + // dataset->SetSerieName(i+1, user->GetAccountName(accountIt->id)); + // 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 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 // XYLineRenderer *renderer = new XYLineRenderer(true, true); // dataset->SetRenderer(renderer); @@ -371,7 +390,7 @@ void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearT // _chart->SetMinSize(// chart->GetSize() // wxSize(750,550)); - // _hbox2->Add(_chart, 0, wxGROW|wxALL, 5); + _hbox2->addWidget(_plot); total = 0.0; 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); } QVector< double > vec; - vec << _categoriesValues[i]; - _pie->setDataset( i, vec, _categories[i] ); + vec << _categoriesValues[_categoriesIndexes[categoriesIt->first]]; + _pie->setDataset(_categoriesIndexes[categoriesIt->first], vec, _categories[_categoriesIndexes[categoriesIt->first]]); } non_fix = total - _categoriesValues[0]; diff --git a/src/view/StatsPanel.hpp b/src/view/StatsPanel.hpp index fe1a971..49c3d09 100644 --- a/src/view/StatsPanel.hpp +++ b/src/view/StatsPanel.hpp @@ -53,7 +53,6 @@ private: QString* _categories; std::map _categoriesIndexes; QBoxLayout *_hbox2, *_vbox2; - //wxChartPanel* _chart, *_chartCategories; QListWidget* _account; void UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearTo);