1
/////////////////////////////////////////////////////////////////////////////
3
// Purpose: wxChart sample
7
// Copyright: (C) 2006, Paolo Gava
8
// RCS-ID: $Id: wxchart.cpp 3904 2007-04-28 19:48:09Z byo $
9
// Licence: wxWindows licence
10
/////////////////////////////////////////////////////////////////////////////
13
// ----------------------------------------------------------------------------
15
// ----------------------------------------------------------------------------
16
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17
#pragma implementation "wxchart.cpp"
18
#pragma interface "wxchart.cpp"
21
// For compilers that support precompilation, includes "wx.h".
22
#include <wx/wxprec.h>
32
#include "wx/points.h"
33
#include "wx/barchartpoints.h"
34
#include "wx/bar3dchartpoints.h"
35
#include "wx/chartctrl.h"
36
#include "wx/chartcolors.h"
37
#include "wx/piechartpoints.h"
38
#include "wx/pie3dchartpoints.h"
41
// ----------------------------------------------------------------------------
43
// ----------------------------------------------------------------------------
45
class MyPanel : public wxPanel
48
MyPanel(wxFrame *frame);
53
wxChartCtrl *m_pChartCtrl;
55
// Helper routines for creating Panels
56
//------------------------------------
57
void CreatePanelLeft1(wxBoxSizer *sizer);
58
void CreatePanelLeft2(wxBoxSizer *sizer);
59
void CreatePanelRight1(wxBoxSizer *sizer);
60
void CreatePanelRight2(wxBoxSizer *sizer);
64
// Define a new application type, each program should derive a class from wxApp
65
class MyApp : public wxApp
68
// override base class virtuals
69
// ----------------------------
71
// this one is called on application startup and is a good place for the app
72
// initialization (doing it here and not in the ctor allows to have an error
73
// return: if OnInit() returns false, the application terminates)
74
virtual bool OnInit();
78
// Define a new frame type: this is going to be our main frame
79
class MyFrame : public wxFrame
83
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
85
// event handlers (these functions should _not_ be virtual)
86
void OnQuit(wxCommandEvent& event);
87
void OnAbout(wxCommandEvent& event);
88
//void OnResize(wxCommandEvent& event);
89
//void OnWrite(wxCommandEvent& event);
94
// any class wishing to process wxWindows events must use this macro
98
// ----------------------------------------------------------------------------
100
// ----------------------------------------------------------------------------
102
// IDs for the controls and the menu commands
114
// ----------------------------------------------------------------------------
115
// event tables and other macros for wxWindows
116
// ----------------------------------------------------------------------------
118
// the event tables connect the wxWindows events with the functions (event
119
// handlers) which process them. It can be also done at run-time, but for the
120
// simple menu events like this the static method is much simpler.
121
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
122
EVT_MENU(File_Quit, MyFrame::OnQuit)
123
EVT_MENU(File_About, MyFrame::OnAbout)
124
//EVT_MENU(Options_Resize, MyFrame::OnResize)
125
//EVT_MENU(Options_Write, MyFrame::OnWrite)
128
// Create a new application object: this macro will allow wxWindows to create
129
// the application object during program execution (it's better than using a
130
// static object for many reasons) and also declares the accessor function
131
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
135
// ============================================================================
137
// ============================================================================
139
// ----------------------------------------------------------------------------
140
// the application class
141
// ----------------------------------------------------------------------------
143
// `Main program' equivalent: the program execution "starts" here
147
// Create the main application window
148
MyFrame *frame = new MyFrame(_T("wxChart sample"),
149
wxPoint(50, 50), wxSize(500, 400));
153
// success: wxApp::OnRun() will be called which will enter the main message
154
// loop and the application will run. If we returned FALSE here, the
155
// application would exit immediately.
159
// ----------------------------------------------------------------------------
161
// ----------------------------------------------------------------------------
164
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
165
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
168
wxMenu *menuFile = new wxMenu;
170
menuFile->Append(File_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
171
menuFile->AppendSeparator();
172
menuFile->Append(File_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
174
//wxMenu *menuOptions = new wxMenu;
175
//menuOptions->Append( Options_Resize, _T("&Resize"),
176
// _T("Fit chart to actual window size"));
177
//menuOptions->Append( Options_Write, _T("&Write"),
178
// _T("write chart to file"));
181
// now append the freshly created menu to the menu bar...
182
wxMenuBar *menuBar = new wxMenuBar;
183
menuBar->Append(menuFile, _T("&File"));
184
//menuBar->Append(menuOptions, _T("&Options"));
186
// ... and attach this menu bar to the frame
189
m_panel = new MyPanel(this);
193
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
195
// TRUE is to force the frame to close
199
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
201
wxString msg = wxString::Format(
202
wxT("Chart for wxWidgets. \n 2006 Paolo Gava. Ver: %d.%d.%d"),
203
wxCHART_MAJOR, wxCHART_MINOR, wxCHART_RELEASE );
205
wxMessageBox( msg, _T("Chart"), wxOK | wxICON_INFORMATION, this );
208
//void MyFrame::OnResize(wxCommandEvent& WXUNUSED(event))
210
// m_panel->ResizeChart();
213
//void MyFrame::OnWrite(wxCommandEvent& WXUNUSED(event))
215
// m_panel->WriteChart();
218
// ----------------------------------------------------------------------------
220
// ----------------------------------------------------------------------------
221
MyPanel::MyPanel(wxFrame *frame)
225
wxBoxSizer *VerSizer = new wxBoxSizer( wxVERTICAL );
226
wxBoxSizer *Hor1Sizer = new wxBoxSizer( wxHORIZONTAL );
227
wxBoxSizer *Hor2Sizer = new wxBoxSizer( wxHORIZONTAL );
229
VerSizer->Add( Hor1Sizer, 1, wxEXPAND );
230
VerSizer->Add( Hor2Sizer, 1, wxEXPAND );
232
wxBoxSizer *Ver1Sizer = new wxBoxSizer( wxVERTICAL );
233
wxBoxSizer *Ver2Sizer = new wxBoxSizer( wxVERTICAL );
234
wxBoxSizer *Ver3Sizer = new wxBoxSizer( wxVERTICAL );
235
wxBoxSizer *Ver4Sizer = new wxBoxSizer( wxVERTICAL );
237
Hor1Sizer->Add( Ver1Sizer, 1, wxEXPAND );
238
Hor1Sizer->Add( Ver2Sizer, 1, wxEXPAND );
239
Hor2Sizer->Add( Ver3Sizer, 1, wxEXPAND );
240
Hor2Sizer->Add( Ver4Sizer, 1, wxEXPAND );
242
// Create Panel Left Row 1
243
CreatePanelLeft1( Ver1Sizer );
245
// Create Panel Right Row 1
246
CreatePanelRight1(Ver2Sizer);
248
// Create Panel Left Row2
249
CreatePanelLeft2( Ver3Sizer );
251
// Create Panel right Row2
252
CreatePanelRight2(Ver4Sizer);
254
SetAutoLayout( TRUE );
255
SetSizer( VerSizer );
256
VerSizer->Fit( this );
261
//+++-S-cf-------------------------------------------------------------------
262
// NAME: CreatePanelLeft1()
263
// DESC: Create left Panel first row
267
//----------------------------------------------------------------------E-+++
268
void MyPanel::CreatePanelLeft1(wxBoxSizer *sizer)
270
wxChartCtrl *chartLeft1;
272
// Create Panel Left Row 1
273
wxChartPoints *bcpLeft1, *bcpLeft2, *bcpLeft3, *bcpLeft4;
275
bcpLeft1 = wxBar3DChartPoints::CreateWxBar3DChartPoints(
278
bcpLeft2 = wxBar3DChartPoints::CreateWxBar3DChartPoints(
281
bcpLeft3 = wxBar3DChartPoints::CreateWxBar3DChartPoints(
284
bcpLeft4 = wxBar3DChartPoints::CreateWxBar3DChartPoints(
287
bcpLeft1->Add( wxT("val3"), 2, 3 );
288
bcpLeft1->Add( wxT("val1"), 5, 1 );
289
bcpLeft1->Add( wxT("val4"), 8, 4);
290
bcpLeft1->Add( wxT("val2"), 15, 2 );
291
bcpLeft1->Add( wxT("val5"), 20, 5 );
292
bcpLeft1->SetDisplayTag( XVALUE );
294
bcpLeft2->Add( wxT("val: 4"), 2, 4 );
295
bcpLeft2->Add( wxT("val: 2"), 5, 2 );
296
bcpLeft2->Add( wxT("val: 2"), 8, 2);
297
bcpLeft2->Add( wxT("val: 6"), 15, 6 );
298
bcpLeft2->Add( wxT("val: 2"), 20, 2 );
299
bcpLeft2->SetDisplayTag( NAME );
301
bcpLeft3->Add( wxT("val3"), 2, 1 );
302
bcpLeft3->Add( wxT("val1"), 5, 3 );
303
bcpLeft3->Add( wxT("val4"), 8, 4);
304
bcpLeft3->Add( wxT("val2"), 15, 5 );
305
bcpLeft3->Add( wxT("val5"), 20, 5 );
306
bcpLeft3->SetDisplayTag( XVALUE );
308
bcpLeft4->Add( wxT("val3"), 2, 3 );
309
bcpLeft4->Add( wxT("val6"), 5, 6 );
310
bcpLeft4->Add( wxT("val2"), 8, 2);
311
bcpLeft4->Add( wxT("val2"), 15, 2 );
312
bcpLeft4->Add( wxT("val4"), 20, 4 );
313
bcpLeft4->SetDisplayTag( YVALUE );
315
wxStaticText *lblTitle1 = new wxStaticText(this,
316
wxID_ANY, wxT("Bar Charts"));
317
chartLeft1 = new wxChartCtrl( this, -1, DEFAULT_STYLE,
318
wxDefaultPosition, wxSize(200,200),
321
chartLeft1->Add( bcpLeft1 );
322
chartLeft1->Add( bcpLeft2 );
323
chartLeft1->Add( bcpLeft3 );
324
chartLeft1->Add( bcpLeft4 );
326
sizer->Add( lblTitle1, 0, wxEXPAND );
327
sizer->Add( chartLeft1, 1, wxEXPAND );
330
//+++-S-cf-------------------------------------------------------------------
331
// NAME: CreatePanelLeft2()
332
// DESC: Create left Panel second row
336
//----------------------------------------------------------------------E-+++
337
void MyPanel::CreatePanelLeft2(wxBoxSizer *sizer)
339
wxChartCtrl *chartLeft2;
341
// Create Panel Left Row 2
342
wxChartPoints *bcpLeft1;
344
bcpLeft1 = wxBar3DChartPoints::CreateWxBar3DChartPoints(
345
wxT("Ita"), wxCHART_NOCOLOR, true );
347
bcpLeft1->Add( wxT("val3"), 2, 3 );
348
bcpLeft1->Add( wxT("val1"), 5, 1 );
349
bcpLeft1->Add( wxT("val4"), 8, 4);
350
bcpLeft1->Add( wxT("val2"), 15, 2 );
351
bcpLeft1->Add( wxT("val5"), 20, 5 );
352
bcpLeft1->SetDisplayTag( YVALUE );
354
wxStaticText *lblTitle3 = new wxStaticText(this,
355
wxID_ANY, wxT("Bar Charts"));
356
chartLeft2 = new wxChartCtrl( this, -1,
357
(STYLE)(USE_AXIS_X | USE_ZOOM_BUT),
358
wxDefaultPosition, wxSize(200,200),
361
chartLeft2->Add( bcpLeft1 );
363
sizer->Add( lblTitle3, 0, wxEXPAND );
364
sizer->Add( chartLeft2, 1, wxEXPAND );
367
//+++-S-cf-------------------------------------------------------------------
368
// NAME: CreatePanelRight1()
369
// DESC: Create right Panel first row
373
//----------------------------------------------------------------------E-+++
374
void MyPanel::CreatePanelRight1(wxBoxSizer *sizer)
376
wxChartCtrl *chartRight1;
378
// Create Panel Right Row 1
379
wxChartPoints *pcpRight1;
381
pcpRight1 = wxPie3DChartPoints::CreateWxPie3DChartPoints(
382
wxT("Italy"), wxCHART_NOCOLOR, true );
384
// Pie chart ignore x value
385
pcpRight1->Add( wxT("10 %"), 0, 10, wxCHART_GOLD );
386
pcpRight1->Add( wxT("20 %"), 0, 20, wxCHART_CYAN );
387
pcpRight1->Add( wxT("5 %"), 0, 5, wxCHART_GOLDENROD );
388
pcpRight1->Add( wxT("30 %"), 0, 30, wxCHART_GRAY );
389
pcpRight1->Add( wxT("10 %"), 0, 10, wxCHART_AQUAMARINE );
390
pcpRight1->Add( wxT("15 %"), 0, 15, wxCHART_LYELOW );
391
pcpRight1->Add( wxT("10 %"), 0, 10, wxCHART_SEAGREEN );
392
pcpRight1->SetDisplayTag( NAME );
394
wxStaticText *lblTitle2 = new wxStaticText(this,
395
wxID_ANY, wxT("Pie Chart"));
396
chartRight1 = new wxChartCtrl( this, -1, USE_NONE,
397
wxDefaultPosition, wxSize(200,200),
400
chartRight1->Add( pcpRight1 );
402
sizer->Add( lblTitle2, 0, wxEXPAND );
403
sizer->Add( chartRight1, 1, wxEXPAND );
406
//+++-S-cf-------------------------------------------------------------------
407
// NAME: CreatePanelRight2()
408
// DESC: Create right Panel second row
412
//----------------------------------------------------------------------E-+++
413
void MyPanel::CreatePanelRight2(wxBoxSizer *sizer)
415
wxChartCtrl *chartRight2;
417
wxChartPoints *bcpLeft1, *bcpLeft2, *bcpLeft3, *bcpLeft4, *pcpRight1;
419
bcpLeft1 = wxBarChartPoints::CreateWxBarChartPoints(
422
bcpLeft2 = wxBarChartPoints::CreateWxBarChartPoints(
425
bcpLeft3 = wxBarChartPoints::CreateWxBarChartPoints(
428
bcpLeft4 = wxBarChartPoints::CreateWxBarChartPoints(
431
pcpRight1 = wxPieChartPoints::CreateWxPieChartPoints(
434
bcpLeft1->Add( wxT("val3"), 2, 3 );
435
bcpLeft1->Add( wxT("val1"), 5, 1 );
436
bcpLeft1->Add( wxT("val4"), 8, 4);
437
bcpLeft1->Add( wxT("val2"), 15, 2 );
438
bcpLeft1->Add( wxT("val5"), 20, 5 );
439
bcpLeft1->SetDisplayTag( XVALUE );
441
bcpLeft2->Add( wxT("val: 4"), 2, 4 );
442
bcpLeft2->Add( wxT("val: 2"), 5, 2 );
443
bcpLeft2->Add( wxT("val: 2"), 8, 2);
444
bcpLeft2->Add( wxT("val: 6"), 15, 6 );
445
bcpLeft2->Add( wxT("val: 2"), 20, 2 );
446
bcpLeft2->SetDisplayTag( NAME );
448
bcpLeft3->Add( wxT("val3"), 2, 1 );
449
bcpLeft3->Add( wxT("val1"), 5, 3 );
450
bcpLeft3->Add( wxT("val4"), 8, 4);
451
bcpLeft3->Add( wxT("val2"), 15, 5 );
452
bcpLeft3->Add( wxT("val5"), 20, 5 );
453
bcpLeft3->SetDisplayTag( XVALUE );
455
bcpLeft4->Add( wxT("val3"), 2, 3 );
456
bcpLeft4->Add( wxT("val6"), 5, 6 );
457
bcpLeft4->Add( wxT("val2"), 8, 2);
458
bcpLeft4->Add( wxT("val2"), 15, 2 );
459
bcpLeft4->Add( wxT("val4"), 20, 4 );
460
bcpLeft4->SetDisplayTag( YVALUE );
462
pcpRight1->Add( wxT("val1"), 5, 10 );
463
pcpRight1->Add( wxT("val2"), 15, 20 );
464
pcpRight1->Add( wxT("val3"), 2, 5 );
465
pcpRight1->Add( wxT("val4"), 8, 30);
466
pcpRight1->Add( wxT("val5"), 20, 10 );
467
pcpRight1->Add( wxT("val6"), 20, 15 );
468
pcpRight1->Add( wxT("val7"), 20, 10 );
469
pcpRight1->SetDisplayTag( NAME );
471
wxStaticText *lblTitle4 = new wxStaticText(this,
472
wxID_ANY, wxT("Bar/Pie Charts"));
473
chartRight2 = new wxChartCtrl( this, -1, DEFAULT_STYLE,
474
wxDefaultPosition, wxSize(200,200),
477
chartRight2->Add( bcpLeft1 );
478
chartRight2->Add( bcpLeft2 );
479
chartRight2->Add( bcpLeft3 );
480
chartRight2->Add( bcpLeft4 );
481
chartRight2->Add( pcpRight1 );
483
sizer->Add( lblTitle4, 0, wxEXPAND );
484
sizer->Add( chartRight2, 1, wxEXPAND );
487
//+++-S-cf-------------------------------------------------------------------
488
// NAME: ResizeChart()
489
// DESC: resize chart
492
//----------------------------------------------------------------------E-+++
493
void MyPanel::ResizeChart()
495
m_pChartCtrl->Resize();
498
//+++-S-cf-------------------------------------------------------------------
499
// NAME: WriteChart()
500
// DESC: Write chart to file
503
//----------------------------------------------------------------------E-+++
504
void MyPanel::WriteChart()
506
m_pChartCtrl->WriteToFile( wxT("mem.bmp") );