Add statistics

Update README
Update TODO
This commit is contained in:
2010-08-18 21:28:40 +02:00
parent 74c401c8c7
commit 5da154cd48
9 changed files with 251 additions and 124 deletions

View File

@@ -19,24 +19,65 @@ along with KissCount. If not, see <http://www.gnu.org/licenses/>.
#include "StatsPanel.h"
StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent)
enum {RANGE_ID=1, ACCOUNTS_ID};
BEGIN_EVENT_TABLE(StatsPanel, wxPanel)
EVT_CHOICE(RANGE_ID, StatsPanel::OnRangeChange)
EVT_CHECKLISTBOX(ACCOUNTS_ID, StatsPanel::OnAccountsChange)
END_EVENT_TABLE()
StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _kiss(kiss), _wxUI(parent), _plot(NULL), _chart(NULL)
{
wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);
_hbox2 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *vbox2 = new wxBoxSizer(wxVERTICAL);
_vbox2 = new wxBoxSizer(wxVERTICAL);
int i;
User* user = _kiss->GetUser();
std::vector<Account>::iterator accountIt;
std::vector<Category>::iterator categoryIt;
std::map<int, std::vector<int> > operations;
std::map<int, std::vector<int> >::iterator it;
SetSizer(vbox);
_monthFrom = new wxChoice (this, -1, wxDefaultPosition, wxDefaultSize, 12, months);
_monthTo = new wxChoice (this, -1, wxDefaultPosition, wxDefaultSize, 12, months);
_monthFrom = new wxChoice (this, RANGE_ID, wxDefaultPosition, wxDefaultSize, 12, months);
_yearFrom = new wxChoice (this, RANGE_ID);
_monthTo = new wxChoice (this, RANGE_ID, wxDefaultPosition, wxDefaultSize, 12, months);
_yearTo = new wxChoice (this, RANGE_ID);
operations = _kiss->GetAllOperations();
for(i=0, it = operations.begin(); it != operations.end(); it++, i++)
{
_yearFrom->Append(wxString::Format(wxT("%d"), it->first));
_yearTo->Append(wxString::Format(wxT("%d"), it->first));
}
_monthFrom->Select(0);
_monthTo->Select(11);
_yearFrom->Select(i);
_yearTo->Select(i);
wxStaticText* label = new wxStaticText(this, wxID_ANY, _("From"));
hbox->Add(label);
hbox->Add(-1, 10);
hbox->Add(_monthFrom);
hbox->Add(_yearFrom);
hbox->Add(-1, 30);
label = new wxStaticText(this, wxID_ANY, _("To"));
hbox->Add(label);
hbox->Add(-1, 10);
hbox->Add(_monthTo);
hbox->Add(_yearTo);
_account = new wxCheckListBox(this, ACCOUNTS_ID);
for(i=0, accountIt = user->_accounts.begin(); accountIt != user->_accounts.end(); accountIt++, i++)
{
_account->Append(accountIt->name);
_account->Check(i);
}
_categories = new wxString[user->GetCategoriesNumber()] ;
for(i=0, categoryIt = user->_categories.begin();
@@ -65,7 +106,9 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _ki
_statsGrid->SetCellAlignment(i, 1, wxALIGN_RIGHT, wxALIGN_CENTRE);
}
vbox2->Add(_statsGrid);
_vbox2->Add(_account);
_vbox2->Add(-1, 10);
_vbox2->Add(_statsGrid);
_pie = new PiePlot();
@@ -90,20 +133,21 @@ StatsPanel::StatsPanel(KissCount* kiss, wxUI *parent) : wxPanel(&(*parent)), _ki
chart->SetMinSize(// chart->GetSize()
wxSize(200,250));
UpdateStats(hbox2);
wxCommandEvent event ;
OnRangeChange(event);
vbox2->Add(chart);
_vbox2->Add(-1, 10);
_vbox2->Add(chart);
hbox2->Add(vbox2);
vbox->Add(hbox);
vbox->Add(hbox2);
vbox->Add(_hbox2);
Fit();
Hide();
}
void StatsPanel::UpdateStats(wxBoxSizer *hbox2)
void StatsPanel::UpdateStats(int monthFrom, int yearFrom, int monthTo, int yearTo)
{
std::map<wxString, std::map<int, std::map<int, double> > > accountAmounts;
std::map<wxString, double> categories;
@@ -111,12 +155,19 @@ void StatsPanel::UpdateStats(wxBoxSizer *hbox2)
std::map<wxString, std::map<int, std::map<int, double> > >::iterator accountIdIt;
std::map<int, std::map<int, double> >::iterator accountYearIt;
double total;
int size, i, a, b, percents;
int size, i, a, b, percents, account;
double *amounts;
User* user = _kiss->GetUser();
wxString value;
User* user = _kiss->GetUser();
_kiss->GetStats(0, 2010, 11, 2010, &accountAmounts, &categories);
if (_chart)
{
_hbox2->Detach(_chart);
_hbox2->Detach(_vbox2);
delete _chart;
}
_kiss->GetStats(monthFrom, yearFrom, monthTo, yearTo, &accountAmounts, &categories);
// first step: create plot
_plot = new XYPlot();
@@ -124,9 +175,30 @@ void StatsPanel::UpdateStats(wxBoxSizer *hbox2)
// create dataset
XYSimpleDataset *dataset = new XYSimpleDataset();
// add two series
for (i = 0, accountIdIt = accountAmounts.begin(); accountIdIt != accountAmounts.end(); accountIdIt++, i++)
// 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;
}
}
dataset->AddSerie((double *) amounts, size);
delete[] amounts;
for (account = 0, i = 0, accountIdIt = accountAmounts.begin(); accountIdIt != accountAmounts.end();
accountIdIt++, i++, account++)
{
if (!((wxCheckListBox*)_account)->IsChecked(account))
{
i-- ;
continue;
}
size = accountAmounts[accountIdIt->first].size();
amounts = new double[size*12*2];
size = 0;
@@ -145,12 +217,12 @@ void StatsPanel::UpdateStats(wxBoxSizer *hbox2)
}
dataset->AddSerie((double *) amounts, size);
// set serie names to be displayed on legend
dataset->SetSerieName(i, user->GetAccountName(accountIdIt->first));
dataset->SetSerieName(i+1, user->GetAccountName(accountIdIt->first));
delete[] amounts;
}
// create line renderer and set it to dataset
XYLineRenderer *renderer = new XYLineRenderer();
XYLineRenderer *renderer = new XYLineRenderer(true, true);
dataset->SetRenderer(renderer);
// add our dataset to plot
@@ -168,26 +240,17 @@ void StatsPanel::UpdateStats(wxBoxSizer *hbox2)
_plot->LinkDataVerticalAxis(0, 0);
_plot->LinkDataHorizontalAxis(0, 0);
// create line marker
LineMarker *lineMarker = new LineMarker(wxColour(255, 0, 0), 2);
// set value to be marked, in our case horizontal value 15
lineMarker->SetHorizontalLine(25);
// and add marker to dataset
dataset->AddMarker(lineMarker);
// set legend
_plot->SetLegend(new Legend(wxCENTER, wxRIGHT));
wxChartPanel* chart = new wxChartPanel(this);
chart->SetChart(new Chart(_plot, _("Accounts")));
chart->Fit();
chart->Layout();
chart->SetMinSize(// chart->GetSize()
_chart = new wxChartPanel(this);
_chart->SetChart(new Chart(_plot, _("Accounts")));
_chart->Fit();
_chart->Layout();
_chart->SetMinSize(// chart->GetSize()
wxSize(750,550));
hbox2->Add(chart);
_hbox2->Add(_chart);
total = 0.0;
for(categoriesIt = categories.begin(); categoriesIt != categories.end(); categoriesIt++)
@@ -205,6 +268,10 @@ void StatsPanel::UpdateStats(wxBoxSizer *hbox2)
_statsGrid->AutoSizeColumn(1, true);
_pie->DatasetChanged(_dataset);
_hbox2->Add(_vbox2);
Layout();
}
void StatsPanel::OnShow(wxShowEvent& event)
@@ -212,3 +279,26 @@ void StatsPanel::OnShow(wxShowEvent& event)
_wxUI->SetTitle(_kiss->GetUser()->_name + _(" - ") + _("Statistics"));
}
void StatsPanel::OnRangeChange(wxCommandEvent& event)
{
int monthFrom, monthTo, yearFrom, yearTo;
monthFrom = _monthFrom->GetCurrentSelection();
_yearFrom->GetStringSelection().ToLong((long*)&yearFrom);
monthTo = _monthTo->GetCurrentSelection();
_yearTo->GetStringSelection().ToLong((long*)&yearTo);
if (yearTo > yearFrom ||
(yearFrom == yearTo && monthFrom >= monthTo))
{
wxMessageBox(_("Invalide date range"), _("KissCount"), wxICON_ERROR | wxOK);
return;
}
UpdateStats(monthFrom, yearFrom, monthTo, yearTo);
}
void StatsPanel::OnAccountsChange(wxCommandEvent& event)
{
OnRangeChange(event);
}