-
Notifications
You must be signed in to change notification settings - Fork 5
/
mathplot.h
1695 lines (1386 loc) · 66.9 KB
/
mathplot.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// Name: mathplot.cpp
// Purpose: Framework for plotting in wxWindows
// Original Author: David Schalig
// Maintainer: Davide Rondini
// Contributors: Jose Luis Blanco, Val Greene
// Created: 21/07/2003
// Last edit: 22/02/2009
// Copyright: (c) David Schalig, Davide Rondini
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef _MP_MATHPLOT_H_
#define _MP_MATHPLOT_H_
/** @file mathplot.h */
/** @mainpage wxMathPlot
wxMathPlot is a framework for mathematical graph plotting in wxWindows.
The framework is designed for convenience and ease of use.
@section screenshots Screenshots
<a href="http://wxmathplot.sourceforge.net/screenshot.shtml">Go to the screenshots page.</a>
@section overview Overview
The heart of wxMathPlot is mpWindow, which is a 2D canvas for plot layers.
mpWindow can be embedded as subwindow in a wxPane, a wxFrame, or any other wxWindow.
mpWindow provides a zoomable and moveable view of the layers. The current view can
be controlled with the mouse, the scrollbars, and a context menu.
Plot layers are implementations of the abstract base class mpLayer. Those can
be function plots, scale rulers, or any other vector data visualisation. wxMathPlot provides two mpLayer implementations for plotting horizontal and vertical rulers: mpScaleX and mpScaleY.
For convenient function plotting a series of classes derived from mpLayer are provided, like mpFX, mpProfile, mpLegend and so on. These base classes already come with plot code, user's own functions can be implemented by overriding just one member for retrieving a function value.
mpWindow has built-in support for mouse-based pan and zoom through intuitive combinations of buttons and the mouse wheel. It also incorporates an optional double buffering mechanism to avoid flicker. Plots can be easily sent to printer evices or exported in bitmap formats like PNG, BMP or JPEG.
@section coding Coding conventions
wxMathPlot sticks to wxWindow's coding conventions. All entities defined by wxMathPlot have the prefix <i>mp</i>.
@section author Author and license
wxMathPlot is published under the terms of the wxWindow license.<br>
The original author is David Schalig <[email protected]>.<br>
From June 2007 the project is maintained by Davide Rondini <[email protected]>.<br>
Authors can be contacted via the wxMathPlot's homepage at
https://sourceforge.net/projects/wxmathplot<br>
Contributors:<br>
Jose Luis Blanco, Val Greene.<br>
*/
//this definition uses windows dll to export function.
//WXDLLIMPEXP_MATHPLOT definition definition changed to WXDLLIMPEXP_MATHPLOT
//mathplot_EXPORTS will be defined by cmake
#ifdef mathplot_EXPORTS
#define WXDLLIMPEXP_MATHPLOT WXEXPORT
#define WXDLLIMPEXP_DATA_MATHPLOT(type) WXEXPORT type
#else // not making DLL
#define WXDLLIMPEXP_MATHPLOT
#define WXDLLIMPEXP_DATA_MATHPLOT(type) type
#endif
#if defined(__GNUG__) && !defined(__APPLE__)
#pragma interface "mathplot.h"
#endif
#include <vector>
// #include <wx/wx.h>
#include <wx/defs.h>
#include <wx/menu.h>
#include <wx/scrolwin.h>
#include <wx/event.h>
#include <wx/dynarray.h>
#include <wx/pen.h>
#include <wx/dcmemory.h>
#include <wx/string.h>
#include <wx/print.h>
#include <wx/image.h>
#include <deque>
// For memory leak debug
#ifdef _WINDOWS
#ifdef _DEBUG
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif // _DEBUG
#endif // _WINDOWS
// Separation for axes when set close to border
#define X_BORDER_SEPARATION 40
#define Y_BORDER_SEPARATION 60
//-----------------------------------------------------------------------------
// classes
//-----------------------------------------------------------------------------
class WXDLLIMPEXP_MATHPLOT mpLayer;
class WXDLLIMPEXP_MATHPLOT mpFX;
class WXDLLIMPEXP_MATHPLOT mpFY;
class WXDLLIMPEXP_MATHPLOT mpFXY;
class WXDLLIMPEXP_MATHPLOT mpFXYVector;
class WXDLLIMPEXP_MATHPLOT mpScaleX;
class WXDLLIMPEXP_MATHPLOT mpScaleY;
class WXDLLIMPEXP_MATHPLOT mpWindow;
class WXDLLIMPEXP_MATHPLOT mpText;
class WXDLLIMPEXP_MATHPLOT mpPrintout;
/** Command IDs used by mpWindow */
enum
{
mpID_FIT = 2000, //!< Fit view to match bounding box of all layers
mpID_ZOOM_IN, //!< Zoom into view at clickposition / window center
mpID_ZOOM_OUT, //!< Zoom out
mpID_CENTER, //!< Center view on click position
mpID_LOCKASPECT, //!< Lock x/y scaling aspect
mpID_HELP_MOUSE //!< Shows information about the mouse commands
};
//-----------------------------------------------------------------------------
// mpLayer
//-----------------------------------------------------------------------------
typedef enum __mp_Layer_Type {
mpLAYER_UNDEF, //!< Layer type undefined
mpLAYER_AXIS, //!< Axis type layer
mpLAYER_PLOT, //!< Plot type layer
mpLAYER_INFO, //!< Info box type layer
mpLAYER_BITMAP //!< Bitmap type layer
} mpLayerType;
/** Plot layer, abstract base class.
Any number of mpLayer implementations can be attached to mpWindow.
Examples for mpLayer implementations are function graphs, or scale rulers.
For convenience mpLayer defines a name, a font (wxFont), a pen (wxPen),
and a continuity property (bool) as class members.
The default values at constructor are the default font, a black pen, and
continuity set to false (draw separate points).
These may or may not be used by implementations.
*/
class WXDLLIMPEXP_MATHPLOT mpLayer : public wxObject
{
public:
mpLayer();
virtual ~mpLayer() {};
/** Check whether this layer has a bounding box.
The default implementation returns \a TRUE. Override and return
FALSE if your mpLayer implementation should be ignored by the calculation
of the global bounding box for all layers in a mpWindow.
@retval TRUE Has bounding box
@retval FALSE Has not bounding box
*/
virtual bool HasBBox() { return TRUE; }
/** Check whether the layer is an info box.
The default implementation returns \a FALSE. It is overrided to \a TRUE for mpInfoLayer
class and its derivative. It is necessary to define mouse actions behaviour over
info boxes.
@return whether the layer is an info boxes
@sa mpInfoLayer::IsInfo */
virtual bool IsInfo() { return false; };
/** Get inclusive left border of bounding box.
@return Value
*/
virtual double GetMinX() { return -1.0; }
/** Get inclusive right border of bounding box.
@return Value
*/
virtual double GetMaxX() { return 1.0; }
/** Get inclusive bottom border of bounding box.
@return Value
*/
virtual double GetMinY() { return -1.0; }
/** Get inclusive top border of bounding box.
@return Value
*/
virtual double GetMaxY() { return 1.0; }
/** Plot given view of layer to the given device context.
An implementation of this function has to transform layer coordinates to
wxDC coordinates based on the view parameters retrievable from the mpWindow
passed in \a w.
Note that the public methods of mpWindow: x2p,y2p and p2x,p2y are already provided
which transform layer coordinates to DC pixel coordinates, and <b>user code should rely
on them</b> for portability and future changes to be applied transparently, instead of
implementing the following formulas manually.
The passed device context \a dc has its coordinate origin set to the top-left corner
of the visible area (the default). The coordinate orientation is as shown in the
following picture:
<pre>
(wxDC origin 0,0)
x-------------> ascending X ----------------+
| |
| |
V ascending Y |
| |
| |
| |
+-------------------------------------------+ <-- right-bottom corner of the mpWindow visible area.
</pre>
Note that Y ascends in downward direction, whereas the usual vertical orientation
for mathematical plots is vice versa. Thus Y-orientation will be swapped usually,
when transforming between wxDC and mpLayer coordinates. This change of coordinates
is taken into account in the methods p2x,p2y,x2p,y2p.
<b> Rules for transformation between mpLayer and wxDC coordinates </b>
@code
dc_X = (layer_X - mpWindow::GetPosX()) * mpWindow::GetScaleX()
dc_Y = (mpWindow::GetPosY() - layer_Y) * mpWindow::GetScaleY() // swapping Y-orientation
layer_X = (dc_X / mpWindow::GetScaleX()) + mpWindow::GetPosX() // scale guaranteed to be not 0
layer_Y = mpWindow::GetPosY() - (dc_Y / mpWindow::GetScaleY()) // swapping Y-orientation
@endcode
@param dc Device context to plot to.
@param w View to plot. The visible area can be retrieved from this object.
@sa mpWindow::p2x,mpWindow::p2y,mpWindow::x2p,mpWindow::y2p
*/
virtual void Plot(wxDC & dc, mpWindow & w) = 0;
/** Get layer name.
@return Name
*/
wxString GetName() const { return m_name; }
/** Get font set for this layer.
@return Font
*/
const wxFont& GetFont() const { return m_font; }
/** Get pen set for this layer.
@return Pen
*/
const wxPen& GetPen() const { return m_pen; }
/** Set the 'continuity' property of the layer (true:draws a continuous line, false:draws separate points).
* @sa GetContinuity
*/
void SetContinuity(bool continuity) {m_continuous = continuity;}
/** Gets the 'continuity' property of the layer.
* @sa SetContinuity
*/
bool GetContinuity() const {return m_continuous;}
/** Shows or hides the text label with the name of the layer (default is visible).
*/
void ShowName(bool show) { m_showName = show; };
/** Set layer name
@param name Name, will be copied to internal class member
*/
void SetName(wxString name) { m_name = name; }
/** Set layer font
@param font Font, will be copied to internal class member
*/
void SetFont(wxFont& font) { m_font = font; }
/** Set layer pen
@param pen Pen, will be copied to internal class member
*/
void SetPen(wxPen pen) { m_pen = pen; }
/** Set Draw mode: inside or outside margins. Default is outside, which allows the layer to draw up to the mpWindow border.
@param drawModeOutside The draw mode to be set */
void SetDrawOutsideMargins(bool drawModeOutside) { m_drawOutsideMargins = drawModeOutside; };
/** Get Draw mode: inside or outside margins.
@return The draw mode */
bool GetDrawOutsideMargins() { return m_drawOutsideMargins; };
/** Get a small square bitmap filled with the colour of the pen used in the layer. Useful to create legends or similar reference to the layers.
@param side side length in pixels
@return a wxBitmap filled with layer's colour */
wxBitmap GetColourSquare(int side = 16);
/** Get layer type: a Layer can be of different types: plot lines, axis, info boxes, etc, this method returns the right value.
@return An integer indicating layer type */
mpLayerType GetLayerType() { return m_type; };
/** Checks whether the layer is visible or not.
@return \a true if visible */
bool IsVisible() {return m_visible; };
/** Sets layer visibility.
@param show visibility bool. */
void SetVisible(bool show) { m_visible = show; };
/** Get brush set for this layer.
@return brush. */
const wxBrush& GetBrush() const { return m_brush; };
/** Set layer brush
@param brush brush, will be copied to internal class member */
void SetBrush(wxBrush brush) { m_brush = brush; };
protected:
wxFont m_font; //!< Layer's font
wxPen m_pen; //!< Layer's pen
wxBrush m_brush; //!< Layer's brush
wxString m_name; //!< Layer's name
bool m_continuous; //!< Specify if the layer will be plotted as a continuous line or a set of points.
bool m_showName; //!< States whether the name of the layer must be shown (default is true).
bool m_drawOutsideMargins; //!< select if the layer should draw only inside margins or over all DC
mpLayerType m_type; //!< Define layer type, which is assigned by constructor
bool m_visible; //!< Toggles layer visibility
DECLARE_DYNAMIC_CLASS(mpLayer)
};
//-----------------------------------------------------------------------------
// mpInfoLayer
//-----------------------------------------------------------------------------
/** @class mpInfoLayer
@brief Base class to create small rectangular info boxes
mpInfoLayer is the base class to create a small rectangular info box in transparent overlay over plot layers. It is used to implement objects like legends.
*/
class WXDLLIMPEXP_MATHPLOT mpInfoLayer : public mpLayer
{
public:
/** Default constructor. */
mpInfoLayer();
/** Complete constructor.
@param rect Sets the initial size rectangle of the layer.
@param brush pointer to a fill brush. Default is transparent */
mpInfoLayer(wxRect rect, const wxBrush* brush = wxTRANSPARENT_BRUSH);
/** Destructor */
virtual ~mpInfoLayer();
/** Updates the content of the info box. Should be overidden by derived classes.
Update may behave in different ways according to the type of event which called it.
@param w parent mpWindow from which to obtain informations
@param event The event which called the update. */
virtual void UpdateInfo(mpWindow& w, wxEvent& event);
/** mpInfoLayer has not bounding box. @sa mpLayer::HasBBox
@return always \a FALSE */
virtual bool HasBBox() { return false; };
/** Plot method. Can be overidden by derived classes.
@param dc the device content where to plot
@param w the window to plot
@sa mpLayer::Plot */
virtual void Plot(wxDC & dc, mpWindow & w);
/** Specifies that this is an Info box layer.
@return always \a TRUE
@sa mpLayer::IsInfo */
virtual bool IsInfo() { return true; };
/** Checks whether a point is inside the info box rectangle.
@param point The point to be checked
@return \a true if the point is inside the bounding box */
virtual bool Inside(wxPoint& point);
/** Moves the layer rectangle of given pixel deltas.
@param delta The wxPoint container for delta coordinates along x and y. Units are in pixels. */
virtual void Move(wxPoint delta);
/** Updates the rectangle reference point. Used by internal methods of mpWindow to correctly move mpInfoLayers. */
virtual void UpdateReference();
/** Returns the position of the upper left corner of the box (in pixels)
@return The rectangle position */
wxPoint GetPosition();
/** Returns the size of the box (in pixels)
@return The rectangle size */
wxSize GetSize();
/** Returns the current rectangle coordinates.
@return The info layer rectangle */
const wxRect& GetRectangle() { return m_dim; };
protected:
wxRect m_dim; //!< The bounding rectangle of the box. It may be resized dynamically by the Plot method.
wxPoint m_reference; //!< Holds the reference point for movements
wxBrush m_brush; //!< The brush to be used for the background
int m_winX, m_winY; //!< Holds the mpWindow size. Used to rescale position when window is resized.
DECLARE_DYNAMIC_CLASS(mpInfoLayer)
};
/** @class mpInfoCoords
@brief Implements an overlay box which shows the mouse coordinates in plot units.
When an mpInfoCoords layer is activated, when mouse is moved over the mpWindow, its coordinates (in mpWindow units, not pixels) are continuously reported inside the layer box. */
class WXDLLIMPEXP_MATHPLOT mpInfoCoords : public mpInfoLayer
{
public:
/** Default constructor */
mpInfoCoords();
/** Complete constructor, setting initial rectangle and background brush.
@param rect The initial bounding rectangle.
@param brush The wxBrush to be used for box background: default is transparent */
mpInfoCoords(wxRect rect, const wxBrush* brush = wxTRANSPARENT_BRUSH);
/** Default destructor */
~mpInfoCoords();
/** Updates the content of the info box. It is used to update coordinates.
@param w parent mpWindow from which to obtain information
@param event The event which called the update. */
virtual void UpdateInfo(mpWindow& w, wxEvent& event);
/** Plot method.
@param dc the device content where to plot
@param w the window to plot
@sa mpLayer::Plot */
virtual void Plot(wxDC & dc, mpWindow & w);
protected:
wxString m_content; //!< string holding the coordinates to be drawn.
};
/** @class mpInfoLegend
@brief Implements the legend to be added to the plot
This layer allows you to add a legend to describe the plots in the window. The legend uses the layer name as a label, and displays only layers of type mpLAYER_PLOT. */
class WXDLLIMPEXP_MATHPLOT mpInfoLegend : public mpInfoLayer
{
public:
/** Default constructor */
mpInfoLegend();
/** Complete constructor, setting initial rectangle and background brush.
@param rect The initial bounding rectangle.
@param brush The wxBrush to be used for box background: default is transparent
@sa mpInfoLayer::mpInfoLayer */
mpInfoLegend(wxRect rect, const wxBrush* brush = wxTRANSPARENT_BRUSH);
/** Default destructor */
~mpInfoLegend();
/** Updates the content of the info box. Unused in this class.
@param w parent mpWindow from which to obtain information
@param event The event which called the update. */
virtual void UpdateInfo(mpWindow& w, wxEvent& event);
/** Plot method.
@param dc the device content where to plot
@param w the window to plot
@sa mpLayer::Plot */
virtual void Plot(wxDC & dc, mpWindow & w);
protected:
};
//-----------------------------------------------------------------------------
// mpLayer implementations - functions
//-----------------------------------------------------------------------------
/** @name Label alignment constants
@{*/
/** @internal */
#define mpALIGNMASK 0x03
/** Aligns label to the right. For use with mpFX. */
#define mpALIGN_RIGHT 0x00
/** Aligns label to the center. For use with mpFX and mpFY. */
#define mpALIGN_CENTER 0x01
/** Aligns label to the left. For use with mpFX. */
#define mpALIGN_LEFT 0x02
/** Aligns label to the top. For use with mpFY. */
#define mpALIGN_TOP mpALIGN_RIGHT
/** Aligns label to the bottom. For use with mpFY. */
#define mpALIGN_BOTTOM mpALIGN_LEFT
/** Aligns X axis to bottom border. For mpScaleX */
#define mpALIGN_BORDER_BOTTOM 0x04
/** Aligns X axis to top border. For mpScaleX */
#define mpALIGN_BORDER_TOP 0x05
/** Set label for X axis in normal mode */
#define mpX_NORMAL 0x00
/** Set label for X axis in time mode: the value is represented as minutes:seconds.milliseconds if time is less than 2 minutes, hours:minutes:seconds otherwise. */
#define mpX_TIME 0x01
/** Set label for X axis in hours mode: the value is always represented as hours:minutes:seconds. */
#define mpX_HOURS 0x02
/** Set label for X axis in date mode: the value is always represented as yyyy-mm-dd. */
#define mpX_DATE 0x03
/** Set label for X axis in datetime mode: the value is always represented as yyyy-mm-ddThh:mm:ss. */
#define mpX_DATETIME 0x04
/** Aligns Y axis to left border. For mpScaleY */
#define mpALIGN_BORDER_LEFT mpALIGN_BORDER_BOTTOM
/** Aligns Y axis to right border. For mpScaleY */
#define mpALIGN_BORDER_RIGHT mpALIGN_BORDER_TOP
/** Aligns label to north-east. For use with mpFXY. */
#define mpALIGN_NE 0x00
/** Aligns label to north-west. For use with mpFXY. */
#define mpALIGN_NW 0x01
/** Aligns label to south-west. For use with mpFXY. */
#define mpALIGN_SW 0x02
/** Aligns label to south-east. For use with mpFXY. */
#define mpALIGN_SE 0x03
/*@}*/
/** @name mpLayer implementations - functions
@{*/
/** Abstract base class providing plot and labeling functionality for functions F:X->Y.
Override mpFX::GetY to implement a function.
Optionally implement a constructor and pass a name (label) and a label alignment
to the constructor mpFX::mpFX. If the layer name is empty, no label will be plotted.
*/
class WXDLLIMPEXP_MATHPLOT mpFX : public mpLayer
{
public:
/** @param name Label
@param flags Label alignment, pass one of #mpALIGN_RIGHT, #mpALIGN_CENTER, #mpALIGN_LEFT.
*/
mpFX(wxString name = wxEmptyString, int flags = mpALIGN_RIGHT);
/** Get function value for argument.
Override this function in your implementation.
@param x Argument
@return Function value
*/
virtual double GetY( double x ) = 0;
/** Layer plot handler.
This implementation will plot the function in the visible area and
put a label according to the aligment specified.
*/
virtual void Plot(wxDC & dc, mpWindow & w);
protected:
int m_flags; //!< Holds label alignment
DECLARE_DYNAMIC_CLASS(mpFX)
};
/** Abstract base class providing plot and labeling functionality for functions F:Y->X.
Override mpFY::GetX to implement a function.
Optionally implement a constructor and pass a name (label) and a label alignment
to the constructor mpFY::mpFY. If the layer name is empty, no label will be plotted.
*/
class WXDLLIMPEXP_MATHPLOT mpFY : public mpLayer
{
public:
/** @param name Label
@param flags Label alignment, pass one of #mpALIGN_BOTTOM, #mpALIGN_CENTER, #mpALIGN_TOP.
*/
mpFY(wxString name = wxEmptyString, int flags = mpALIGN_TOP);
/** Get function value for argument.
Override this function in your implementation.
@param y Argument
@return Function value
*/
virtual double GetX( double y ) = 0;
/** Layer plot handler.
This implementation will plot the function in the visible area and
put a label according to the aligment specified.
*/
virtual void Plot(wxDC & dc, mpWindow & w);
protected:
int m_flags; //!< Holds label alignment
DECLARE_DYNAMIC_CLASS(mpFY)
};
/** Abstract base class providing plot and labeling functionality for a locus plot F:N->X,Y.
Locus argument N is assumed to be in range 0 .. MAX_N, and implicitly derived by enumerating
all locus values. Override mpFXY::Rewind and mpFXY::GetNextXY to implement a locus.
Optionally implement a constructor and pass a name (label) and a label alignment
to the constructor mpFXY::mpFXY. If the layer name is empty, no label will be plotted.
*/
class WXDLLIMPEXP_MATHPLOT mpFXY : public mpLayer
{
public:
/** @param name Label
@param flags Label alignment, pass one of #mpALIGN_NE, #mpALIGN_NW, #mpALIGN_SW, #mpALIGN_SE.
*/
mpFXY(wxString name = wxEmptyString, int flags = mpALIGN_NE);
/** Rewind value enumeration with mpFXY::GetNextXY.
Override this function in your implementation.
*/
virtual void Rewind() = 0;
/** Get locus value for next N.
Override this function in your implementation.
@param x Returns X value
@param y Returns Y value
*/
virtual bool GetNextXY(double & x, double & y) = 0;
/** Layer plot handler.
This implementation will plot the locus in the visible area and
put a label according to the alignment specified.
*/
virtual void Plot(wxDC & dc, mpWindow & w);
protected:
int m_flags; //!< Holds label alignment
// Data to calculate label positioning
wxCoord maxDrawX, minDrawX, maxDrawY, minDrawY;
//int drawnPoints;
/** Update label positioning data
@param xnew New x coordinate
@param ynew New y coordinate
*/
void UpdateViewBoundary(wxCoord xnew, wxCoord ynew);
DECLARE_DYNAMIC_CLASS(mpFXY)
};
/** Abstract base class providing plot and labeling functionality for functions F:Y->X.
Override mpProfile::GetX to implement a function.
This class is similar to mpFY, but the Plot method is different. The plot is in fact represented by lines instead of points, which gives best rendering of rapidly-varying functions, and in general, data which are not so close one to another.
Optionally implement a constructor and pass a name (label) and a label alignment
to the constructor mpProfile::mpProfile. If the layer name is empty, no label will be plotted.
*/
class WXDLLIMPEXP_MATHPLOT mpProfile : public mpLayer
{
public:
/** @param name Label
@param flags Label alignment, pass one of #mpALIGN_BOTTOM, #mpALIGN_CENTER, #mpALIGN_TOP.
*/
mpProfile(wxString name = wxEmptyString, int flags = mpALIGN_TOP);
/** Get function value for argument.
Override this function in your implementation.
@param x Argument
@return Function value
*/
virtual double GetY( double x ) = 0;
/** Layer plot handler.
This implementation will plot the function in the visible area and
put a label according to the aligment specified.
*/
virtual void Plot(wxDC & dc, mpWindow & w);
protected:
int m_flags; //!< Holds label alignment
DECLARE_DYNAMIC_CLASS(mpProfile)
};
/*@}*/
//-----------------------------------------------------------------------------
// mpLayer implementations - furniture (scales, ...)
//-----------------------------------------------------------------------------
/** @name mpLayer implementations - furniture (scales, ...)
@{*/
/** Plot layer implementing a x-scale ruler.
The ruler is fixed at Y=0 in the coordinate system. A label is plotted at
the bottom-right hand of the ruler. The scale numbering automatically
adjusts to view and zoom factor.
*/
class WXDLLIMPEXP_MATHPLOT mpScaleX : public mpLayer
{
public:
/** Full constructor.
@param name Label to plot by the ruler
@param flags Set the position of the scale with respect to the window.
@param ticks Select ticks or grid. Give TRUE (default) for drawing axis ticks, FALSE for drawing the grid.
@param type mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds. */
mpScaleX(wxString name = wxT("X"), int flags = mpALIGN_CENTER, bool ticks = true, unsigned int type = mpX_NORMAL);
/** Layer plot handler.
This implementation will plot the ruler adjusted to the visible area. */
virtual void Plot(wxDC & dc, mpWindow & w);
/** Check whether this layer has a bounding box.
This implementation returns \a FALSE thus making the ruler invisible
to the plot layer bounding box calculation by mpWindow. */
virtual bool HasBBox() { return FALSE; }
/** Set X axis alignment.
@param align alignment (choose between mpALIGN_BORDER_BOTTOM, mpALIGN_BOTTOM, mpALIGN_CENTER, mpALIGN_TOP, mpALIGN_BORDER_TOP */
void SetAlign(int align) { m_flags = align; };
/** Set X axis ticks or grid
@param ticks TRUE to plot axis ticks, FALSE to plot grid. */
void SetTicks(bool ticks) { m_ticks = ticks; };
/** Get X axis ticks or grid
@return TRUE if plot is drawing axis ticks, FALSE if the grid is active. */
bool GetTicks() { return m_ticks; };
/** Get X axis label view mode.
@return mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds. */
unsigned int GetLabelMode() { return m_labelType; };
/** Set X axis label view mode.
@param mode mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds. */
void SetLabelMode(unsigned int mode) { m_labelType = mode; };
/** Set X axis Label format (used for mpX_NORMAL draw mode).
@param format The format string */
void SetLabelFormat(const wxString& format) { m_labelFormat = format; };
/** Get X axis Label format (used for mpX_NORMAL draw mode).
@return The format string */
const wxString& SetLabelFormat() { return m_labelFormat; };
protected:
int m_flags; //!< Flag for axis alignment
bool m_ticks; //!< Flag to toggle between ticks or grid
unsigned int m_labelType; //!< Select labels mode: mpX_NORMAL for normal labels, mpX_TIME for time axis in hours, minutes, seconds
wxString m_labelFormat; //!< Format string used to print labels
DECLARE_DYNAMIC_CLASS(mpScaleX)
};
/** Plot layer implementing a y-scale ruler.
If align is set to mpALIGN_CENTER, the ruler is fixed at X=0 in the coordinate system. If the align is set to mpALIGN_TOP or mpALIGN_BOTTOM, the axis is always drawn respectively at top or bottom of the window. A label is plotted at
the top-right hand of the ruler. The scale numbering automatically
adjusts to view and zoom factor.
*/
class WXDLLIMPEXP_MATHPLOT mpScaleY : public mpLayer
{
public:
/** @param name Label to plot by the ruler
@param flags Set position of the scale respect to the window.
@param ticks Select ticks or grid. Give TRUE (default) for drawing axis ticks, FALSE for drawing the grid */
mpScaleY(wxString name = wxT("Y"), int flags = mpALIGN_CENTER, bool ticks = true);
/** Layer plot handler.
This implementation will plot the ruler adjusted to the visible area.
*/
virtual void Plot(wxDC & dc, mpWindow & w);
/** Check whether this layer has a bounding box.
This implementation returns \a FALSE thus making the ruler invisible
to the plot layer bounding box calculation by mpWindow.
*/
virtual bool HasBBox() { return FALSE; }
/** Set Y axis alignment.
@param align alignment (choose between mpALIGN_BORDER_LEFT, mpALIGN_LEFT, mpALIGN_CENTER, mpALIGN_RIGHT, mpALIGN_BORDER_RIGHT) */
void SetAlign(int align) { m_flags = align; };
/** Set Y axis ticks or grid
@param ticks TRUE to plot axis ticks, FALSE to plot grid. */
void SetTicks(bool ticks) { m_ticks = ticks; };
/** Get Y axis ticks or grid
@return TRUE if plot is drawing axis ticks, FALSE if the grid is active. */
bool GetTicks() { return m_ticks; };
/** Set Y axis Label format.
@param format The format string */
void SetLabelFormat(const wxString& format) { m_labelFormat = format; };
/** Get Y axis Label format.
@return The format string */
const wxString& SetLabelFormat() { return m_labelFormat; };
protected:
int m_flags; //!< Flag for axis alignment
bool m_ticks; //!< Flag to toggle between ticks or grid
wxString m_labelFormat; //!< Format string used to print labels
DECLARE_DYNAMIC_CLASS(mpScaleY)
};
//-----------------------------------------------------------------------------
// mpWindow
//-----------------------------------------------------------------------------
/** @name Constants defining mouse modes for mpWindow
@{*/
/** Mouse panning drags the view. Mouse mode for mpWindow. */
#define mpMOUSEMODE_DRAG 0
/** Mouse panning creates a zoom box. Mouse mode for mpWindow. */
#define mpMOUSEMODE_ZOOMBOX 1
/*@}*/
/** Define the type for the list of layers inside mpWindow */
//WX_DECLARE_HASH_MAP( int, mpLayer*, wxIntegerHash, wxIntegerEqual, wxLayerList );
typedef std::deque<mpLayer*> wxLayerList;
/** Canvas for plotting mpLayer implementations.
This class defines a zoomable and moveable 2D plot canvas. Any number
of mpLayer implementations (scale rulers, function plots, ...) can be
attached using mpWindow::AddLayer.
The canvas window provides a context menu with actions for navigating the view.
The context menu can be retrieved with mpWindow::GetPopupMenu, e.g. for extending it
externally.
Since wxMathPlot version 0.03, the mpWindow incorporates the following features:
- DoubleBuffering (Default=disabled): Can be set with EnableDoubleBuffer
- Mouse based pan/zoom (Default=enabled): Can be set with EnableMousePanZoom.
The mouse commands can be visualized by the user through the popup menu, and are:
- Mouse Move+CTRL: Pan (Move)
- Mouse Wheel: Vertical scroll
- Mouse Wheel+SHIFT: Horizontal scroll
- Mouse Wheel UP+CTRL: Zoom in
- Mouse Wheel DOWN+CTRL: Zoom out
*/
class WXDLLIMPEXP_MATHPLOT mpWindow : public wxWindow
{
public:
mpWindow() {}
mpWindow( wxWindow *parent, wxWindowID id,
const wxPoint &pos = wxDefaultPosition,
const wxSize &size = wxDefaultSize,
long flags = 0);
~mpWindow();
/** Get reference to context menu of the plot canvas.
@return Pointer to menu. The menu can be modified.
*/
wxMenu* GetPopupMenu() { return &m_popmenu; }
/** Add a plot layer to the canvas.
@param layer Pointer to layer. The mpLayer object will get under control of mpWindow,
i.e. it will be delete'd on mpWindow destruction
@param refreshDisplay States whether to refresh the display (UpdateAll) after adding the layer.
@retval TRUE Success
@retval FALSE Failure due to out of memory.
*/
bool AddLayer( mpLayer* layer, bool refreshDisplay = true);
/** Remove a plot layer from the canvas.
@param layer Pointer to layer. The mpLayer object will be destructed using delete.
@param alsoDeleteObject If set to true, the mpLayer object will be also "deleted", not just removed from the internal list.
@param refreshDisplay States whether to refresh the display (UpdateAll) after removing the layer.
@return true if layer is deleted correctly
N.B. Only the layer reference in the mpWindow is deleted, the layer object still exists!
*/
bool DelLayer( mpLayer* layer, bool alsoDeleteObject = false, bool refreshDisplay = true);
/** Remove all layers from the plot.
@param alsoDeleteObject If set to true, the mpLayer objects will be also "deleted", not just removed from the internal list.
@param refreshDisplay States whether to refresh the display (UpdateAll) after removing the layers.
*/
void DelAllLayers( bool alsoDeleteObject, bool refreshDisplay = true);
/*! Get the layer in list position indicated.
N.B. You <i>must</i> know the index of the layer inside the list!
@param position position of the layer in the layers list
@return pointer to mpLayer
*/
mpLayer* GetLayer(int position);
/*! Get the layer by its name (case sensitive).
@param name The name of the layer to retrieve
@return A pointer to the mpLayer object, or NULL if not found.
*/
mpLayer* GetLayerByName( const wxString &name);
/** Get current view's X scale.
See @ref mpLayer::Plot "rules for coordinate transformation"
@return Scale
*/
double GetXscl() { return m_scaleX; }
double GetScaleX(void) const{ return m_scaleX; }; // Schaling's method: maybe another method esists with the same name
/** Get current view's Y scale.
See @ref mpLayer::Plot "rules for coordinate transformation"
@return Scale
*/
double GetYscl() const { return m_scaleY; }
double GetScaleY(void) const { return m_scaleY; } // Schaling's method: maybe another method exists with the same name
/** Get current view's X position.
See @ref mpLayer::Plot "rules for coordinate transformation"
@return X Position in layer coordinate system, that corresponds to the center point of the view.
*/
double GetXpos() const { return m_posX; }
double GetPosX(void) const { return m_posX; }
/** Get current view's Y position.
See @ref mpLayer::Plot "rules for coordinate transformation"
@return Y Position in layer coordinate system, that corresponds to the center point of the view.
*/
double GetYpos() const { return m_posY; }
double GetPosY(void) const { return m_posY; }
/** Get current view's X dimension in device context units.
Usually this is equal to wxDC::GetSize, but it might differ thus mpLayer
implementations should rely on the value returned by the function.
See @ref mpLayer::Plot "rules for coordinate transformation"
@return X dimension.
*/
int GetScrX(void) const { return m_scrX; }
int GetXScreen(void) const { return m_scrX; }
/** Get current view's Y dimension in device context units.
Usually this is equal to wxDC::GetSize, but it might differ thus mpLayer
implementations should rely on the value returned by the function.
See @ref mpLayer::Plot "rules for coordinate transformation"
@return Y dimension.
*/
int GetScrY(void) const { return m_scrY; }
int GetYScreen(void) const { return m_scrY; }
/** Set current view's X scale and refresh display.
@param scaleX New scale, must not be 0.
*/
void SetScaleX(double scaleX);
/** Set current view's Y scale and refresh display.
@param scaleY New scale, must not be 0.
*/
void SetScaleY(double scaleY) { if (scaleY!=0) m_scaleY=scaleY; UpdateAll(); }
/** Set current view's X position and refresh display.
@param posX New position that corresponds to the center point of the view.
*/
void SetPosX(double posX) { m_posX=posX; UpdateAll(); }
/** Set current view's Y position and refresh display.
@param posY New position that corresponds to the center point of the view.
*/
void SetPosY(double posY) { m_posY=posY; UpdateAll(); }
/** Set current view's X and Y position and refresh display.
@param posX New position that corresponds to the center point of the view.
@param posY New position that corresponds to the center point of the view.
*/
void SetPos( double posX, double posY) { m_posX=posX; m_posY=posY; UpdateAll(); }
/** Set current view's dimensions in device context units.
Needed by plotting functions. It doesn't refresh display.
@param scrX New position that corresponds to the center point of the view.
@param scrY New position that corresponds to the center point of the view.
*/
void SetScr( int scrX, int scrY) { m_scrX=scrX; m_scrY=scrY; }
/** Converts mpWindow (screen) pixel coordinates into graph (floating point) coordinates, using current mpWindow position and scale.
* @sa p2y,x2p,y2p */
// double p2x(wxCoord pixelCoordX, bool drawOutside = true ); // { return m_posX + pixelCoordX/m_scaleX; }
inline double p2x(wxCoord pixelCoordX ) { return m_posX + pixelCoordX/m_scaleX; }
/** Converts mpWindow (screen) pixel coordinates into graph (floating point) coordinates, using current mpWindow position and scale.
* @sa p2x,x2p,y2p */
// double p2y(wxCoord pixelCoordY, bool drawOutside = true ); //{ return m_posY - pixelCoordY/m_scaleY; }
inline double p2y(wxCoord pixelCoordY ) { return m_posY - pixelCoordY/m_scaleY; }
/** Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates, using current mpWindow position and scale.
* @sa p2x,p2y,y2p */
// wxCoord x2p(double x, bool drawOutside = true); // { return (wxCoord) ( (x-m_posX) * m_scaleX); }
inline wxCoord x2p(double x) { return (wxCoord) ( (x-m_posX) * m_scaleX); }
/** Converts graph (floating point) coordinates into mpWindow (screen) pixel coordinates, using current mpWindow position and scale.
* @sa p2x,p2y,x2p */
// wxCoord y2p(double y, bool drawOutside = true); // { return (wxCoord) ( (m_posY-y) * m_scaleY); }
inline wxCoord y2p(double y) { return (wxCoord) ( (m_posY-y) * m_scaleY); }
/** Enable/disable the double-buffering of the window, eliminating the flicker (default=disabled).
*/
void EnableDoubleBuffer( bool enabled ) { m_enableDoubleBuffer = enabled; }
/** Enable/disable the feature of pan/zoom with the mouse (default=enabled)
*/
void EnableMousePanZoom( bool enabled ) { m_enableMouseNavigation = enabled; }
/** Enable or disable X/Y scale aspect locking for the view.
@note Explicit calls to mpWindow::SetScaleX and mpWindow::SetScaleY will set
an unlocked aspect, but any other action changing the view scale will
lock the aspect again.
*/
void LockAspect(bool enable = TRUE);
/** Checks whether the X/Y scale aspect is locked.
@retval TRUE Locked
@retval FALSE Unlocked
*/
inline bool IsAspectLocked() { return m_lockaspect; }
/** Set view to fit global bounding box of all plot layers and refresh display.
Scale and position will be set to show all attached mpLayers.
The X/Y scale aspect lock is taken into account.
*/
void Fit();