]> git.saurik.com Git - wxWidgets.git/blame - src/generic/plot.cpp
dbgrid.cpp dbtree.cpp
[wxWidgets.git] / src / generic / plot.cpp
CommitLineData
981b2508
RR
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
846e1424
RR
36#include <math.h>
37
981b2508
RR
38//-----------------------------------------------------------------------------
39// wxPlotCurve
40//-----------------------------------------------------------------------------
41
42IMPLEMENT_ABSTRACT_CLASS(wxPlotCurve, wxObject)
43
846e1424 44wxPlotCurve::wxPlotCurve( int offsetY, double startY, double endY )
981b2508
RR
45{
46 m_offsetY = offsetY;
846e1424
RR
47 m_startY = startY;
48 m_endY = endY;
981b2508
RR
49}
50
51//-----------------------------------------------------------------------------
52// wxPlotArea
53//-----------------------------------------------------------------------------
54
55IMPLEMENT_DYNAMIC_CLASS(wxPlotArea, wxWindow)
56
57BEGIN_EVENT_TABLE(wxPlotArea, wxWindow)
58 EVT_PAINT( wxPlotArea::OnPaint)
59 EVT_LEFT_DOWN( wxPlotArea::OnMouse)
60END_EVENT_TABLE()
61
62wxPlotArea::wxPlotArea( wxPlotWindow *parent )
63 : wxWindow( parent, -1, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, "plotarea" )
64{
65 m_owner = parent;
66
67 SetBackgroundColour( *wxWHITE );
68}
69
70void 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;
06b466c7
VZ
80
81 wxPoint pos = event.GetPosition();
82 int x = pos.x;
83 int y = pos.y;
981b2508
RR
84 x += view_x;
85 y += view_y;
06b466c7 86
981b2508
RR
87 wxNode *node = m_owner->m_curves.First();
88 while (node)
89 {
90 wxPlotCurve *curve = (wxPlotCurve*)node->Data();
06b466c7 91
846e1424
RR
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();
06b466c7 96
846e1424
RR
97 double dy = (end - curve->GetY( x )) / range;
98 wxCoord curve_y = (wxCoord)(dy * double_client_height) - offset_y - 1;
06b466c7 99
981b2508
RR
100 if ((y-curve_y < 4) && (y-curve_y > -4))
101 {
102 m_owner->SetCurrent( curve );
103 return;
104 }
06b466c7 105
981b2508
RR
106 node = node->Next();
107 }
108}
109
d3a809c7
RR
110void 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
118void wxPlotArea::DrawCurve( wxDC *dc, wxPlotCurve *curve, int from, int to )
981b2508 119{
d3a809c7
RR
120 int view_x;
121 int view_y;
122 m_owner->GetViewStart( &view_x, &view_y );
123 view_x *= 10;
06b466c7 124
d3a809c7
RR
125 if (from == -1)
126 from = view_x;
127
981b2508
RR
128 int client_width;
129 int client_height;
130 GetClientSize( &client_width, &client_height);
06b466c7 131
d3a809c7
RR
132 if (to == -1)
133 to = view_x + client_width;
06b466c7 134
d3a809c7
RR
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();
06b466c7 145
d3a809c7
RR
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;
06b466c7 151
d3a809c7
RR
152 if (x != start_x)
153 dc->DrawLine( x-1, last_y, x, y );
06b466c7 154
d3a809c7
RR
155 last_y = y;
156 }
157}
158
159void wxPlotArea::OnPaint( wxPaintEvent &WXUNUSED(event) )
160{
981b2508
RR
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() );
06b466c7 171
981b2508
RR
172 while (upd)
173 {
174 int update_x = upd.GetX();
175 int update_y = upd.GetY();
176 int update_width = upd.GetWidth();
06b466c7 177
981b2508
RR
178 update_x += view_x;
179 update_y += view_y;
06b466c7 180
846e1424 181/*
981b2508
RR
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 }
846e1424 188*/
06b466c7 189
981b2508
RR
190 wxNode *node = m_owner->m_curves.First();
191 while (node)
192 {
193 wxPlotCurve *curve = (wxPlotCurve*)node->Data();
06b466c7 194
981b2508
RR
195 if (curve == m_owner->GetCurrent())
196 dc.SetPen( *wxBLACK_PEN );
197 else
198 dc.SetPen( *wxLIGHT_GREY_PEN );
06b466c7 199
d3a809c7 200 DrawCurve( &dc, curve, update_x-1, update_x+update_width+2 );
981b2508 201
981b2508
RR
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
221IMPLEMENT_DYNAMIC_CLASS(wxPlotWindow, wxScrolledWindow)
222
223BEGIN_EVENT_TABLE(wxPlotWindow, wxScrolledWindow)
d3a809c7
RR
224 EVT_PAINT( wxPlotWindow::OnPaint)
225 EVT_BUTTON( ID_MOVE_UP, wxPlotWindow::OnMoveUp)
226 EVT_BUTTON( ID_MOVE_DOWN, wxPlotWindow::OnMoveDown)
06b466c7 227
d3a809c7
RR
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)
981b2508
RR
232END_EVENT_TABLE()
233
234wxPlotWindow::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 );
06b466c7 238
981b2508 239 wxBoxSizer *mainsizer = new wxBoxSizer( wxHORIZONTAL );
06b466c7 240
981b2508
RR
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 );
06b466c7 250
981b2508 251 mainsizer->Add( buttonlist, 0, wxEXPAND );
06b466c7 252
981b2508 253 mainsizer->Add( m_area, 1, wxEXPAND|wxLEFT, 50 );
06b466c7 254
981b2508
RR
255 SetAutoLayout( TRUE );
256 SetSizer( mainsizer );
257
258 SetTargetWindow( m_area );
259
260 SetBackgroundColour( *wxWHITE );
06b466c7 261
981b2508
RR
262 m_current = (wxPlotCurve*) NULL;
263}
264
265wxPlotWindow::~wxPlotWindow()
266{
267}
268
269void wxPlotWindow::Add( wxPlotCurve *curve )
270{
271 m_curves.Append( curve );
272 if (!m_current) m_current = curve;
273}
274
275size_t wxPlotWindow::GetCount()
276{
277 return m_curves.GetCount();
278}
279
280wxPlotCurve *wxPlotWindow::GetAt( size_t n )
281{
282 wxNode *node = m_curves.Nth( n );
283 if (!node)
284 return (wxPlotCurve*) NULL;
06b466c7 285
981b2508
RR
286 return (wxPlotCurve*) node->Data();
287}
288
289void wxPlotWindow::SetCurrent( wxPlotCurve* current )
290{
291 m_current = current;
846e1424 292 m_area->Refresh( FALSE );
06b466c7 293
d3a809c7
RR
294 RedrawYAxis();
295}
296
297wxPlotCurve *wxPlotWindow::GetCurrent()
298{
299 return m_current;
300}
301
302void wxPlotWindow::Move( wxPlotCurve* curve, int pixels_up )
303{
304 m_area->DeleteCurve( curve );
06b466c7 305
d3a809c7 306 curve->SetOffsetY( curve->GetOffsetY() + pixels_up );
06b466c7 307
d3a809c7 308 m_area->Refresh( FALSE );
06b466c7 309
d3a809c7
RR
310 RedrawYAxis();
311}
312
313void wxPlotWindow::OnMoveUp( wxCommandEvent& WXUNUSED(event) )
314{
315 if (!m_current) return;
06b466c7 316
d3a809c7
RR
317 Move( m_current, 25 );
318}
319
320void wxPlotWindow::OnMoveDown( wxCommandEvent& WXUNUSED(event) )
321{
322 if (!m_current) return;
06b466c7 323
d3a809c7
RR
324 Move( m_current, -25 );
325}
326
327void wxPlotWindow::Enlarge( wxPlotCurve *curve, double factor )
328{
329 m_area->DeleteCurve( curve );
06b466c7 330
d3a809c7
RR
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 );
06b466c7 336
d3a809c7 337 m_area->Refresh( FALSE );
06b466c7 338
d3a809c7
RR
339 RedrawYAxis();
340}
341
342void wxPlotWindow::OnEnlarge100( wxCommandEvent& WXUNUSED(event) )
343{
344 if (!m_current) return;
06b466c7 345
d3a809c7
RR
346 Enlarge( m_current, 2.0 );
347}
348
349void wxPlotWindow::OnEnlarge50( wxCommandEvent& WXUNUSED(event) )
350{
351 if (!m_current) return;
06b466c7 352
d3a809c7
RR
353 Enlarge( m_current, 1.5 );
354}
355
356void wxPlotWindow::OnShrink50( wxCommandEvent& WXUNUSED(event) )
357{
358 if (!m_current) return;
06b466c7 359
d3a809c7
RR
360 Enlarge( m_current, 0.5 );
361}
362
363void wxPlotWindow::OnShrink33( wxCommandEvent& WXUNUSED(event) )
364{
365 if (!m_current) return;
06b466c7 366
d3a809c7
RR
367 Enlarge( m_current, 0.6666666 );
368}
369
370void wxPlotWindow::RedrawYAxis()
371{
981b2508
RR
372 int client_width;
373 int client_height;
374 GetClientSize( &client_width, &client_height);
06b466c7 375
d3a809c7 376 wxPoint pos( m_area->GetPosition() );
06b466c7 377
846e1424 378 wxRect rect(pos.x-45,0,45,client_height);
981b2508
RR
379 Refresh(TRUE,&rect);
380}
381
981b2508
RR
382void wxPlotWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
383{
384 wxPaintDC dc( this );
06b466c7 385
981b2508 386 if (!m_current) return;
06b466c7 387
981b2508
RR
388 int client_width;
389 int client_height;
390 GetClientSize( &client_width, &client_height);
391
392 dc.SetPen( *wxBLACK_PEN );
06b466c7 393
981b2508 394 wxPoint pos( m_area->GetPosition() );
06b466c7 395
846e1424
RR
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++)
06b466c7 405 step *= 10;
846e1424
RR
406 }
407 if (int_log_range < 0)
408 {
409 for (int i = 0; i < -int_log_range; i++)
06b466c7 410 step /= 10;
846e1424
RR
411 }
412 double lower = ceil(start / step) * step;
413 double upper = floor(end / step) * step;
06b466c7 414
d3a809c7 415 // if too few values, shrink size
846e1424 416 int steps = (int)ceil((upper-lower)/step);
d3a809c7
RR
417 if (steps < 4)
418 {
419 step /= 2;
420 if (lower-step > start) lower -= step;
421 if (upper+step < end) upper += step;
422 }
06b466c7 423
d3a809c7
RR
424 // if still too few, again
425 steps = (int)ceil((upper-lower)/step);
426 if (steps < 4)
846e1424
RR
427 {
428 step /= 2;
429 if (lower-step > start) lower -= step;
d3a809c7 430 if (upper+step < end) upper += step;
846e1424 431 }
06b466c7 432
846e1424
RR
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;
8ca97ad0 442 label.Printf( wxT("%.1f"), current );
846e1424
RR
443 dc.DrawText( label, pos.x-45, y-7 );
444 }
445
446 current += step;
447 }
06b466c7 448
d3a809c7
RR
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 );
06b466c7 452
981b2508
RR
453}
454