HatchKeeper  0.90
The Free Open-Source Egg Incubation Software
GraphDialog.cpp
Go to the documentation of this file.
1 /*******************************************************************/
8 #ifndef __NO_GRAPH__
9 
10 #include <wx/bars/barplot.h>
11 
12 #include <wx/chartpanel.h>
13 
14 #include <wx/axis/numberaxis.h>
15 #include <wx/axis/categoryaxis.h>
16 
17 #include <wx/xy/xyhistorenderer.h>
18 
19 #include <wx/category/categorysimpledataset.h>
20 
21 
28 class GraphDialog : public wxDialog
29 {
30  protected:
31  wxChartPanel* GraphChartPanel;
32  wxStaticLine* m_staticline1;
33  wxListView* GraphList;
34  wxChoice* GraphModeChoice;
35  wxStaticText* GraphModeStat;
36  wxButton* GraphRefreshButton;
37  wxButton* GraphSaveButton;
38  wxButton* GraphCloseButton;
39  wxCheckBox* BlueCheckBox;
40  wxCheckBox* YellowCheckBox;
41  wxCheckBox* RedCheckBox;
42  wxCheckBox* GreenCheckBox;
43 
44  void OnModeChoice(wxCommandEvent&);
45  void OnListSelect(wxListEvent& evt);
46  void OnGraphRefresh(wxCommandEvent&);
47  void OnGraphSave(wxCommandEvent&);
48  void OnGraphClose(wxCommandEvent&);
49 
50  void FillGraphList(int a);
51  vector<int> GroupAverage(int a);
52  public:
53  GraphDialog( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 1000,650 ), long style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER );
54 
55 };
56 
58 GraphDialog::GraphDialog( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxDialog( parent, id, title, pos, size, style )
59 {
60  this->SetSizeHints( wxDefaultSize, wxDefaultSize );
61  //Set Color
62  vector<int> ColorRed = Settings.GetIntArray("ColorRed");
63  vector<int> ColorGreen = Settings.GetIntArray("ColorGreen");
64  vector<int> ColorBlue = Settings.GetIntArray("ColorBlue");
65  this->SetForegroundColour( wxColor(ColorRed[8],ColorGreen[8],ColorBlue[8]) );
66  this->SetBackgroundColour( wxColor(ColorRed[7],ColorGreen[7],ColorBlue[7]) );
67 
68  wxBoxSizer* bSizer1;
69  bSizer1 = new wxBoxSizer( wxVERTICAL );
70 
71  GraphChartPanel = new wxChartPanel(this,-1);
72  bSizer1->Add(GraphChartPanel, 1,wxALL|wxEXPAND, 5);
73 
74  m_staticline1 = new wxStaticLine( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
75  bSizer1->Add( m_staticline1, 0, wxEXPAND | wxALL, 5 );
76 
77  wxGridSizer* gSizer1;
78  gSizer1 = new wxGridSizer( 0, 3, 0, 0 );
79 
80  GraphList = new wxListView( this, wxID_ANY, wxDefaultPosition, wxSize( -1,-1 ),wxLC_REPORT );
81  GraphList->Enable(false);
82  gSizer1->Add( GraphList, 1, wxALL|wxEXPAND, 5 );
83 
84  wxBoxSizer* bSizer2;
85  bSizer2 = new wxBoxSizer( wxVERTICAL );
86 
87  wxGridSizer* gSizer2;
88  gSizer2 = new wxGridSizer( 0, 2, 0, 0 );
89 
90  wxArrayString GraphModeChoiceChoices;
91  GraphModeChoice = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, GraphModeChoiceChoices, 0 );
92  GraphModeChoice->SetSelection( 0 );
93  gSizer2->Add( GraphModeChoice, 0, wxALL|wxEXPAND, 5 );
94 
95  GraphModeStat = new wxStaticText( this, wxID_ANY, l("Step 1 - Graph Mode"), wxDefaultPosition, wxDefaultSize, 0 );
96  GraphModeStat->Wrap( -1 );
97  gSizer2->Add( GraphModeStat, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_CENTER_HORIZONTAL, 5 );
98 
99 
100  bSizer2->Add( gSizer2, 1, wxEXPAND, 5 );
101 
102  GraphRefreshButton = new wxButton( this, wxID_ANY, l("Step 3 - Refresh Graph"), wxDefaultPosition, wxDefaultSize, 0 );
103  GraphRefreshButton->Enable(false);
104  bSizer2->Add( GraphRefreshButton, 0, wxALL|wxEXPAND, 5 );
105 
106  GraphSaveButton = new wxButton( this,wxID_ANY, l("Save Graph As Image"), wxDefaultPosition, wxDefaultSize, 0 );
107  GraphSaveButton->Enable(false);
108  bSizer2->Add( GraphSaveButton, 0, wxALL|wxEXPAND, 5 );
109 
110  GraphCloseButton = new wxButton( this, wxID_ANY, l("Close"), wxDefaultPosition, wxDefaultSize, 0 );
111  bSizer2->Add( GraphCloseButton, 0, wxALL|wxEXPAND, 5 );
112 
113 
114  gSizer1->Add( bSizer2, 1, wxEXPAND, 5 );
115 
116  wxBoxSizer* bSizer3;
117  bSizer3 = new wxBoxSizer( wxVERTICAL );
118 
119  BlueCheckBox = new wxCheckBox( this, wxID_ANY, l("Show % Were Fertile(Blue)"), wxDefaultPosition, wxDefaultSize, 0 );
120  BlueCheckBox->SetValue(true);
121  bSizer3->Add( BlueCheckBox, 1, wxALL, 5 );
122 
123  YellowCheckBox = new wxCheckBox( this, wxID_ANY, l("Show % Of Fertile Hatched(Yellow)"), wxDefaultPosition, wxDefaultSize, 0 );
124  YellowCheckBox->SetValue(true);
125  bSizer3->Add( YellowCheckBox, 1, wxALL, 5 );
126 
127  RedCheckBox = new wxCheckBox( this, wxID_ANY, l("Show % Of Total Hatched(Red)"), wxDefaultPosition, wxDefaultSize, 0 );
128  RedCheckBox->SetValue(true);
129  bSizer3->Add( RedCheckBox, 1, wxALL, 5 );
130 
131  GreenCheckBox = new wxCheckBox( this, wxID_ANY, l("Show % Of Total Unhatched(Green)"), wxDefaultPosition, wxDefaultSize, 0 );
132  GreenCheckBox->SetValue(true);
133  bSizer3->Add( GreenCheckBox, 1, wxALL, 5 );
134 
135 
136  gSizer1->Add( bSizer3, 1, wxEXPAND, 5 );
137 
138 
139  bSizer1->Add( gSizer1, 0, wxEXPAND, 5 );
140  GraphModeChoice->Bind(wxEVT_CHOICE, &GraphDialog::OnModeChoice, this);
141  GraphList->Bind(wxEVT_COMMAND_LIST_ITEM_SELECTED, &GraphDialog::OnListSelect, this);
142  GraphList->Bind(wxEVT_COMMAND_LIST_ITEM_DESELECTED, &GraphDialog::OnListSelect, this);
143  GraphRefreshButton->Bind(wxEVT_BUTTON, &GraphDialog::OnGraphRefresh, this);
144  GraphSaveButton->Bind(wxEVT_BUTTON, &GraphDialog::OnGraphSave, this);
145  GraphCloseButton->Bind(wxEVT_BUTTON, &GraphDialog::OnGraphClose, this);
146 
147  this->SetSizer( bSizer1 );
148  this->Layout();
149 
150  this->Centre( wxBOTH );
151 
152  //Set up Mode options
153  GraphModeChoice->Append(l("Select..."));
154  GraphModeChoice->Append(l("Multiple Batches"));
155  GraphModeChoice->Append(l("Single Group"));
156  GraphModeChoice->Append(l("Multiple Groups"));
157  GraphModeChoice->SetSelection(0);
158 
159  wxInitAllImageHandlers();//set up for exporting as png
160 }
161 
162 
163 void GraphDialog::OnModeChoice(wxCommandEvent&)
164 {
165  int a = GraphModeChoice->GetSelection();
166 
167  if(a == 0) {
168  GraphRefreshButton->Enable(false);
169  GraphList->ClearAll();//Clear the previous list items
170  return;
171  }
172  else
173  FillGraphList(a);
174 
175  GraphList->Enable(true);
176  GraphRefreshButton->Enable(false);
177 }
178 
179 void GraphDialog::OnListSelect(wxListEvent& evt) {
180 
181  int a = GraphModeChoice->GetSelection();
182 
183  switch(a){
184  case 2: {
185  if(GraphList->GetSelectedItemCount() > 0)
186  GraphRefreshButton->Enable(true);
187  else
188  GraphRefreshButton->Enable(false);
189  break;
190  }
191  default: {
192  if(GraphList->GetSelectedItemCount() > 1)
193  GraphRefreshButton->Enable(true);
194  else
195  GraphRefreshButton->Enable(false);
196  break;
197  }
198  }
199 }
200 
202 void GraphDialog::OnGraphRefresh(wxCommandEvent&)
203 {
204  wxString GrTitle; //graph title
205  vector<string> TempNames;
206  vector<double> TempValues1,TempValues2,TempValues3,TempValues4;
207 
208  int b, sel; //misc numbers
209  vector<int> z; //holds all averages for one group
210  int a = GraphModeChoice->GetSelection();
211  bool checker; //for checking if certain bars should be displayed
212 
213  //get selected items and data
214  switch(a) {
215  case 0: {
216  wxMessageDialog dialog(this,
217  l("Hey Change The Mode And Select\nSomthing From The List!"));
218  dialog.ShowModal();
219  return;
220  }
221  case 1: {
222  for(b = 0; b < Batches.GetCount(); b++) {
223  if(!GraphList->IsSelected(b))
224  continue;
225  else {
226  TempNames.push_back(Batches.GetName(b));
227  TempValues1.push_back(Batches.PercentFertile(b));
228  TempValues2.push_back(Batches.PercentOfFertile(b));
229  TempValues3.push_back(Batches.PercentOfTotal(b));
230  TempValues4.push_back(Batches.PercentOfTotalUnhatched(b));
231  }
232  }
233  if(TempNames.size() < 2) {
234  wxMessageDialog dialog(this,
235  l("You'll Need To Select More Than 1\n Batch For This To Work.\n\nHold CTRL or Cmd + Click\n To Select Multiple Items."));
236  dialog.ShowModal();
237  return;
238  }
239  GrTitle = l("Batches");
240 
241  break;
242  }
243  case 2: {
244  sel = GraphList->GetFirstSelected();
245  for(b = 0; b < Batches.GetCount(); b++) {
246  if(Groups.GetName(sel) == Batches.GetGroup(b)) {
247  TempNames.push_back(Batches.GetName(b));
248  TempValues1.push_back(Batches.PercentFertile(b));
249  TempValues2.push_back(Batches.PercentOfFertile(b));
250  TempValues3.push_back(Batches.PercentOfTotal(b));
251  TempValues4.push_back(Batches.PercentOfTotalUnhatched(b));
252  }
253  }
254  if(TempNames.size() < 2) {
255  wxMessageDialog dialog(this,
256  l("This Group Only Has One Batch!\nAdd Some More To Compare."));
257  dialog.ShowModal();
258  return;
259  }
260  GrTitle = l("Batches For Group: ") + Groups.GetName(sel);
261  break;
262 
263  }
264  case 3: {
265  for(b = 0; b < Groups.GetCount(); b++) {
266  if(!GraphList->IsSelected(b))
267  continue;
268  else {
269  TempNames.push_back(Groups.GetName(b));
270 
271  z = GroupAverage(b);
272  TempValues1.push_back(z[0]);
273  TempValues2.push_back(z[1]);
274  TempValues3.push_back(z[2]);
275  TempValues4.push_back(z[3]);
276  }
277  }
278  if(TempNames.size() < 2) {
279  wxMessageDialog dialog(this,
280  l("You'll Need To Select More Than 1 Group\nFor This To Work.\n\nHold CTRL or Cmd + Click\n To Select Multiple Items"));
281  dialog.ShowModal();
282  return;
283  }
284  GrTitle = l("Group Averages");
285  break;
286  }
287  }
288  int DataCount = TempNames.size();
289 
290  wxString names[DataCount];
291  double values1[DataCount];
292  double values2[DataCount];
293  double values3[DataCount];
294  double values4[DataCount];
295 
296  bool oneFound = false;
297 
298  for(b = 0; b < DataCount; b++) {
299  names[b] = TempNames[b];
300 
301  if(BlueCheckBox->IsChecked()) {
302  values1[b] = TempValues1[b];
303  oneFound = true;
304  }
305  else
306  values1[b] = 0;
307 
308  if(YellowCheckBox->IsChecked()) {
309  values2[b] = TempValues2[b];
310  oneFound = true;
311  }
312  else
313  values2[b] = 0;
314 
315  if(RedCheckBox->IsChecked()) {
316  values3[b] = TempValues3[b];
317  oneFound = true;
318  }
319  else
320  values3[b] = 0;
321 
322  if(GreenCheckBox->IsChecked()) {
323  values4[b] = TempValues4[b];
324  oneFound = true;
325  }
326  else
327  values4[b] = 0;
328  }
329 
330  //make sure there is a checkbox toggled
331  if(oneFound == false) {
332  wxMessageDialog dialog(this,
333  l("You'll Need At Least One Check Box Checked!"));
334  dialog.ShowModal();
335  return;
336  }
337 
338  // Create dataset
339  CategorySimpleDataset *dataset = new CategorySimpleDataset(names, WXSIZEOF(names));
340 
341  // add two series to it
342 
343  dataset->AddSerie(l("% Were Fertile"), values1, WXSIZEOF(values1));
344  dataset->AddSerie(l("% Of Fertile Hatched"), values2, WXSIZEOF(values2));
345  dataset->AddSerie(l("% Of Total Hatched"), values3, WXSIZEOF(values3));
346  dataset->AddSerie(l("% Of Total UnHatched"), values4, WXSIZEOF(values4));
347  // create layered bar type with width=40 and base=0
348  BarType *barType = new LayeredBarType(40, 0);
349 
350  // Set bar renderer for it, with layered bar type
351  BarRenderer *renderer = new BarRenderer(barType);
352 
353  // Some eye-candy: gradient bars
354  renderer->SetBarDraw(0, new GradientAreaDraw(*wxBLUE, wxColor(0,171,255),
355  wxColor(0,171,255).ChangeLightness(150), wxNORTH));
356  renderer->SetBarDraw(1, new GradientAreaDraw(*wxYELLOW, wxColor(215,215,35),
357  wxColor(215,215,35).ChangeLightness(150), wxNORTH));
358 
359  renderer->SetBarDraw(2, new GradientAreaDraw(*wxRED, wxColor(255,0,0),
360  wxColor(255,0,0).ChangeLightness(150), wxNORTH));
361 
362  renderer->SetBarDraw(3, new GradientAreaDraw(*wxGREEN, wxColor(0,128,0),
363  wxColor(0,128,0).ChangeLightness(150), wxNORTH));
364 
365  // assign renderer to dataset
366  dataset->SetRenderer(renderer);
367 
368  // Create bar plot
369  BarPlot *plot = new BarPlot();
370 
371  // Add left category axis
372  NumberAxis *leftAxis = new NumberAxis(AXIS_LEFT);
373  leftAxis->SetMargins(0, 0);
374  plot->AddAxis(leftAxis);
375 
376  // Add bottom number axis
377  CategoryAxis *bottomAxis = new CategoryAxis(AXIS_BOTTOM);
378  bottomAxis->SetMargins(20, 20);
379  plot->AddAxis(bottomAxis);
380 
381  // Add dataset to plot
382  plot->AddDataset(dataset);
383 
384  // Link first dataset with first horizontal axis
385  plot->LinkDataHorizontalAxis(0, 0);
386 
387  // Link first dataset with first vertical axis
388  plot->LinkDataVerticalAxis(0, 0);
389 
390  //test
391  Legend* legend = new Legend(wxCENTER, wxRIGHT, new FillAreaDraw(*wxTRANSPARENT_PEN, *wxTRANSPARENT_BRUSH));
392  plot->SetLegend(legend);
393 
394  // and finally construct and return chart
395  GraphChartPanel->SetChart(new Chart(plot, GrTitle));
396 
397  GraphSaveButton->Enable(true);
398 }
399 
401 void GraphDialog::OnGraphSave(wxCommandEvent&)
402 {
403  string path;
404  string ext1, ext2 = "png";
405  Chart *chart = GraphChartPanel->GetChart();
406 
407  if (chart != NULL) {
408  wxFileDialog dlg(this, l("Choose file..."), wxEmptyString, wxEmptyString,
409  wxString(wxT("PNG files (*.png)|*.png")), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
410 
411  if (dlg.ShowModal() != wxID_OK)
412  return ;
413 
414  path = dlg.GetPath();
415 
416  wxFileName fname(path.c_str());
417  ext1 = fname.GetExt();
418 
419  if( ext1 != ext2)
420  path = path + ".png";
421 
422 
423  wxBitmap bitmap =GraphChartPanel->CopyBackbuffer();
424  bitmap.ConvertToImage().SaveFile(path.c_str(), wxBITMAP_TYPE_PNG);
425  }
426  else {
427  wxMessageDialog dialog(this,
428  l("Error! No Graph is Being Shown!"));
429  dialog.ShowModal();
430  return;
431  }
432 }
433 
435 void GraphDialog::OnGraphClose(wxCommandEvent&){EndModal(0);}
436 
439 {
440  int b;
441 
442  GraphList->ClearAll();//Clear the previous list items
443  GraphList->InsertColumn(0,l("Step 2 - Select Item(s)" ), wxLIST_FORMAT_LEFT, 400);
444 
445  switch(a) {
446  case 1: {
447  for(b = 0; b < Batches.GetCount(); b++)
448  GraphList->InsertItem(b, wxString::Format(wxT("%s"), Batches.GetName(b).c_str()));
449  break;
450  }
451  case 2: {
452  for(b = 0; b < Groups.GetCount(); b++)
453  GraphList->InsertItem(b, wxString::Format(wxT("%s"), Groups.GetName(b).c_str()));
454  break;
455  }
456  case 3: {
457  for(b = 0; b < Groups.GetCount(); b++)
458  GraphList->InsertItem(b, wxString::Format(wxT("%s"), Groups.GetName(b).c_str()));
459  break;
460  }
461  }
462 }
463 
465 vector<int> GraphDialog::GroupAverage(int a)
466 {
467  vector<int> Out;
468  int c, d = 0;
469 
470  int fertileTotal = 0;
471  int hatchOfTotal = 0;
472  int hatchOfFer = 0;
473  int unHatchOfT = 0;
474 
475  for(c = 0; c < Batches.GetCount(); c++) {
476  if(Groups.GetName(a) == Batches.GetGroup(c)) {
477  fertileTotal += Batches.GetFertile(c);
478  hatchOfTotal += Batches.PercentOfTotal(c);
479  hatchOfFer += Batches.PercentOfFertile(c);
480  unHatchOfT += Batches.PercentOfTotalUnhatched(c);
481  d++;
482  }
483  }
484 
485  Out.push_back(fertileTotal / d);
486  Out.push_back(hatchOfTotal / d);
487  Out.push_back(hatchOfFer / d);
488  Out.push_back(unHatchOfT / d);
489 
490  return Out;
491 }
492 #endif //__NO_GRAPH__
HK_KeyValue Settings
Definition: Declare.h:36
HK_Batches Batches
Definition: Declare.h:44
std::string l(std::string Text)
A Helper Function For HK_Language.
Definition: Declare.h:52
HK_Storage Groups
Definition: Declare.h:41
A Dialog For Viewing Graph Stats.
Definition: GraphDialog.cpp:29
wxCheckBox * RedCheckBox
Definition: GraphDialog.cpp:41
void OnModeChoice(wxCommandEvent &)
wxChoice * GraphModeChoice
Definition: GraphDialog.cpp:34
wxListView * GraphList
Definition: GraphDialog.cpp:33
wxCheckBox * GreenCheckBox
Definition: GraphDialog.cpp:42
wxButton * GraphSaveButton
Definition: GraphDialog.cpp:37
void OnGraphRefresh(wxCommandEvent &)
Redraws The Graph When The Refresh Button Is Clicked.
void OnListSelect(wxListEvent &evt)
wxButton * GraphRefreshButton
Definition: GraphDialog.cpp:36
vector< int > GroupAverage(int a)
Returns An Array Of Group Averages.
wxButton * GraphCloseButton
Definition: GraphDialog.cpp:38
GraphDialog(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(1000, 650), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
Constructor.
Definition: GraphDialog.cpp:58
void OnGraphClose(wxCommandEvent &)
Closes Dialog.
wxStaticText * GraphModeStat
Definition: GraphDialog.cpp:35
wxChartPanel * GraphChartPanel
Definition: GraphDialog.cpp:31
void FillGraphList(int a)
Populates GraphList.
void OnGraphSave(wxCommandEvent &)
Saves The Graph To A PNG File.
wxCheckBox * YellowCheckBox
Definition: GraphDialog.cpp:40
wxStaticLine * m_staticline1
Definition: GraphDialog.cpp:32
wxCheckBox * BlueCheckBox
Definition: GraphDialog.cpp:39
int PercentOfTotalUnhatched(int Select)
Returns The Percentage Of Total Eggs That Didn't Hatch For The Selected Batch.
Definition: HK_Batches.cpp:129
int GetFertile(int Select)
Returns Number Of Fertile Eggs For Selected Batch.
Definition: HK_Batches.cpp:77
int PercentOfTotal(int Select)
Returns The Percentage Of Total Eggs That Hatched For The Selected Batch.
Definition: HK_Batches.cpp:117
string GetGroup(int Select)
Returns Batch Group For Selected Batch.
Definition: HK_Batches.cpp:69
int PercentFertile(int Select)
Returns The Percentage Of Egg That Were Fertile For The Selected Batch.
Definition: HK_Batches.cpp:105
int GetCount()
Returns The Number Of Batches Stored.
Definition: HK_Batches.cpp:191
int PercentOfFertile(int Select)
Returns The Percentage Of Fertile Eggs That Hatched For The Selected Batch.
Definition: HK_Batches.cpp:111
string GetName(int Select)
Returns Batch Name For Selected Batch.
Definition: HK_Batches.cpp:63
vector< int > GetIntArray(int Selection)
Returns The Selected Value As An Array Of Integers.
int GetCount()
Returns The Number Of Elements Stored.
Definition: HK_Storage.cpp:101
string GetName(int Select)
Returns The Selected Stored Name.
Definition: HK_Storage.cpp:61