]> git.saurik.com Git - wxWidgets.git/blob - src/generic/plot.cpp
dbgrid.cpp dbtree.cpp
[wxWidgets.git] / src / generic / plot.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: plot.cpp
3 // Purpose: wxPlotWindow
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 12/01/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "plot.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/object.h"
25 #include "wx/font.h"
26 #include "wx/colour.h"
27 #include "wx/settings.h"
28 #include "wx/sizer.h"
29 #include "wx/log.h"
30 #include "wx/intl.h"
31 #include "wx/dcclient.h"
32 #endif
33
34 #include "wx/generic/plot.h"
35
36 #include <math.h>
37
38 //-----------------------------------------------------------------------------
39 // wxPlotCurve
40 //-----------------------------------------------------------------------------
41
42 IMPLEMENT_ABSTRACT_CLASS(wxPlotCurve, wxObject)
43
44 wxPlotCurve::wxPlotCurve( int offsetY, double startY, double endY )
45 {
46 m_offsetY = offsetY;
47 m_startY = startY;
48 m_endY = endY;
49 }
50
51 //-----------------------------------------------------------------------------
52 // wxPlotArea
53 //-----------------------------------------------------------------------------
54
55 IMPLEMENT_DYNAMIC_CLASS(wxPlotArea, wxWindow)
56
57 BEGIN_EVENT_TABLE(wxPlotArea, wxWindow)
58 EVT_PAINT( wxPlotArea::OnPaint)
59 EVT_LEFT_DOWN( wxPlotArea::OnMouse)
60 END_EVENT_TABLE()
61
62 wxPlotArea::wxPlotArea( wxPlotWindow *parent )
63 : wxWindow( parent, -1, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, "plotarea" )
64 {
65 m_owner = parent;
66
67 SetBackgroundColour( *wxWHITE );
68 }
69
70 void wxPlotArea::OnMouse( wxMouseEvent &event )
71 {
72 int client_width;
73 int client_height;
74 GetClientSize( &client_width, &client_height);
75 int view_x;
76 int view_y;
77 m_owner->GetViewStart( &view_x, &view_y );
78 view_x *= 10;
79 view_y *= 10;
80
81 wxPoint pos = event.GetPosition();
82 int x = pos.x;
83 int y = pos.y;
84 x += view_x;
85 y += view_y;
86
87 wxNode *node = m_owner->m_curves.First();
88 while (node)
89 {
90 wxPlotCurve *curve = (wxPlotCurve*)node->Data();
91
92 double double_client_height = (double)client_height;
93 double range = curve->GetEndY() - curve->GetStartY();
94 double end = curve->GetEndY();
95 wxCoord offset_y = curve->GetOffsetY();
96
97 double dy = (end - curve->GetY( x )) / range;
98 wxCoord curve_y = (wxCoord)(dy * double_client_height) - offset_y - 1;
99
100 if ((y-curve_y < 4) && (y-curve_y > -4))
101 {
102 m_owner->SetCurrent( curve );
103 return;
104 }
105
106 node = node->Next();
107 }
108 }
109
110 void wxPlotArea::DeleteCurve( wxPlotCurve *curve, int from, int to )
111 {
112 wxClientDC dc(this);
113 m_owner->PrepareDC( dc );
114 dc.SetPen( *wxWHITE_PEN );
115 DrawCurve( &dc, curve, from, to );
116 }
117
118 void wxPlotArea::DrawCurve( wxDC *dc, wxPlotCurve *curve, int from, int to )
119 {
120 int view_x;
121 int view_y;
122 m_owner->GetViewStart( &view_x, &view_y );
123 view_x *= 10;
124
125 if (from == -1)
126 from = view_x;
127
128 int client_width;
129 int client_height;
130 GetClientSize( &client_width, &client_height);
131
132 if (to == -1)
133 to = view_x + client_width;
134
135 int start_x = wxMax( from, curve->GetStartX() );
136 int end_x = wxMin( to, curve->GetEndX() );
137
138 start_x = wxMax( view_x, start_x );
139 end_x = wxMin( view_x + client_width, end_x );
140
141 double double_client_height = (double)client_height;
142 double range = curve->GetEndY() - curve->GetStartY();
143 double end = curve->GetEndY();
144 wxCoord offset_y = curve->GetOffsetY();
145
146 wxCoord y=0,last_y=0;
147 for (int x = start_x; x < end_x; x++)
148 {
149 double dy = (end - curve->GetY( x )) / range;
150 y = (wxCoord)(dy * double_client_height) - offset_y - 1;
151
152 if (x != start_x)
153 dc->DrawLine( x-1, last_y, x, y );
154
155 last_y = y;
156 }
157 }
158
159 void wxPlotArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
160 {
161 int view_x;
162 int view_y;
163 m_owner->GetViewStart( &view_x, &view_y );
164 view_x *= 10;
165 view_y *= 10;
166
167 wxPaintDC dc( this );
168 m_owner->PrepareDC( dc );
169
170 wxRegionIterator upd( GetUpdateRegion() );
171
172 while (upd)
173 {
174 int update_x = upd.GetX();
175 int update_y = upd.GetY();
176 int update_width = upd.GetWidth();
177
178 update_x += view_x;
179 update_y += view_y;
180
181 /*
182 if (m_owner->m_current)
183 {
184 dc.SetPen( *wxLIGHT_GREY_PEN );
185 int base_line = client_height - m_owner->m_current->GetOffsetY();
186 dc.DrawLine( update_x-1, base_line-1, update_x+update_width+2, base_line-1 );
187 }
188 */
189
190 wxNode *node = m_owner->m_curves.First();
191 while (node)
192 {
193 wxPlotCurve *curve = (wxPlotCurve*)node->Data();
194
195 if (curve == m_owner->GetCurrent())
196 dc.SetPen( *wxBLACK_PEN );
197 else
198 dc.SetPen( *wxLIGHT_GREY_PEN );
199
200 DrawCurve( &dc, curve, update_x-1, update_x+update_width+2 );
201
202 node = node->Next();
203 }
204 upd ++;
205 }
206 }
207
208 //-----------------------------------------------------------------------------
209 // wxPlotWindow
210 //-----------------------------------------------------------------------------
211
212 #define ID_ENLARGE_100 1000
213 #define ID_ENLARGE_50 1001
214 #define ID_SHRINK_33 1002
215 #define ID_SHRINK_50 1003
216
217 #define ID_MOVE_UP 1006
218 #define ID_MOVE_DOWN 1007
219
220
221 IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow, wxScrolledWindow)
222
223 BEGIN_EVENT_TABLE(wxPlotWindow, wxScrolledWindow)
224 EVT_PAINT( wxPlotWindow::OnPaint)
225 EVT_BUTTON( ID_MOVE_UP, wxPlotWindow::OnMoveUp)
226 EVT_BUTTON( ID_MOVE_DOWN, wxPlotWindow::OnMoveDown)
227
228 EVT_BUTTON( ID_ENLARGE_100, wxPlotWindow::OnEnlarge100)
229 EVT_BUTTON( ID_ENLARGE_50, wxPlotWindow::OnEnlarge50)
230 EVT_BUTTON( ID_SHRINK_50, wxPlotWindow::OnShrink50)
231 EVT_BUTTON( ID_SHRINK_33, wxPlotWindow::OnShrink33)
232 END_EVENT_TABLE()
233
234 wxPlotWindow::wxPlotWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int flag )
235 : wxScrolledWindow( parent, id, pos, size, flag, "plotcanvas" )
236 {
237 m_area = new wxPlotArea( this );
238
239 wxBoxSizer *mainsizer = new wxBoxSizer( wxHORIZONTAL );
240
241 wxBoxSizer *buttonlist = new wxBoxSizer( wxVERTICAL );
242 buttonlist->Add( new wxButton( this, ID_ENLARGE_100, _("+ 100%") ), 0, wxEXPAND|wxALL, 5 );
243 buttonlist->Add( new wxButton( this, ID_ENLARGE_50, _("+ 50%") ), 0, wxEXPAND|wxALL, 5 );
244 buttonlist->Add( new wxButton( this, ID_SHRINK_33, _("- 33%") ), 0, wxEXPAND|wxALL, 5 );
245 buttonlist->Add( new wxButton( this, ID_SHRINK_50, _("- 50%") ), 0, wxEXPAND|wxALL, 5 );
246 buttonlist->Add( 20,20, 0 );
247 buttonlist->Add( new wxButton( this, ID_MOVE_UP, _("Up") ), 0, wxEXPAND|wxALL, 5 );
248 buttonlist->Add( new wxButton( this, ID_MOVE_DOWN, _("Down") ), 0, wxEXPAND|wxALL, 5 );
249 buttonlist->Add( 20,20, 1 );
250
251 mainsizer->Add( buttonlist, 0, wxEXPAND );
252
253 mainsizer->Add( m_area, 1, wxEXPAND|wxLEFT, 50 );
254
255 SetAutoLayout( TRUE );
256 SetSizer( mainsizer );
257
258 SetTargetWindow( m_area );
259
260 SetBackgroundColour( *wxWHITE );
261
262 m_current = (wxPlotCurve*) NULL;
263 }
264
265 wxPlotWindow::~wxPlotWindow()
266 {
267 }
268
269 void wxPlotWindow::Add( wxPlotCurve *curve )
270 {
271 m_curves.Append( curve );
272 if (!m_current) m_current = curve;
273 }
274
275 size_t wxPlotWindow::GetCount()
276 {
277 return m_curves.GetCount();
278 }
279
280 wxPlotCurve *wxPlotWindow::GetAt( size_t n )
281 {
282 wxNode *node = m_curves.Nth( n );
283 if (!node)
284 return (wxPlotCurve*) NULL;
285
286 return (wxPlotCurve*) node->Data();
287 }
288
289 void wxPlotWindow::SetCurrent( wxPlotCurve* current )
290 {
291 m_current = current;
292 m_area->Refresh( FALSE );
293
294 RedrawYAxis();
295 }
296
297 wxPlotCurve *wxPlotWindow::GetCurrent()
298 {
299 return m_current;
300 }
301
302 void wxPlotWindow::Move( wxPlotCurve* curve, int pixels_up )
303 {
304 m_area->DeleteCurve( curve );
305
306 curve->SetOffsetY( curve->GetOffsetY() + pixels_up );
307
308 m_area->Refresh( FALSE );
309
310 RedrawYAxis();
311 }
312
313 void wxPlotWindow::OnMoveUp( wxCommandEvent& WXUNUSED(event) )
314 {
315 if (!m_current) return;
316
317 Move( m_current, 25 );
318 }
319
320 void wxPlotWindow::OnMoveDown( wxCommandEvent& WXUNUSED(event) )
321 {
322 if (!m_current) return;
323
324 Move( m_current, -25 );
325 }
326
327 void wxPlotWindow::Enlarge( wxPlotCurve *curve, double factor )
328 {
329 m_area->DeleteCurve( curve );
330
331 double range = curve->GetEndY() - curve->GetStartY();
332 double new_range = range * factor;
333 double middle = curve->GetEndY() - range/2;
334 curve->SetStartY( middle - new_range / 2 );
335 curve->SetEndY( middle + new_range / 2 );
336
337 m_area->Refresh( FALSE );
338
339 RedrawYAxis();
340 }
341
342 void wxPlotWindow::OnEnlarge100( wxCommandEvent& WXUNUSED(event) )
343 {
344 if (!m_current) return;
345
346 Enlarge( m_current, 2.0 );
347 }
348
349 void wxPlotWindow::OnEnlarge50( wxCommandEvent& WXUNUSED(event) )
350 {
351 if (!m_current) return;
352
353 Enlarge( m_current, 1.5 );
354 }
355
356 void wxPlotWindow::OnShrink50( wxCommandEvent& WXUNUSED(event) )
357 {
358 if (!m_current) return;
359
360 Enlarge( m_current, 0.5 );
361 }
362
363 void wxPlotWindow::OnShrink33( wxCommandEvent& WXUNUSED(event) )
364 {
365 if (!m_current) return;
366
367 Enlarge( m_current, 0.6666666 );
368 }
369
370 void wxPlotWindow::RedrawYAxis()
371 {
372 int client_width;
373 int client_height;
374 GetClientSize( &client_width, &client_height);
375
376 wxPoint pos( m_area->GetPosition() );
377
378 wxRect rect(pos.x-45,0,45,client_height);
379 Refresh(TRUE,&rect);
380 }
381
382 void wxPlotWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
383 {
384 wxPaintDC dc( this );
385
386 if (!m_current) return;
387
388 int client_width;
389 int client_height;
390 GetClientSize( &client_width, &client_height);
391
392 dc.SetPen( *wxBLACK_PEN );
393
394 wxPoint pos( m_area->GetPosition() );
395
396 double range = m_current->GetEndY() - m_current->GetStartY();
397 double offset = ((double) m_current->GetOffsetY() / (double)client_height ) * range;
398 double start = m_current->GetStartY() - offset;
399 double end = m_current->GetEndY() - offset;
400 int int_log_range = (int)floor( log10( range ) );
401 double step = 1.0;
402 if (int_log_range > 0)
403 {
404 for (int i = 0; i < int_log_range; i++)
405 step *= 10;
406 }
407 if (int_log_range < 0)
408 {
409 for (int i = 0; i < -int_log_range; i++)
410 step /= 10;
411 }
412 double lower = ceil(start / step) * step;
413 double upper = floor(end / step) * step;
414
415 // if too few values, shrink size
416 int steps = (int)ceil((upper-lower)/step);
417 if (steps < 4)
418 {
419 step /= 2;
420 if (lower-step > start) lower -= step;
421 if (upper+step < end) upper += step;
422 }
423
424 // if still too few, again
425 steps = (int)ceil((upper-lower)/step);
426 if (steps < 4)
427 {
428 step /= 2;
429 if (lower-step > start) lower -= step;
430 if (upper+step < end) upper += step;
431 }
432
433 double current = lower;
434 while (current < upper+(step/2))
435 {
436 int y = (int)((m_current->GetEndY()-current) / range * (double)client_height) - 1;
437 y -= m_current->GetOffsetY();
438 if ((y > 10) && (y < client_height-7))
439 {
440 dc.DrawLine( pos.x-15, y, pos.x-7, y );
441 wxString label;
442 label.Printf( wxT("%.1f"), current );
443 dc.DrawText( label, pos.x-45, y-7 );
444 }
445
446 current += step;
447 }
448
449 dc.DrawLine( pos.x-15, 6, pos.x-15, client_height-5 );
450 dc.DrawLine( pos.x-19, 8, pos.x-15, 2 );
451 dc.DrawLine( pos.x-10, 9, pos.x-15, 2 );
452
453 }
454