TickMarkDecorator.cc
18.8 KB
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
/*
* TickMarkDecorator.cc
*
* Created on: Dec 18, 2013
* Author: amdadev
*/
#include "TickMarkDecorator.hh"
#include <iosfwd>
#include <boost/format.hpp>
#include <tuple>
#include "log4cxx/logger.h"
#include "plplot/plplot.h"
#include "boost/exception/all.hpp"
#include "PlotLogger.hh"
#include "TimeAxis.hh"
#include "Page.hh"
#include "Time/TimePlot.hh"
#include "TickPlot.hh"
using boost::tuples::get;
namespace plot {
log4cxx::LoggerPtr TickMarkDecorator::_logger(
log4cxx::Logger::getLogger("AMDA-Kernel.Plot.Time.TickMarkDecorator"));
TickMarkDecorator::TickMarkDecorator (bool isStandalone, PanelPlotOutput* decoratorPlot) :
TimeAxisDecorator(),
_seriesInfoList(),
_valuesFormat("% -4.4G"),
_maxValueLen(0),
_isStandalone(isStandalone),
_decoratorPlot(decoratorPlot) {
_pTimeInfo = TimeInfoPtr(new TimeInfo());
}
TickMarkDecorator::TickMarkDecorator(const TickMarkDecorator& ref_) :
TimeAxisDecorator(ref_), _seriesInfoList(
ref_._seriesInfoList), _valuesFormat(ref_._valuesFormat), _maxValueLen(
ref_._maxValueLen), _isStandalone(ref_._isStandalone), _decoratorPlot(
ref_._decoratorPlot) {
_pTimeInfo = TimeInfoPtr(new TimeInfo(*(ref_._pTimeInfo)));
}
TickMarkDecorator& TickMarkDecorator::operator=(const TickMarkDecorator& ref_) {
if (_pTimeInfo == nullptr) {
_pTimeInfo = boost::shared_ptr<TimeInfo>(
new TimeInfo(*(ref_._pTimeInfo)));
} else {
_pTimeInfo->_timeFormat = ref_._pTimeInfo->_timeFormat;
_pTimeInfo->_timeTitle = ref_._pTimeInfo->_timeTitle;
_pTimeInfo->_min = ref_._pTimeInfo->_min;
_pTimeInfo->_maxLen = ref_._pTimeInfo->_maxLen;
}
_seriesInfoList = ref_._seriesInfoList;
_valuesFormat = ref_._valuesFormat;
_maxValueLen = ref_._maxValueLen;
_isStandalone = ref_._isStandalone;
_decoratorPlot = ref_._decoratorPlot;
return *this;
}
TickMarkDecorator::~TickMarkDecorator() {
}
void TickMarkDecorator::updatePlotArea(PanelPlotOutput* pplot_, Axis* axis_, Bounds& bounds_) {
LOG4CXX_DEBUG(_logger, "TickMarkDecorator Updating plot area : "<<bounds_.toString());
TimeAxis* pAxis = (TimeAxis*) axis_;
// gets plot margins :
Margin m = pplot_->getMargin();
// calculate char size for labels:
Font legendFont(pAxis->_legend._font);
Font panelFont(pplot_->_panel->_font);
if (legendFont._size == 0) {
legendFont = panelFont;
}
CharSize legendSize = pplot_->getCharacterSize(getPlFontScaleFactor(legendFont));
double tickmarkSpace = 0;
if (pAxis->_visible) {
// process room for label tickmarks :
double lineHeight = legendSize.second * 2;
// take into account first space between time label
tickmarkSpace += lineHeight * 1.5;
// reserve space for all series labels :
tickmarkSpace += (lineHeight * _seriesInfoList.size());
} else {
LOG4CXX_DEBUG(_logger, " Axis not visible nothing to do.");
}
// update y and height; x and width are left unchanged.
// origin of viewport is at left,bottom corner.
// to take into account the tickmarks.
bounds_._height -= tickmarkSpace;
// specific case when axis labels are displayed at bottom of the graph. Needs to update y.
if (pAxis->_position == PlotCommon::POS_BOTTOM) {
bounds_._y += tickmarkSpace;
}
// needs to reserve enough space for start date above
CharSize startDateSize = pplot_->getCharacterSize(
getPlFontScaleFactor(pplot_->_panel->_font));
bounds_._height -= startDateSize.second * 3;
bounds_._y += startDateSize.second * 3;
// take margins into account :
// compute what we need for labels at left side :
// reserve same space amount in both side (since text are centered on ticks, only half length is taken into account).
double minspaceleft = std::max((const int) (_pTimeInfo->_maxLen + 1),
(const int) _maxValueLen + 1);
if (legendFont._size > panelFont._size) {
minspaceleft *= legendSize.first;
} else {
CharSize panelSize = pplot_->getCharacterSize(getPlFontScaleFactor(panelFont));
minspaceleft *= panelSize.first;
}
minspaceleft *= 0.5;
double minspaceright = minspaceleft;
// we also need to reserve enough space for start date information
// (only in width, because height is already taken into account in PlotPanelOutput).
if (pAxis->_reverse) {
minspaceright = std::max(minspaceright,
computeStartDateWidth(pplot_, pAxis));
} else {
minspaceleft = std::max(minspaceleft,
computeStartDateWidth(pplot_, pAxis));
}
// LOG4CXX_DEBUG(_logger,"min space="<<minspace<<" margin.left="<<m._left <<" margin.right="<<m._right);
if (m._left < minspaceleft) {
// adjust x bounds :
bounds_._x += (minspaceleft - m._left);
bounds_._width -= (minspaceleft - m._left);
}
if (m._right < minspaceright) {
// adjust only width bounds :
bounds_._width -= (minspaceright - m._right);
}
// specific case of only tickmarks graph
if (pAxis->isOnlyTickmarks()) {
// since there is no graph to plot, reduce plot area to minimum :
bounds_._height = legendSize.second * 2;
}
LOG4CXX_DEBUG(_logger, "Updated plot area : "<<bounds_.toString());
}
double TickMarkDecorator::computeStartDateWidth(PanelPlotOutput* pplot_,
TimeAxis* pAxis_) {
// not very efficient ...
long int lTime = static_cast<long int>(pAxis_->getRange().getMin());
tm * lTimeTm = gmtime(&lTime);
char lTimeChr[80];
// Format date.
size_t datelen =
strftime(lTimeChr, 80,
getPlStartTimeFormat(pAxis_->_timeFormat,
pAxis_->getRange().getMin(),
pAxis_->getRange().getMax()).c_str(), lTimeTm);
CharSize fontSize = pplot_->getCharacterSize(
getPlFontScaleFactor(pplot_->_panel->_font));
return fontSize.first * datelen;
}
void TickMarkDecorator::configure(PanelPlotOutput* pplot_, Axis* axis_, double start_,
double stop_, std::map<std::string, ParameterData> *pParameterValues) {
LOG4CXX_DEBUG(_logger, "TickMarkDecorator::configure");
_pParameterValues = pParameterValues;
// retrieve time axis
TimeAxis* pTimeAxis = (TimeAxis*) axis_;
// configure x axis range
pTimeAxis->setRange(start_, stop_);
// show xAxis or not
pTimeAxis->setOnlyTickmarks(_isStandalone);
setPlFormat(
getPlTimeFormat(pTimeAxis->_timeFormat,
pTimeAxis->getRange().getMin(),
pTimeAxis->getRange().getMax(),
getMajorTickNumber(pTimeAxis,
pTimeAxis->getRange().getMin(),
pTimeAxis->getRange().getMax())));
// install label generator
installLabelGenerator(pplot_, pTimeAxis);
}
void TickMarkDecorator::draw(PanelPlotOutput* pplot_, Axis* axis_,
std::shared_ptr<plstream> pls_) {
// draw a virtual axis for each entry // take into account plot orientation
TimeAxis* pAxis = (TimeAxis*) axis_;
// Get plot area viewport.
Bounds vpBounds;
PLFLT lXMin, lXMax, lYMin, lYMax;
pls_->gvpd(lXMin, lXMax, lYMin, lYMax);
vpBounds._x = lXMin;
vpBounds._y = lYMin;
vpBounds._width = lXMax - lXMin;
vpBounds._height = lYMax - lYMin;
// get windows
Bounds winBounds;
PLFLT xwmin, xwmax, ywmin, ywmax;
pls_->gvpw(xwmin, xwmax, ywmin, ywmax);
winBounds._x = xwmin;
winBounds._y = ywmin;
winBounds._width = xwmax - xwmin;
winBounds._height = ywmax - ywmin;
LOG4CXX_DEBUG(_logger, " viewport :"<<vpBounds.toString());
LOG4CXX_DEBUG(_logger, " window :"<<winBounds.toString());
// tick configuration for this axis :
double xtick = getMajorTickSpace(pAxis, pAxis->getRange().getMin(),
pAxis->getRange().getMax());
int nxsub = getMinorTickNumber(pAxis, pAxis->getRange().getMin(),
pAxis->getRange().getMax(), xtick);
// set color for drawing axis :
Color lInitialColor = changeColor(pls_, pAxis->_color,
pplot_->_panel->_page->_mode);
// Set tick (major and minor) length factor
PLFLT lTickLenFact = PanelPlotOutput::DEFAULT_TICK_LENGTH_FACTOR;
if (!isnan(pAxis->_tick._lengthFactor)) {
lTickLenFact = pAxis->_tick._lengthFactor;
}
// 0 => keep tick height in millimeters.
pls_->smaj(0, lTickLenFact);
pls_->smin(0, lTickLenFact);
// Set font to draw axes.
std::vector<Label::Style> panelStyles;
Font panelFont(pplot_->_panel->_font);
std::vector<Label::Style> legendStyles(pAxis->_legend._style);
Font legendFont(pAxis->_legend._font);
if (legendFont._size == 0) {
// font has not been defined for that axis, used default panel font :
legendFont = panelFont;
legendStyles = panelStyles;
}
pls_->schr(getPlFontDef(legendFont), getPlFontScaleFactor(legendFont));
pls_->sfont(getPlFontFamily(legendFont), getPlFontStyle(legendStyles),
getPlFontWeight(legendStyles));
// viewport translation only along y direction: we translate about n charHeight. So, gets panel character height.
CharSize legendCharSize(pplot_->getCharacterSize(getPlFontScaleFactor(legendFont)));
CharSize panelCharSize(pplot_->getCharacterSize(getPlFontScaleFactor(panelFont)));
// delta is negative when axis is at bottom (which is the default case). positive otherwise.
// (viewport origin is in the left bottom corner of the page).
double yDelta = -legendCharSize.second * 2;
std::string options = "";
int start;
int end;
int dir;
if (pAxis->_position == PlotCommon::POS_TOP) {
options = "mxo"; // options means : m=display above axe, x=do not plot ticks, o=user label function provided.
yDelta = -yDelta; // if axis is displayed on top of graph we have to use an inverse translation delta.
// reverse direction :
start = _seriesInfoList.size() - 1;
end = -1;
dir = -1;
} else {
options = "nxo";// options means : n=display below axe, x=do not plot ticks, o=user label function provided.
start = 0;
end = _seriesInfoList.size();
dir = 1;
}
LOG4CXX_DEBUG(_logger, " viewport translation:"<<yDelta);
LOG4CXX_DEBUG(_logger, " virtual axis options:"<<options);
// store initial delta, as it is also the increment to add to delta at each new serie (see below).
double yInc = yDelta;
// at this point, we change viewport to be able to plot each virtual axis
// (only labels are plotted)...
// We need to take into account tick length.
if (pAxis->_tick._position == Tick::TickPosition::OUTWARDS) {
// set room for tick, tickmarks (graduation) and extra space between tickmarks (graduation) and data time.
yDelta = getVerticalTickLength(pAxis, panelCharSize.second) + yDelta + (0.5 * yInc);
} else {
// for the first serie, we add an extra space (half of yInc) to add a sepration with time labels
yDelta = yDelta + (0.5 * yInc);
}
for (int i = start; i != end; i += dir) {
LOG4CXX_TRACE(_logger, " virtual axis ["<<i<<"]");
// install label generator specific for that virtual axis
pls_->slabelfunc(generateYLabel, _seriesInfoList[i].get());
// translate viewport :
pls_->vpor(vpBounds._x, vpBounds._x + vpBounds._width,
vpBounds._y + yDelta, vpBounds._y + vpBounds._height + yDelta);
// sets windows for new viewport :
pls_->wind(winBounds._x, winBounds._x + winBounds._width, winBounds._y,
winBounds._y + winBounds._height);
pls_->box(options.c_str(), xtick, nxsub, "", 0, 0);
yDelta = yDelta + yInc;
}
// restore viewport.
pls_->vpor(vpBounds._x, vpBounds._x + vpBounds._width, vpBounds._y,
vpBounds._y + vpBounds._height);
// restore window
pls_->wind(winBounds._x, winBounds._x + winBounds._width, winBounds._y,
winBounds._y + winBounds._height);
// Restore initial color.
restoreColor(pls_, lInitialColor, pplot_->_panel->_page->_mode);
}
/**
* @brief Search for each tick time, the closest parameter time in the paramdata_ and store the corresponding value.
* Selected parameter time is in range ]ticktime-step, ticktime+step[ where step is the space between two tick.
* If no such time is found for a given tick, associated value is marked as NaN.
* Note that this algorithm may retrieve same value twice if there is exactly one time between two ticks
* and it is exactly at the middle.
* @param seriesdata_ series data to fill
* @param pAxis_ related axis
* @param paramdata_ related parameter
* @param serieIndex_ series index.
* @return longest label to display for that series (in number of char).
*/
unsigned int TickMarkDecorator::fillSeriesData(SeriesData& seriesdata_,
TimeAxis* pAxis_, ParameterData& paramdata_, AMDA::Common::ParameterIndexComponent serieIndex_) {
LOG4CXX_DEBUG(_logger, " filling series data...");
// string used to compute formattedvalue max length
char formattedValue [40];
unsigned int maxlen = 0;
// time range :
double start = pAxis_->getRange().getMin();
double stop = pAxis_->getRange().getMax();
double step = getMajorTickSpace(pAxis_, start, stop);
LOG4CXX_TRACE(_logger,
" for all ticks between " << start << " and " << stop << " spaced by " << step);
int index = -1;
int size = paramdata_.getSize();
for (double tickTime = start; tickTime <= stop; tickTime += step) {
// search index closest value to that time :
double* ptime = paramdata_.getTimes();
int i = index + 1;
while (i < size && (*(ptime + i) < tickTime)) {
i++;
}
double cur = (i >= size) ? -1 : *(ptime + i);
double prev = (i == 0) ? -1 : *(ptime + i - 1);
if (prev != -1 && prev <= (tickTime - step)) {
prev = -1;
}
if (cur != -1 && cur >= (tickTime + step)) {
cur = -1;
}
if (prev == -1) {
// only cur can be eligible
if (cur == -1) {
index = -1; // no suitable value
} else {
index = i;
}
} else if (cur == -1) {
// only prev can be eligible and it is not -1 due to previous test
index = i - 1;
} else {
// both are eligible, choose closest one :
if ((tickTime - prev) > (cur - tickTime)) {
index = i;
} else {
index = i - 1;
}
}
// time found, retrieve related value or put a nan one:
if (index == -1) {
LOG4CXX_TRACE(_logger,
"updating series data at time:"<<tickTime<<" with value:(NaN)");
seriesdata_[tickTime] = nan("NaN");
// restore last index for next loop :
index = i;
} else {
double value = *(paramdata_.getValues(serieIndex_) + index);
LOG4CXX_TRACE(_logger,
"updating series data at time:"<<tickTime<<" with value :("<<value<<")");
// compute formatted value length :
const unsigned int len = snprintf(formattedValue, 40, getValuesFormat().c_str(), value);
maxlen = std::max((const unsigned int) maxlen, len);
seriesdata_[tickTime] = value;
}
}
return std::min((const unsigned int) maxlen, (const unsigned int) 40);
}
void TickMarkDecorator::installLabelGenerator(PanelPlotOutput* /*pplot_*/, TimeAxis* timeAxis_) {
LOG4CXX_TRACE(_logger, "installing LabelGenerator function");
// axis label :
LOG4CXX_TRACE(_logger, "processing axis : "<< timeAxis_->_legend._text);
// install time format
_pTimeInfo->_timeFormat = getPlFormat();
_pTimeInfo->_timeTitle = timeAxis_->_legend._text;
_pTimeInfo->_min = timeAxis_->getRange().getMin();
unsigned int maxNameLen = _pTimeInfo->_timeTitle.size();
// init maximum value len with formatted time :
if (TimePlot::qsasconfig == NULL) {
configqsas(1. / 86400., 0., 0., 0x0, 1, 1970, 0, 1, 0, 0, 0.,
&TimePlot::qsasconfig);
}
char formattedStr [40];
double v = 200800000000000;
_maxValueLen = strfqsas(formattedStr, 40, _pTimeInfo->_timeFormat.c_str(), v, TimePlot::qsasconfig);
// get decorator parameter value
//_decoratorPlot->getDataFromServer();
// build all series names :
_seriesInfoList.clear();
for (auto p : _decoratorPlot->_parameterAxesList) {
const std::string name = p._originalParamId;
LOG4CXX_DEBUG(_logger, " inspecting parameter : " << name);
// list asked series :
for (auto q : p.getYSerieIndexList(_pParameterValues)) {
boost::shared_ptr<SeriesInfo> pSerie =
boost::shared_ptr<SeriesInfo>(new SeriesInfo());
std::ostringstream osstr;
osstr << name;
// display index only if non scalar
if(q.getDim1Index() != -1){
osstr << "[" << q.getDim1Index() ;
if (q.getDim2Index() != -1)
osstr << "," << q.getDim2Index();
osstr << "]";
}
pSerie->_name = osstr.str();
if (pSerie->_name.size() > maxNameLen) {
maxNameLen = pSerie->_name.size();
}
pSerie->_min = timeAxis_->getRange().getMin();
pSerie->_valuesFormat = getValuesFormat();
// get serie at index :
SeriesData data;
_maxValueLen = std::max((const unsigned int) _maxValueLen,
fillSeriesData(data, timeAxis_,
(*_pParameterValues)[p.getYSeriePropertiesAt(q).getParamId()],
q));
pSerie->_data = data;
_seriesInfoList.push_back(pSerie);
}
}
// normalize titles :
_pTimeInfo->_maxLen = maxNameLen;
for (auto p : _seriesInfoList) {
p->_maxLen = maxNameLen;
p->_maxValueLen = _maxValueLen;
}
// no legend for this axe since it is displayed at axis origin:
timeAxis_->_legend._text = ""; //titles.str();
// install label generator for main axe (the one that is displayed)
timeAxis_->_labelGenerator->_data = _pTimeInfo.get();
timeAxis_->_labelGenerator->_function = generateOrbitTimeLabel;
}
/***************************** EXTERNAL METHODS ********************************/
void generateYLabel(PLINT axis, PLFLT value, char *label, PLINT length,
PLPointer data) {
if (axis == PL_X_AXIS) {
if (data == NULL) {
std::cout
<< "WARNING !!!! NO DATA FOUND FOR generateYLabel FUNCTION !!!"
<< std::endl << std::endl;
LOG4CXX_WARN(gLogger,
"WARNING !!!! NO DATA FOUND FOR generateYLabel FUNCTION !!");
return;
}
SeriesInfo* pInfo = static_cast<SeriesInfo*> (data);
// if value equals min, display _name string
// otherwise, format value and display it
if (value == pInfo->_min) {
// do not display min value label
// but display title :
snprintf(label, length, "%*s", pInfo->_maxLen,
pInfo->_name.c_str());
} else {
// display formatted value
std::ostringstream labels;
// loops over each series and display value at this time
auto q = pInfo->_data.find((double) value);
if (q != pInfo->_data.end()) {
// we found it !
labels << boost::format(pInfo->_valuesFormat) % q->second;
} else {
// not found just display Nan indicator
labels << "NaN" << std::endl;
}
snprintf(label, length, "%s", labels.str().c_str());
}
}
}
void generateOrbitTimeLabel(PLINT axis, PLFLT value, char *label, PLINT length,
PLPointer data) {
if (axis == PL_X_AXIS) {
if (data == NULL) {
std::cout
<< "WARNING !!!! NO DATA FOUND FOR generateOrbitTimeLabel FUNCTION !!!"
<< std::endl << std::endl;
LOG4CXX_WARN(gLogger,
"WARNING !!!! NO DATA FOUND FOR generateOrbitTimeLabel FUNCTION !!");
return;
}
TimeInfo* pInfo = static_cast<TimeInfo*> (data);
// get time format and min value
// if value equals min, display an empty string
// otherwise, format time and display formatted time
if (value == pInfo->_min) {
// do not display min value label
// but display title :
snprintf(label, length, "%*s", pInfo->_maxLen,
pInfo->_timeTitle.c_str());
} else {
// display formatted time
std::string timeFormatstr = pInfo->_timeFormat.c_str();
char *timeFormat = new char[timeFormatstr.length() + 1];
strcpy(timeFormat, timeFormatstr.c_str());
if (TimePlot::qsasconfig == NULL) {
configqsas(1. / 86400., 0., 0., 0x0, 1, 1970, 0, 1, 0, 0, 0.,
&TimePlot::qsasconfig);
}
char formattedTime[40];
strfqsas(formattedTime, 40, timeFormat, (double) value,
TimePlot::qsasconfig);
delete[] timeFormat;
snprintf(label, length, "%s", formattedTime);
}
}
}
} /* namespace plot */