Blame view

src/ParamOutputImpl/Plot/Epoch/EpochPlot.cc 12.8 KB
fbe3c2bb   Benjamin Renard   First commit
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
/*
 * EpochPlot.cc
 *
 *  Created on: 16 jan. 2015
 *      Author: AKKA
 */

#include "EpochPlot.hh"
#include "ParamsNode.hh"
#include "AxesNode.hh"
#include "PlotOutput.hh"
#include "TimeUtil.hh"
#include "Parameter.hh"
#include "ParamInfo.hh"
#include "ParamMgr.hh"
#include <fstream>

#include "EpochAxisDecorator.hh"
#include "TickMarkDecorator.hh"
#include "PlotLogger.hh"
#include "ParamMgr.hh"
#include "TimeUtil.hh"
#include "AxisLegendManager.hh"

#include <boost/format.hpp>

using namespace AMDA::Parameters;
using namespace AMDA::Info;

namespace plot {

EpochPlot::EpochPlot(AMDA::Parameters::ParameterManager& manager,
		boost::shared_ptr<Panel> panel) :
		PanelPlotOutput(manager, panel),
		_centerTimeId(""){
	_epochDecoratorPtr.reset(new EpochAxisDecorator());
}

EpochPlot::~EpochPlot() {
}


EpochAxis* EpochPlot::getEpochAxis(){
	std::string lAxisId = getXAxisId();
	boost::shared_ptr<Axis> xAxis = _panel->getAxis(lAxisId);
	if (xAxis.get() == nullptr) {
		std::stringstream lError;
		lError << "EpochPlot::apply" << ": epoch axis with id '" << lAxisId << "' not found.";
		BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
	}
	return dynamic_cast<EpochAxis*>(xAxis.get());
}

void EpochPlot::preparePlotArea(double startTime, double stopTime, int intervalIndex) {
	// dump properties for test
	const char* lBuildType=getenv("BUILD_TYPE");
	if(lBuildType && std::string(lBuildType) == "Debug") {
		std::ofstream out("epochPlot.txt");
		dump(out);
		out.close();
	}

	configureSeriesAxis();

	configureAxisLegend();

	configureParamsLegend(startTime, stopTime, intervalIndex);

	// configure X epoch axis
	_epochDecoratorPtr->configure(this, getEpochAxis(),
			getEpochAxis()->getRange().getMin(), getEpochAxis()->getRange().getMax(), _pParameterValues);

08ec1dde   Benjamin Renard   Add propertie _us...
73
74
	getEpochAxis()->_used = true;

fbe3c2bb   Benjamin Renard   First commit
75
76
77
	PanelPlotOutput::preparePlotArea(startTime,stopTime,intervalIndex);
}

d57f00dc   Benjamin Renard   Draw NO DATA
78
bool EpochPlot::draw(double startTime, double stopTime, int intervalIndex,
fbe3c2bb   Benjamin Renard   First commit
79
80
		bool isFirstInterval, bool isLastInterval) {

d57f00dc   Benjamin Renard   Draw NO DATA
81
	return PanelPlotOutput::draw(startTime,stopTime,intervalIndex,isFirstInterval,isLastInterval);
fbe3c2bb   Benjamin Renard   First commit
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
}


void EpochPlot::calculatePlotArea(Bounds& bounds_) {
	PanelPlotOutput::calculatePlotArea(bounds_);
	// decorator is responsible of reserving extra space for what it manage (labels for instance).
	_epochDecoratorPtr->updatePlotArea(this, getEpochAxis(), bounds_);
}


void EpochPlot::configureSeriesAxis() {
	/*
	 * Configure the epoch time axis
	 */
	EpochAxis* epochAxis = getEpochAxis();

	if (epochAxis == NULL)
	{
		std::stringstream lError;
		lError << "EpochPlot::configureSeriesAxis: Cannot find epochAxis";
		BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
	}

	//get epoch axis range
	Range lEpochRange;
	if (!epochAxis->isNormalized())
	{
		//axis not normalized
		TimeIntervalList::iterator crtTimeInterval = _timeIntervalListPtr->begin();
		while (crtTimeInterval != _timeIntervalListPtr->end())
		{
			//get center time data for this interval
db4a6b56   Benjamin Renard   Get the possibili...
114
115
116
			double leftDeltaTime = 0.;
			double rightDeltaTime = 0.;

fbe3c2bb   Benjamin Renard   First commit
117
118
119
			std::vector<std::string>& crtCenterTimeData = _parameterManager.getInputIntervalDataList(crtTimeInterval->_index, _centerTimeId);
			if (crtCenterTimeData.empty())
			{
db4a6b56   Benjamin Renard   Get the possibili...
120
121
122
123
124
125
126
127
128
				leftDeltaTime  = (crtTimeInterval->_stopTime-crtTimeInterval->_startTime) / 2.;
				rightDeltaTime = leftDeltaTime;
			}
			else
			{
				//compute left and right delta times (relative to the center time)
				double crtCenterTime  = AMDA::TimeUtil::readTimeInIso((char*)crtCenterTimeData[0].c_str());
				leftDeltaTime  = crtCenterTime-crtTimeInterval->_startTime;
				rightDeltaTime = crtTimeInterval->_stopTime-crtCenterTime;
fbe3c2bb   Benjamin Renard   First commit
129
130
			}

fbe3c2bb   Benjamin Renard   First commit
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
			if ((leftDeltaTime < 0) || (rightDeltaTime < 0))
			{
				LOG4CXX_WARN(gLogger, "EpochPlot::configureSeriesAxis - centerTime not contained in the interval for the interval with index : " << crtTimeInterval->_index << " => skip this interval");
				++crtTimeInterval;
				continue;
			}

			//configure the range of the axis
			if (isnan(lEpochRange.getMin()) || (lEpochRange.getMin() > -leftDeltaTime))
				lEpochRange.setMin(-leftDeltaTime);

			if (isnan(lEpochRange.getMax()) || (lEpochRange.getMax() < rightDeltaTime))
				lEpochRange.setMax(rightDeltaTime);

			++crtTimeInterval;
		}
	}
	else
	{
		//normalized epoch axis
		lEpochRange.setMin(-1);
		lEpochRange.setMax(1);
	}

	if (isnan(lEpochRange.getMin()) || isnan(lEpochRange.getMax()))
	{
		std::stringstream lError;
		lError << "EpochPlot::configureSeriesAxis: Cannot configure the range of the epochAxis";
		BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
	}

faf4d845   Benjamin Renard   Add common functi...
162
	fixRange(lEpochRange, epochAxis->_scale == Axis::Scale::LOGARITHMIC);
fbe3c2bb   Benjamin Renard   First commit
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	epochAxis->setRange(lEpochRange);

	/*
	 * configure y and z axis
	 */
	std::map<std::string, Range> lAxisRange;
	Range lColorAxeRange;
	SeriesProperties lSeriesProperties;

	boost::shared_ptr<ColorAxis> lZAxis = _panel->getColorAxis();

	// Parse each parameter to define on which axis to draw series.
	for (auto param: _parameterAxesList)
	{
		// Get number of series to draw
		// For each index of parameter identify to which axis series must be drawn.
2fc1f2f8   Benjamin Renard   Full rework for s...
179
		for(auto lSeriesProperties: param.getYSeriePropertiesList()) {
fbe3c2bb   Benjamin Renard   First commit
180
181
182
183
184
185
186
187
188
			if(!lSeriesProperties.hasYAxis()){
				continue;
			}
			boost::shared_ptr<Axis> lYAxis = _panel->getAxis(lSeriesProperties.getYAxisId());
			if (lYAxis.get() == nullptr) {
				std::stringstream lError;
				lError << "EpochPlot::configureSeriesAxis" << ": Y axis with id '" << lSeriesProperties.getYAxisId() << "' not found.";
				BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
			}
08ec1dde   Benjamin Renard   Add propertie _us...
189
			lYAxis->_used = true;
fbe3c2bb   Benjamin Renard   First commit
190
191
192
193
194
195

			Range lRange(lYAxis->getRange());
			// If range status for this axis is set by the user do not update range "automatically".
			if (isnan(lRange.getMin()) && isnan(lRange.getMax()))
			{
				Range lEstimatedRange(lAxisRange[lYAxis->_id]);
2fc1f2f8   Benjamin Renard   Full rework for s...
196
197
198
199
200
201
202
203
204
205
206
207
				for (auto index : lSeriesProperties.getIndexList(_pParameterValues)) {
					Range lParamIndexRange(
							(*_pParameterValues)[lSeriesProperties.getParamId()].getMin(index),
							(*_pParameterValues)[lSeriesProperties.getParamId()].getMax(index));

					if (isnan(lEstimatedRange.getMin()) && isnan(lEstimatedRange.getMax())) {
						lEstimatedRange.setMin(lParamIndexRange.getMin());
						lEstimatedRange.setMax(lParamIndexRange.getMax());
					} else {
						lEstimatedRange.setMin(std::min(lEstimatedRange.getMin(), lParamIndexRange.getMin()));
						lEstimatedRange.setMax(std::max(lEstimatedRange.getMax(), lParamIndexRange.getMax()));
					}
fbe3c2bb   Benjamin Renard   First commit
208
				}
faf4d845   Benjamin Renard   Add common functi...
209
				fixRange(lEstimatedRange, lYAxis->_scale == Axis::Scale::LOGARITHMIC);
fbe3c2bb   Benjamin Renard   First commit
210
211
212
213
214
215
216
217
218
				lEstimatedRange._extend = lRange._extend;
				lAxisRange[lYAxis->_id] = lEstimatedRange;
			}

			// Set ZAxis range if a color param is defined for this serie
			if (lZAxis != nullptr)
			{
				if (!lSeriesProperties.getColorParamId().empty())
				{
08ec1dde   Benjamin Renard   Add propertie _us...
219
					lZAxis->_used = true;
fbe3c2bb   Benjamin Renard   First commit
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
					Range lRange(lZAxis->getRange());
					ParameterAxes* colorSerieParameterAxes = getParameterAxesByColorSerieId(lSeriesProperties.getColorSerieId());
					if (colorSerieParameterAxes == NULL)
						continue;
					ColorSeriesProperties& colorSerieProp = colorSerieParameterAxes->getColorSeriePropertiesById(lSeriesProperties.getColorSerieId());
					// If range status for this axis is set by the user do not update range "automatically".
					if (isnan(lRange.getMin()) && isnan(lRange.getMax()))
					{
						Range lEstimatedRange(lColorAxeRange);
						Range lParamIndexRange(
							(*_pParameterValues)[lSeriesProperties.getColorParamId()].getMin(
								colorSerieProp.getIndex()),
							(*_pParameterValues)[lSeriesProperties.getColorParamId()].getMax(
								colorSerieProp.getIndex()));

						if (isnan(lEstimatedRange.getMin()) && isnan(lEstimatedRange.getMax())) {
							lEstimatedRange.setMin(lParamIndexRange.getMin());
							lEstimatedRange.setMax(lParamIndexRange.getMax());
						} else {
							lEstimatedRange.setMin(std::min(lEstimatedRange.getMin(), lParamIndexRange.getMin()));
							lEstimatedRange.setMax(std::max(lEstimatedRange.getMax(), lParamIndexRange.getMax()));
						}
faf4d845   Benjamin Renard   Add common functi...
242
						fixRange(lEstimatedRange, lZAxis->_scale == Axis::Scale::LOGARITHMIC);
fbe3c2bb   Benjamin Renard   First commit
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
						lEstimatedRange._extend = lRange._extend;
						lColorAxeRange = lEstimatedRange;
					}
				}
			}
		}
	}


	// Update range of axis. Done after because, axis range may be processed in several pass (one for each series)
	for (auto lAxis: lAxisRange) {
		_panel->getAxis(lAxis.first)->setRange(lAxis.second);
	}

	if (lZAxis != nullptr && lColorAxeRange.isSet())
		lZAxis->setRange(lColorAxeRange);
}

void EpochPlot::configureAxisLegend() {
	// Y axis
	AxisLegendManager::configureYAxisLegendForSeries(this);

	// Z axis
	AxisLegendManager::configureColorAxisLegendForSeries(this);
}

void EpochPlot::drawXAxis(boost::shared_ptr<Axis> pXAxis, PlWindow& pPlWindow, Bounds& pPlotAreaSize, TickConf& pTickConf){
	LOG4CXX_DEBUG(gLogger, "Drawing X axis ");
	// draw main axis...
	PanelPlotOutput::drawXAxis(pXAxis,pPlWindow, pPlotAreaSize, pTickConf);
	// delegate to decorator any extra drawing stuff it needs to perform...
	_epochDecoratorPtr->draw(this,getEpochAxis(), _pls);
}

void EpochPlot::drawSeries(double startDate, double stopDate, int intervalIndex, std::string pParamId,
		SeriesProperties& pSeries,
		AMDA::Common::ParameterIndexComponent pParamIndex, ParameterAxes& param,
		bool moreThanOneSerieForAxis) {
	LOG4CXX_DEBUG(gLogger, "Drawing epoch serie for parameter "<<pParamId<<"["<<pParamIndex.getDim1Index()<<","<<pParamIndex.getDim2Index()<<"]");
	// This will configure window, draw axes (if needed) and legend of axes.
	PanelPlotOutput::drawSeries(startDate, stopDate, intervalIndex, pParamId, pSeries, pParamIndex, param, moreThanOneSerieForAxis);

	if(!pSeries.hasYAxis())
		return;

	// Y axis may be missing (tickplot for example)
	std::string	yAxisId = pSeries.getYAxisId();
	if (yAxisId.empty())
		return;

	EpochAxis* epochAxis = getEpochAxis();

	//get center time data for this interval
	std::vector<std::string>& crtCenterTimeData = _parameterManager.getInputIntervalDataList(intervalIndex, _centerTimeId);
db4a6b56   Benjamin Renard   Get the possibili...
297
298

	double crtCenterTime = 0.;
fbe3c2bb   Benjamin Renard   First commit
299
	if (crtCenterTimeData.empty())
db4a6b56   Benjamin Renard   Get the possibili...
300
301
302
		crtCenterTime = startDate + (stopDate - startDate) / 2.;
	else
		crtCenterTime  = AMDA::TimeUtil::readTimeInIso((char*)crtCenterTimeData[0].c_str());
fbe3c2bb   Benjamin Renard   First commit
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

	//set the current center time to the epoch axis
	epochAxis->setCrtCenterTime(crtCenterTime);

	//get computed values
	double *computedValues	= NULL;
	double *timeValues      = NULL;
	int    nbValues;
	if (!getComputedValuesFromSerieAndInterval(startDate, stopDate, pSeries, pParamIndex,
			&computedValues, &timeValues, nbValues))
	{
		LOG4CXX_DEBUG(gLogger, "EpochPlot::drawSeries - Cannot get computed values for serie");
		return;
	}

	//compute epoch time value
	double *epochTimeValues = epochAxis->getComputedValues(
			timeValues,
			nbValues,
			startDate,
			stopDate);

	double *coloredComputedValues = NULL;
	double *coloredTimeValues     = NULL;

	//get colored value if needed
	if (!pSeries.getColorParamId().empty() && (_panel->getColorAxis() != nullptr))
	{
		int nbColoredValues;
		if (!getColoredComputedValuesFromSerieAndInterval(startDate, stopDate, pSeries,
				&coloredComputedValues, &coloredTimeValues, nbColoredValues))
		{
			LOG4CXX_DEBUG(gLogger, "EpochPlot::drawSeries - Cannot get computed values for colored parameter");
			return;
		}
	}

	PlWindow lPlWindow = PlWindow(getEpochAxis()->getRange().getMin(), getEpochAxis()->getRange().getMax(),
			_panel->getAxis(yAxisId)->getRange().getMin(), _panel->getAxis(yAxisId)->getRange().getMax());

	_pls->wind(std::get<0>(lPlWindow), std::get<1>(lPlWindow),
				std::get<2>(lPlWindow), std::get<3>(lPlWindow));

	//draw serie
	Color lineColor   = getSerieLineColor(pSeries, moreThanOneSerieForAxis);
	Color symbolColor = getSerieSymbolColor(pSeries, lineColor);

	drawSymbols(
		pSeries.getSymbolProperties().getType(),
		pSeries.getSymbolProperties().getSize(), 1.,
		symbolColor,
		nbValues, epochTimeValues, computedValues, coloredComputedValues);

	drawLines(
		pSeries.getLineProperties().getType(),
		pSeries.getLineProperties().getStyle(),
		pSeries.getLineProperties().getWidth(),
		lineColor,
		nbValues, epochTimeValues, computedValues, coloredComputedValues);

	addSerieToParamsLegend(pSeries,pParamIndex,param._originalParamId, lineColor,symbolColor,startDate,stopDate,intervalIndex);

	delete[] computedValues;
	delete[] epochTimeValues;
	if (coloredComputedValues != NULL)
		delete[] coloredComputedValues;
}

const std::string EpochPlot::getXAxisId() const {
	// epoch plot can manage only one x axis.
	// search for its id

	for (auto param : _parameterAxesList)
	{
		//search epoch axis in color serie if exist
2fc1f2f8   Benjamin Renard   Full rework for s...
378
379
		if (!param.getYSeriePropertiesList().empty())
			return param.getYSeriePropertiesList().begin()->getXAxisId();
fbe3c2bb   Benjamin Renard   First commit
380
381
	}

8e99b602   Benjamin Renard   Function to parse...
382
	return "epochAxis";
fbe3c2bb   Benjamin Renard   First commit
383
384
385
386
387
388
389
390
391
}

void EpochPlot::setCenterTimeId(std::string centerTimeId)
{
	_centerTimeId = centerTimeId;
}


} /* namespace plot */