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
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#ifndef WX_PLOT_HEADER_2344252459
#define WX_PLOT_HEADER_2344252459
#include <vector>
#include <memory>
#include <wx/panel.h>
#include <wx/dcbuffer.h>
#include <zen/string_tools.h>
//simple 2D graph as wxPanel specialization
namespace zen
{
/*
Example:
//init graph (optional)
m_panelGraph->setAttributes(Graph2D::MainAttributes().
setLabelX(Graph2D::POSLX_BOTTOM, 20, std::make_shared<LabelFormatterTimeElapsed>()).
setLabelY(Graph2D::POSLY_RIGHT, 60, std::make_shared<LabelFormatterBytes>()));
//set graph data
std::shared_ptr<GraphData> graphDataBytes = ...
m_panelGraph->setData(graphDataBytes, Graph2D::CurveAttributes().setLineWidth(2).setColor(wxColor(0, 192, 0)));
*/
//------------------------------------------------------------------------------------------------------------
struct GraphData
{
virtual ~GraphData() {}
virtual double getValue (double x) const = 0;
virtual double getXBegin() const = 0;
virtual double getXEnd () const = 0; //upper bound for x, getValue() is NOT evaluated at this position! Similar to std::vector::end()
};
//reference data implementation
class RangeData : public GraphData
{
public:
std::vector<double>& refData() { return data; }
private:
virtual double getValue(double x) const
{
const size_t pos = static_cast<size_t>(x);
return pos < data.size() ? data[pos] : 0;
}
virtual double getXBegin() const { return 0; }
virtual double getXEnd() const { return data.size(); } //example: two-element range is accessible within [0, 2)
std::vector<double> data;
};
//reference data implementation
class VectorData : public GraphData
{
public:
operator std::vector<double>& () { return data; }
private:
virtual double getValue(double x) const
{
const size_t pos = static_cast<size_t>(x);
return pos < data.size() ? data[pos] : 0;
}
virtual double getXBegin() const { return 0; }
virtual double getXEnd() const { return data.size(); } //example: two-element range is accessible within [0, 2)
std::vector<double> data;
};
//------------------------------------------------------------------------------------------------------------
struct LabelFormatter
{
virtual ~LabelFormatter() {}
//determine convenient graph label block size in unit of data: usually some small deviation on "sizeProposed"
virtual double getOptimalBlockSize(double sizeProposed) const = 0;
//create human-readable text for x or y-axis position
virtual wxString formatText(double value, double optimalBlockSize) const = 0;
};
double nextNiceNumber(double blockSize); //round to next number which is convenient to read, e.g. 2.13 -> 2; 2.7 -> 2.5; 7 -> 5
struct DecimalNumberFormatter : public LabelFormatter
{
virtual double getOptimalBlockSize(double sizeProposed) const { return nextNiceNumber(sizeProposed); }
virtual wxString formatText(double value, double optimalBlockSize) const { return zen::numberTo<wxString>(value); }
};
//------------------------------------------------------------------------------------------------------------
//emit data selection event
//Usage: wnd.Connect(wxEVT_GRAPH_SELECTION, GraphSelectEventHandler(MyDlg::OnGraphSelection), nullptr, this);
// void MyDlg::OnGraphSelection(GraphSelectEvent& event);
extern const wxEventType wxEVT_GRAPH_SELECTION;
struct SelectionBlock
{
struct Point
{
Point() : x(0), y(0) {}
Point(double xVal, double yVal) : x(xVal), y(yVal) {}
double x;
double y;
};
Point from;
Point to;
};
class GraphSelectEvent : public wxCommandEvent
{
public:
GraphSelectEvent(const SelectionBlock& selBlock) : wxCommandEvent(wxEVT_GRAPH_SELECTION), selBlock_(selBlock) {}
virtual wxEvent* Clone() const { return new GraphSelectEvent(selBlock_); }
SelectionBlock getSelection() { return selBlock_; }
private:
SelectionBlock selBlock_;
};
typedef void (wxEvtHandler::*GraphSelectEventFunction)(GraphSelectEvent&);
#define GraphSelectEventHandler(func) \
(wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(GraphSelectEventFunction, &func)
//------------------------------------------------------------------------------------------------------------
class Graph2D : public wxPanel
{
public:
Graph2D(wxWindow* parent,
wxWindowID winid = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = wxTAB_TRAVERSAL | wxNO_BORDER,
const wxString& name = wxPanelNameStr);
class CurveAttributes
{
public:
CurveAttributes() : autoColor(true), drawCurveArea(false), lineWidth(2) {}
CurveAttributes& setColor (const wxColour& col) { color = col; autoColor = false; return *this; }
CurveAttributes& fillCurveArea(const wxColour& col) { fillColor = col; drawCurveArea = true; return *this; }
CurveAttributes& setLineWidth(size_t width) { lineWidth = static_cast<int>(width); return *this; }
private:
friend class Graph2D;
bool autoColor;
wxColour color;
bool drawCurveArea;
wxColour fillColor;
int lineWidth;
};
void setData(const std::shared_ptr<GraphData>& data, const CurveAttributes& attr = CurveAttributes());
void addData(const std::shared_ptr<GraphData>& data, const CurveAttributes& attr = CurveAttributes());
enum PosLabelY
{
Y_LABEL_LEFT,
Y_LABEL_RIGHT,
Y_LABEL_NONE
};
enum PosLabelX
{
X_LABEL_TOP,
X_LABEL_BOTTOM,
X_LABEL_NONE
};
enum SelMode
{
SELECT_NONE,
SELECT_RECTANGLE,
SELECT_X_AXIS,
SELECT_Y_AXIS,
};
class MainAttributes
{
public:
MainAttributes() :
minXauto(true),
maxXauto(true),
minX(0),
maxX(0),
minYauto(true),
maxYauto(true),
minY(0),
maxY(0),
labelposX(X_LABEL_BOTTOM),
xLabelHeight(25),
labelFmtX(std::make_shared<DecimalNumberFormatter>()),
labelposY(Y_LABEL_LEFT),
yLabelWidth(60),
labelFmtY(std::make_shared<DecimalNumberFormatter>()),
mouseSelMode(SELECT_RECTANGLE) {}
MainAttributes& setMinX(double newMinX) { minX = newMinX; minXauto = false; return *this; }
MainAttributes& setMaxX(double newMaxX) { maxX = newMaxX; maxXauto = false; return *this; }
MainAttributes& setMinY(double newMinY) { minY = newMinY; minYauto = false; return *this; }
MainAttributes& setMaxY(double newMaxY) { maxY = newMaxY; maxYauto = false; return *this; }
MainAttributes& setAutoSize() { minXauto = true; maxXauto = true; minYauto = true; maxYauto = true; return *this; }
static const std::shared_ptr<LabelFormatter> defaultFormat;
MainAttributes& setLabelX(PosLabelX posX, size_t height = 25, const std::shared_ptr<LabelFormatter>& newLabelFmt = defaultFormat)
{
labelposX = posX;
xLabelHeight = static_cast<int>(height);
labelFmtX = newLabelFmt;
return *this;
}
MainAttributes& setLabelY(PosLabelY posY, size_t width = 60, const std::shared_ptr<LabelFormatter>& newLabelFmt = defaultFormat)
{
labelposY = posY;
yLabelWidth = static_cast<int>(width);
labelFmtY = newLabelFmt;
return *this;
}
MainAttributes& setSelectionMode(SelMode mode) { mouseSelMode = mode; return *this; }
private:
friend class Graph2D;
bool minXauto; //autodetect range for X value
bool maxXauto;
double minX; //x-range to visualize
double maxX;
bool minYauto; //autodetect range for Y value
bool maxYauto;
double minY; //y-range to visualize
double maxY;
PosLabelX labelposX;
int xLabelHeight;
std::shared_ptr<LabelFormatter> labelFmtX;
PosLabelY labelposY;
int yLabelWidth;
std::shared_ptr<LabelFormatter> labelFmtY;
SelMode mouseSelMode;
};
void setAttributes(const MainAttributes& newAttr) { attr = newAttr; Refresh(); }
MainAttributes getAttributes() const { return attr; }
std::vector<SelectionBlock> getSelections() const { return oldSel; }
void setSelections(const std::vector<SelectionBlock>& sel)
{
oldSel = sel;
activeSel.reset();
Refresh();
}
void clearSelection() { oldSel.clear(); Refresh(); }
private:
void OnMouseLeftDown(wxMouseEvent& event);
void OnMouseMovement(wxMouseEvent& event);
void OnMouseLeftUp (wxMouseEvent& event);
void OnMouseCaptureLost(wxMouseCaptureLostEvent& event);
void onPaintEvent(wxPaintEvent& event);
void onSizeEvent(wxSizeEvent& event) { Refresh(); event.Skip(); }
void onEraseBackGround(wxEraseEvent& event) {}
void render(wxDC& dc) const;
class MouseSelection
{
public:
MouseSelection(wxWindow& wnd, const wxPoint& posDragStart) : wnd_(wnd), posDragStart_(posDragStart), posDragCurrent(posDragStart) { wnd_.CaptureMouse(); }
~MouseSelection() { if (wnd_.HasCapture()) wnd_.ReleaseMouse(); }
wxPoint getStartPos() const { return posDragStart_; }
wxPoint& refCurrentPos() { return posDragCurrent; }
SelectionBlock& refSelection() { return selBlock; } //set when selection is drawn: this is fine, 'cause only what's shown should be selected!
private:
wxWindow& wnd_;
const wxPoint posDragStart_;
wxPoint posDragCurrent;
SelectionBlock selBlock;
};
std::vector<SelectionBlock> oldSel; //applied selections
std::shared_ptr<MouseSelection> activeSel; //set during mouse selection
MainAttributes attr; //global attributes
std::unique_ptr<wxBitmap> doubleBuffer;
typedef std::vector<std::pair<std::shared_ptr<GraphData>, CurveAttributes>> GraphList;
GraphList curves_;
};
}
#endif //WX_PLOT_HEADER_2344252459
|