-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathplbarplot.cpp
343 lines (267 loc) · 9.78 KB
/
plbarplot.cpp
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
/*
BSD 3-Clause License
Copyright (c) Alliance for Sustainable Energy, LLC. See also https://github.com/NREL/wex/blob/develop/LICENSE
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <algorithm>
#include <math.h>
#include <wx/dc.h>
#include "wex/plot/plbarplot.h"
wxPLBarPlotBase::wxPLBarPlotBase() {
Init();
}
wxPLBarPlotBase::wxPLBarPlotBase(const std::vector<wxRealPoint> &data, double baseline,
const wxString &label,
const wxColour &col)
: wxPLPlottable(label) {
Init();
m_baseline = baseline;
m_colour = col;
m_data = data;
}
wxPLBarPlotBase::~wxPLBarPlotBase() {
// nothing to do
}
void wxPLBarPlotBase::Init() {
m_baseline = 0;
m_antiAliasing = false;
m_colour = *wxLIGHT_GREY;
m_thickness = wxPL_BAR_AUTOSIZE;
}
wxRealPoint wxPLBarPlotBase::At(size_t i) const {
return m_data[i];
}
size_t wxPLBarPlotBase::Len() const {
return m_data.size();
}
void wxPLBarPlotBase::DrawInLegend(wxPLOutputDevice &dc, const wxPLRealRect &rct) {
dc.NoPen();
dc.Brush(m_colour);
dc.Rect(rct);
}
////////// wxPLBarPlot ///////////
wxPLBarPlot::wxPLBarPlot()
: wxPLBarPlotBase() {
m_stackedOn = 0;
}
wxPLBarPlot::wxPLBarPlot(const std::vector<wxRealPoint> &data, double baseline_y,
const wxString &label,
const wxColour &col)
: wxPLBarPlotBase(data, baseline_y, label, col) {
m_stackedOn = 0;
}
wxPLBarPlot::~wxPLBarPlot() {
// nothing to do
}
wxPLAxis *wxPLBarPlot::SuggestYAxis() const {
if (Len() < 1) return new wxPLLinearAxis(0, 1);
double ymin = CalcYPos(At(0).x);
double ymax = ymin;
for (size_t i = 1; i < Len(); i++) {
double y = CalcYPos(At(i).x);
if (y < ymin) ymin = y;
if (y > ymax) ymax = y;
}
if (ymin > m_baseline) ymin = m_baseline;
if (ymax < m_baseline) ymax = m_baseline;
return new wxPLLinearAxis(ymin, ymax);
}
double wxPLBarPlot::CalcYPos(double x) const {
double y_start = m_baseline;
if (m_stackedOn && m_stackedOn != this) {
y_start += m_stackedOn->CalcYPos(x);
}
for (size_t i = 0; i < Len(); i++) {
if (At(i).x == x) {
y_start += At(i).y;
break;
}
}
return y_start;
}
double wxPLBarPlot::CalcXPos(double x, const wxPLDeviceMapping &map, double dispwidth) {
if (m_stackedOn && m_stackedOn != this)
return m_stackedOn->CalcXPos(x, map, dispwidth);
double x_start = map.ToDevice(x, 0).x;
if (m_data.size() < 366) {
size_t g, idx;
std::vector<wxPLBarPlot *>::iterator iit = std::find(m_group.begin(), m_group.end(), this);
if (m_group.size() > 0
&& iit != m_group.end()) {
idx = iit - m_group.begin();
std::vector<double> barwidths;
for (g = 0; g < m_group.size(); g++)
barwidths.push_back(m_group[g]->CalcDispBarWidth(map));
double group_width = 0;
for (g = 0; g < m_group.size(); g++)
group_width += barwidths[g];
double bar_start = x_start - group_width / 2;
for (g = 0; g < idx; g++)
bar_start += barwidths[g];
x_start = bar_start + dispwidth / 2;
}
}
return x_start;
}
double wxPLBarPlot::CalcDispBarWidth(const wxPLDeviceMapping &map) {
if (m_thickness <= 0) {
wxRealPoint pos, size;
map.GetDeviceExtents(&pos, &size);
double xmin = map.GetWorldMinimum().x;
double xmax = map.GetWorldMaximum().x;
double cxmin = 1e99, cxmax = -1e99;
int bars_in_view = 0;
// SLOW for multiple bar graphs
for (size_t i = 0; i < Len(); i++) {
double x = At(i).x;
if (x >= xmin && x <= xmax) {
bars_in_view++;
wxRealPoint C(map.ToDevice(At(i)));
if (C.x < cxmin) cxmin = C.x;
if (C.x > cxmax) cxmax = C.x;
}
}
if (bars_in_view == 0 || cxmin == cxmax)
return 10; // default point thickness
if (m_group.size() > 0) bars_in_view *= m_group.size();
int w = (int) (0.75 * ((cxmax - cxmin) / ((double) (bars_in_view))));
if (w < 2) w = 2;
return (double) w;
} else return m_thickness;
}
void wxPLBarPlot::Draw(wxPLOutputDevice &dc, const wxPLDeviceMapping &map) {
if (Len() == 0) return;
double dispbar_w = CalcDispBarWidth(map);
dc.SetAntiAliasing(false);
dc.NoPen();
dc.Brush(m_colour);
for (size_t i = 0; i < Len(); i++) {
wxRealPoint pt = At(i);
double pbottom = 0, ptop = 0;
double y_start = m_baseline;
double x_start = CalcXPos(pt.x, map, dispbar_w);
if (m_stackedOn != NULL && m_stackedOn != this) {
y_start = m_stackedOn->CalcYPos(pt.x);
x_start = m_stackedOn->CalcXPos(pt.x, map, dispbar_w);
}
wxPLRealRect prct;
prct.x = x_start - dispbar_w / 2;
prct.width = dispbar_w;
pbottom = map.ToDevice(0, y_start).y;
ptop = map.ToDevice(0, pt.y + y_start).y;
prct.y = pbottom < ptop ? pbottom : ptop;
prct.height = std::abs(pbottom - ptop);
dc.Rect(prct.x, prct.y, prct.width, prct.height);
}
}
/////////// wxPLHBarPlot ////////////
wxPLHBarPlot::wxPLHBarPlot() : wxPLBarPlotBase() { /* nothing to do */ }
wxPLHBarPlot::wxPLHBarPlot(const std::vector<wxRealPoint> &data, double baseline_x,
const wxString &label,
const wxColour &col)
: wxPLBarPlotBase(data, baseline_x, label, col) {
m_stackedOn = 0;
}
wxPLHBarPlot::~wxPLHBarPlot() {
m_stackedOn = 0;
}
void wxPLHBarPlot::Draw(wxPLOutputDevice &dc, const wxPLDeviceMapping &map) {
if (Len() == 0) return;
double bar_width = CalcDispBarWidth(map);
dc.NoPen();
dc.Brush(m_colour);
for (size_t i = 0; i < Len(); i++) {
wxRealPoint pt(At(i));
double pleft = 0, pright = 0;
double x_start = m_baseline;
if (m_stackedOn != NULL && m_stackedOn != this)
x_start = m_stackedOn->CalcXPos(pt.y);
wxPLRealRect prct;
pleft = map.ToDevice(x_start, 0).x;
pright = map.ToDevice(pt.x, 0).x;
if (pleft < pright) {
prct.x = pleft;
prct.width = pright - pleft;
} else {
prct.x = pright;
prct.width = pleft - pright;
}
prct.y = map.ToDevice(0, pt.y).y - bar_width / 2;
prct.height = bar_width;
prct.width = std::abs(pleft - pright);
if (prct.width > 0 && prct.height > 0)
dc.Rect(prct.x, prct.y, prct.width, prct.height);
}
/*
wxRealPoint start,end;
end.x = start.x = map.ToDevice( m_baseline, 0 ).x;
wxRealPoint pos, size;
map.GetDeviceExtents( &pos, &size );
// don't automatically draw a vertical bar to show baseline
// now can use annotation feature to do this.
start.y = pos.y+1;
end.y = start.y + size.y-1;
dc.Pen( *wxBLACK, 1 );
dc.Line(start.x, start.y, end.x, end.y);
*/
}
double wxPLHBarPlot::CalcXPos(double y) const {
double x_start = m_baseline;
if (m_stackedOn && m_stackedOn != this)
x_start += m_stackedOn->CalcXPos(y);
for (size_t i = 0; i < Len(); i++) {
if (At(i).y == y) {
x_start += At(i).x;
break;
}
}
return x_start;
}
double wxPLHBarPlot::CalcDispBarWidth(const wxPLDeviceMapping &map) {
if (m_thickness <= 0.0) {
wxRealPoint pos, size;
map.GetDeviceExtents(&pos, &size);
double ymin = map.GetWorldMinimum().y;
double ymax = map.GetWorldMaximum().y;
int bars_in_view = 0;
for (size_t i = 0; i < Len(); i++) {
double y = At(i).y;
if (y >= ymin && y <= ymax)
bars_in_view++;
}
return (((double) size.y) / ((double) (bars_in_view + 4)));
} else return m_thickness;
}
wxPLAxis *wxPLHBarPlot::SuggestXAxis() const {
if (Len() < 1) return new wxPLLinearAxis(0, 1);
double xmin = CalcXPos(At(0).y);
double xmax = xmin;
for (size_t i = 1; i < Len(); i++) {
double x = CalcXPos(At(i).y);
if (x < xmin) xmin = x;
if (x > xmax) xmax = x;
}
if (xmin > m_baseline) xmin = m_baseline;
if (xmax < m_baseline) xmax = m_baseline;
return new wxPLLinearAxis(xmin, xmax);
}