Blame view

src/ParamOutputImpl/Plot/Scatter/XYPlot.cc 39 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
/*
 * XYPlot.cc
 *
 *  Created on: 29 oct. 2013
 *      Author: CS
 */

#include <fstream>
#include <iomanip>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>

#include "XYPlot.hh"
#include "ParamsNode.hh"
#include "AxesNode.hh"
#include "PlotLogger.hh"
#include "PlotOutput.hh"
#include "TimeUtil.hh"
#include "ParamMgr.hh"
#include "AxisLegendManager.hh"

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

namespace plot {

XYPlot::XYPlot(AMDA::Parameters::ParameterManager& manager,
		boost::shared_ptr<Panel> panel) :
		PanelPlotOutput(manager, panel), _isIsotropic(false) {

}

XYPlot::~XYPlot() {
}

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

	// Configure range of series and color.
	configureSeriesAxis();
	configureAxisLegend();

	// Configure params legend
	configureParamsLegend(startTime,stopTime,intervalIndex);

	// If panel title is empty, replace it with start and end date.
f6eaec4e   Benjamin Renard   Optimize plot ele...
56
	if (_panel->getTitle()->_text.empty() || _panel->_updateTitleOnNextInterval) {
fbe3c2bb   Benjamin Renard   First commit
57
58
59
		//Title must be updated during the next interval plot
		_panel->_updateTitleOnNextInterval = true;
		// Set start date and end date.
904d4160   Hacene SI HADJ MOHAND   year 4 digits
60
		std::string lTimeFormat("%H:%M %d/%m/%Y");
fbe3c2bb   Benjamin Renard   First commit
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

		long int lStartTime = static_cast<long int>(startTime);
		tm * lStartTimeTm = gmtime(&lStartTime);
		char lStartTimeChr[80];

		// Format date.
		strftime(lStartTimeChr, 80, lTimeFormat.c_str(), lStartTimeTm);

		long int lStopTime = static_cast<long int>(stopTime);
		tm * lStopTimeTm = gmtime(&lStopTime);
		char lStopTimeChr[80];

		// Format date.
		strftime(lStopTimeChr, 80, lTimeFormat.c_str(), lStopTimeTm);

f6eaec4e   Benjamin Renard   Optimize plot ele...
76
77
		std::string titleText = std::string(lStartTimeChr) + " - " + std::string(lStopTimeChr);
		_panel->setTitleText(titleText.c_str());
fbe3c2bb   Benjamin Renard   First commit
78
79
80
81
82
83
84
85
86
87
88
	}

	PanelPlotOutput::preparePlotArea(startTime,stopTime,intervalIndex);
}

void XYPlot::createParameters(std::list<std::string>& usedParametersId_)
{

	for (ParameterAxesList::iterator it = _parameterAxesList.begin();
				it != _parameterAxesList.end(); ++it)
	{
2fc1f2f8   Benjamin Renard   Full rework for s...
89
		std::vector<SeriesProperties>::iterator ity;
fbe3c2bb   Benjamin Renard   First commit
90
91
92
93
94
95
		AMDA::Parameters::ParameterSPtr originalYParam =
			_parameterManager.getParameter(it->_originalParamId);

		double samplingYValue = 0.;
		double samplingXValue = 0.;

2fc1f2f8   Benjamin Renard   Full rework for s...
96
		for (ity = it->getYSeriePropertiesList().begin(); ity != it->getYSeriePropertiesList().end();
fbe3c2bb   Benjamin Renard   First commit
97
98
				++ity)
		{
2fc1f2f8   Benjamin Renard   Full rework for s...
99
			ParameterAxes* xSerieParameterAxes = getParameterAxesByXSerieId(ity->getXId());
fbe3c2bb   Benjamin Renard   First commit
100

c46af5a8   Benjamin Renard   Implements multi ...
101
102
103
104
			if (xSerieParameterAxes == NULL) {
				continue;
			}

2fc1f2f8   Benjamin Renard   Full rework for s...
105
			XSeriesProperties& xSerie = xSerieParameterAxes->getXSeriePropertiesById(ity->getXId());
c46af5a8   Benjamin Renard   Implements multi ...
106
			AMDA::Parameters::ParameterSPtr originalXSerieParam = _parameterManager.getParameter(xSerieParameterAxes->_originalParamId);
fbe3c2bb   Benjamin Renard   First commit
107

2fc1f2f8   Benjamin Renard   Full rework for s...
108
			ParameterAxes* colorSerieParameterAxes = getParameterAxesByColorSerieId(ity->getColorSerieId());
fbe3c2bb   Benjamin Renard   First commit
109
110
111
112
113
114
115
116

			AMDA::Parameters::ParameterSPtr originalColorParam;
			if (colorSerieParameterAxes != NULL)
				originalColorParam = _parameterManager.getParameter(colorSerieParameterAxes->_originalParamId);

			AMDA::Parameters::ParameterSPtr usedXParam;
			AMDA::Parameters::ParameterSPtr usedYParam;

2fc1f2f8   Benjamin Renard   Full rework for s...
117
			if (!ity->getComputeExpression().empty())
fbe3c2bb   Benjamin Renard   First commit
118
119
			{
				if (samplingXValue == 0.)
c46af5a8   Benjamin Renard   Implements multi ...
120
					samplingXValue = getSamplingInTreeParameter(originalXSerieParam);
fbe3c2bb   Benjamin Renard   First commit
121

2fc1f2f8   Benjamin Renard   Full rework for s...
122
				double correctedSamplingValue = getCorrectedSamplingValue(ity->getMaxResolution(), samplingXValue);
fbe3c2bb   Benjamin Renard   First commit
123
124
125
126

				if (abs(samplingXValue - correctedSamplingValue) > 1.)
				{
					//create resampling parameter for xparam
c46af5a8   Benjamin Renard   Implements multi ...
127
					usedXParam = createSampledParameter(originalXSerieParam, correctedSamplingValue);
fbe3c2bb   Benjamin Renard   First commit
128
129
130
				}
				else
				{
c46af5a8   Benjamin Renard   Implements multi ...
131
					usedXParam = originalXSerieParam;
fbe3c2bb   Benjamin Renard   First commit
132
133
134
135
136
				}

				if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedXParam->getId()) == usedParametersId_.end())
					usedParametersId_.push_back(usedXParam->getId());

2fc1f2f8   Benjamin Renard   Full rework for s...
137
				std::string expr = ity->getComputeExpression();
fbe3c2bb   Benjamin Renard   First commit
138
139
140
				boost::replace_all(expr, OrbitParamComponentName, usedXParam->getId());

				//create parameter from expression
854a7fcf   Benjamin Renard   First implementat...
141
				usedYParam = _parameterManager.getParameterFromExpression(expr, usedXParam->getGapThreshold(), true);
fbe3c2bb   Benjamin Renard   First commit
142
143
144
145
146
147
148
149

				if (usedYParam == nullptr)
				{
					LOG4CXX_ERROR(gLogger, "XYPlot::createParameters - Cannot create parameter from expression " << expr);
					continue;
				}

				AMDA::Info::ParamInfoSPtr paramYInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(usedYParam->getId(),true);
c46af5a8   Benjamin Renard   Implements multi ...
150
				AMDA::Info::ParamInfoSPtr paramXInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(originalXSerieParam->getInfoId(),true);
fbe3c2bb   Benjamin Renard   First commit
151
152
				if (paramYInfo != nullptr)
				{
2fc1f2f8   Benjamin Renard   Full rework for s...
153
					paramYInfo->setShortName(ity->getComputeExpressionName());
fbe3c2bb   Benjamin Renard   First commit
154
155
156
157
158
159
160
161
162
163
164
					if (paramXInfo != nullptr)
					{
						paramYInfo->setUnits(paramXInfo->getUnits());
						paramYInfo->setCoordinatesSystem(paramXInfo->getCoordinatesSystem());
					}
				}

				if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedYParam->getId()) == usedParametersId_.end())
					usedParametersId_.push_back(usedYParam->getId());

				//link serie to this resampled parameter
2fc1f2f8   Benjamin Renard   Full rework for s...
165
				ity->setParamId(usedYParam->getId());
c46af5a8   Benjamin Renard   Implements multi ...
166
				xSerie.setParamId(usedXParam->getId());
fbe3c2bb   Benjamin Renard   First commit
167
168
169
170
171
172
173
174

				if (originalColorParam != nullptr)
				{
					AMDA::Parameters::ParameterSPtr usedColorParam = createSampledParameterUnderReferenceParameter(originalColorParam, usedYParam);
					//Add used color parameter to parameters list
					if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedColorParam->getId()) == usedParametersId_.end())
						usedParametersId_.push_back(usedColorParam->getId());
					//link this color parameter to the y serie
2fc1f2f8   Benjamin Renard   Full rework for s...
175
					ity->setColorParamId(usedColorParam->getId());
fbe3c2bb   Benjamin Renard   First commit
176
					//link the used parameter to the color serie
2fc1f2f8   Benjamin Renard   Full rework for s...
177
					colorSerieParameterAxes->getColorSeriePropertiesById(ity->getColorSerieId()).addParamId(usedYParam->getId(), usedColorParam->getId());
fbe3c2bb   Benjamin Renard   First commit
178
					//activate the Z Axis
2fc1f2f8   Benjamin Renard   Full rework for s...
179
					ity->setZAxis(true);
fbe3c2bb   Benjamin Renard   First commit
180
181
182
183
184
185
186
187
188
189
				}

				usedYParam.reset();

				continue;
			}

			if (samplingYValue == 0.)
				samplingYValue = getSamplingInTreeParameter(originalYParam);

c46af5a8   Benjamin Renard   Implements multi ...
190
			samplingXValue = getSamplingInTreeParameter(originalXSerieParam);
2fc1f2f8   Benjamin Renard   Full rework for s...
191
			switch (ity->getResamplingProperties().getType())
fbe3c2bb   Benjamin Renard   First commit
192
193
194
195
196
			{
			case ResamplingType::MANUAL :
				{
					LOG4CXX_DEBUG(gLogger, "XYPlot::createSeriesParameters - ResamplingType::MANUAL");
					//create resampling parameters for xparam
2fc1f2f8   Benjamin Renard   Full rework for s...
197
					usedXParam = createSampledParameter(originalXSerieParam, ity->getResamplingProperties().getValue());
fbe3c2bb   Benjamin Renard   First commit
198
199
					if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedXParam->getId()) == usedParametersId_.end())
						usedParametersId_.push_back(usedXParam->getId());
c46af5a8   Benjamin Renard   Implements multi ...
200
					if (originalXSerieParam == originalYParam)
fbe3c2bb   Benjamin Renard   First commit
201
202
203
204
205
206
207
					{
						//same original parameter => re-use xparam
						usedYParam = usedXParam;
					}
					else
					{
						//create resampling parameters for xparam
2fc1f2f8   Benjamin Renard   Full rework for s...
208
						usedYParam = createSampledParameter(originalYParam, ity->getResamplingProperties().getValue());
fbe3c2bb   Benjamin Renard   First commit
209
210
211
212
213
214
215
216
217
						if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedYParam->getId()) == usedParametersId_.end())
							usedParametersId_.push_back(usedYParam->getId());
					}
				}
				break;
			case ResamplingType::AUTO   :
			case ResamplingType::XPARAM :
				{
					LOG4CXX_DEBUG(gLogger, "XYPlot::createSeriesParameters - ResamplingType::XPARAM");
2fc1f2f8   Benjamin Renard   Full rework for s...
218
					double correctedSamplingValue = getCorrectedSamplingValue(ity->getMaxResolution(), samplingXValue);
fbe3c2bb   Benjamin Renard   First commit
219
220
221
					if (abs(samplingXValue - correctedSamplingValue) > 1.)
					{
						//create resampling parameter for xparam
c46af5a8   Benjamin Renard   Implements multi ...
222
						usedXParam = createSampledParameter(originalXSerieParam, correctedSamplingValue);
fbe3c2bb   Benjamin Renard   First commit
223
224
						if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedXParam->getId()) == usedParametersId_.end())
							usedParametersId_.push_back(usedXParam->getId());
c46af5a8   Benjamin Renard   Implements multi ...
225
						if (originalXSerieParam == originalYParam)
fbe3c2bb   Benjamin Renard   First commit
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
						{
							//same original parameter => re-use xparam
							usedYParam = usedXParam;
						}
						else
						{
							//create resampling parameter for yparam
							usedYParam = createSampledParameter(originalYParam, correctedSamplingValue);
							if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedYParam->getId()) == usedParametersId_.end())
								usedParametersId_.push_back(usedYParam->getId());
						}
					}
					else
					{
						//get original parameter for xparam
c46af5a8   Benjamin Renard   Implements multi ...
241
						usedXParam = originalXSerieParam;
fbe3c2bb   Benjamin Renard   First commit
242
243
						if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedXParam->getId()) == usedParametersId_.end())
							usedParametersId_.push_back(usedXParam->getId());
c46af5a8   Benjamin Renard   Implements multi ...
244
						if (originalXSerieParam == originalYParam)
fbe3c2bb   Benjamin Renard   First commit
245
246
247
248
249
250
251
						{
							//same original parameter => re-use xparam
							usedYParam = usedXParam;
						}
						else
						{
							//create resampled parameter under times of xparam for yparam
c46af5a8   Benjamin Renard   Implements multi ...
252
							usedYParam = createSampledParameterUnderReferenceParameter(originalYParam, originalXSerieParam);
fbe3c2bb   Benjamin Renard   First commit
253
254
255
256
257
258
259
260
							if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedYParam->getId()) == usedParametersId_.end())
								usedParametersId_.push_back(usedYParam->getId());
						}
					}
				}
				break;
			case ResamplingType::YPARAM :
				LOG4CXX_DEBUG(gLogger, "XYPlot::createSeriesParameters - ResamplingType::YPARAM");
2fc1f2f8   Benjamin Renard   Full rework for s...
261
				double correctedSamplingValue = getCorrectedSamplingValue(ity->getMaxResolution(), samplingYValue);
fbe3c2bb   Benjamin Renard   First commit
262
263
264
				if (abs(samplingYValue - correctedSamplingValue) > 1.)
				{
					//create resampling parameter for xparam
c46af5a8   Benjamin Renard   Implements multi ...
265
					usedXParam = createSampledParameter(originalXSerieParam, correctedSamplingValue);
fbe3c2bb   Benjamin Renard   First commit
266
267
					if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedXParam->getId()) == usedParametersId_.end())
						usedParametersId_.push_back(usedXParam->getId());
c46af5a8   Benjamin Renard   Implements multi ...
268
					if (originalXSerieParam == originalYParam)
fbe3c2bb   Benjamin Renard   First commit
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
					{
						//same original parameter => re-use xparam
						usedYParam = usedXParam;
					}
					else
					{
						//create resampling parameter for yparam
						usedYParam = createSampledParameter(originalYParam, correctedSamplingValue);
						if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedYParam->getId()) == usedParametersId_.end())
							usedParametersId_.push_back(usedYParam->getId());
					}
				}
				else
				{
					//get original parameter for yparam
					usedYParam = originalYParam;
					if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedYParam->getId()) == usedParametersId_.end())
						usedParametersId_.push_back(usedYParam->getId());
c46af5a8   Benjamin Renard   Implements multi ...
287
					if (originalXSerieParam == originalYParam)
fbe3c2bb   Benjamin Renard   First commit
288
289
290
291
292
293
294
					{
						//same original parameter => re-use yparam
						usedXParam = usedYParam;
					}
					else
					{
						//create resampled parameter under times of yparam for cparam
c46af5a8   Benjamin Renard   Implements multi ...
295
						usedXParam = createSampledParameterUnderReferenceParameter(originalXSerieParam, originalYParam);
fbe3c2bb   Benjamin Renard   First commit
296
297
298
299
300
301
302
303
						if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedXParam->getId()) == usedParametersId_.end())
							usedParametersId_.push_back(usedXParam->getId());
					}
				}
				break;
			}

			//link serie to this resampled parameter
2fc1f2f8   Benjamin Renard   Full rework for s...
304
			ity->setParamId(usedYParam->getId());
c46af5a8   Benjamin Renard   Implements multi ...
305
			xSerie.setParamId(usedXParam->getId());
fbe3c2bb   Benjamin Renard   First commit
306
307
308
309
310
311
312
313

			if (originalColorParam != nullptr)
			{
				AMDA::Parameters::ParameterSPtr usedColorParam = createSampledParameterUnderReferenceParameter(originalColorParam, usedYParam);
				//Add used color parameter to parameters list
				if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedColorParam->getId()) == usedParametersId_.end())
					usedParametersId_.push_back(usedColorParam->getId());
				//link this color parameter to the y serie
2fc1f2f8   Benjamin Renard   Full rework for s...
314
				ity->setColorParamId(usedColorParam->getId());
fbe3c2bb   Benjamin Renard   First commit
315
				//link the used parameter to the color serie
2fc1f2f8   Benjamin Renard   Full rework for s...
316
				colorSerieParameterAxes->getColorSeriePropertiesById(ity->getColorSerieId()).addParamId(usedYParam->getId(), usedColorParam->getId());
fbe3c2bb   Benjamin Renard   First commit
317
				//activate the Z Axis
2fc1f2f8   Benjamin Renard   Full rework for s...
318
				ity->setZAxis(true);
fbe3c2bb   Benjamin Renard   First commit
319
320
321
322
323
			}
		}
	}
}

fbe3c2bb   Benjamin Renard   First commit
324
325
326
327
328
329
330
331
332
/*
 * Dumps properties for test.
 */
void XYPlot::dump(std::ostream& out_){
	PanelPlotOutput::dump(out_);
	out_ << "isotropic=" << std::boolalpha << _isIsotropic;
}


8c860e4f   Benjamin Renard   Rework for tickpl...
333
334
void XYPlot::calculatePlotArea(const Bounds& panelBounds_, Bounds& bounds_) {
	PanelPlotOutput::calculatePlotArea(panelBounds_, bounds_);
fbe3c2bb   Benjamin Renard   First commit
335
336

	if (_isIsotropic) {
f6eaec4e   Benjamin Renard   Optimize plot ele...
337
338
339
		// Get bounds of panel
		Bounds lPanelBounds(_panel->getBoundsInPlPage());

fbe3c2bb   Benjamin Renard   First commit
340
341
342
343
344
345
346
347
348
349
		// Parse each parameter to define size of axis.
		double lHorizontalAxisGap = 0;
		Range lHorizontalRange;
		double lVerticalAxisGap = 0;
		Range lVerticalRange;
		SeriesProperties lSeriesProperties;
		boost::shared_ptr<Axis> lYAxis;
		boost::shared_ptr<Axis> lXAxis;
		for (auto param: _parameterAxesList) {
			// Get Y axis
2fc1f2f8   Benjamin Renard   Full rework for s...
350
			for(auto lSeriesProperties: param.getYSeriePropertiesList()) {
fbe3c2bb   Benjamin Renard   First commit
351
352
353
354
355
356
357
358
				lYAxis = _panel->getAxis(lSeriesProperties.getYAxisId());
				if (lYAxis.get() == nullptr) {
					std::stringstream lError;
					lError << "XYPlot::calculatePlotArea" << ": Y axis with id '" << lSeriesProperties.getYAxisId() << "' not found.";
					BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
				}

				lVerticalRange = lYAxis->getRange();
6fcde196   Hacene SI HADJ MOHAND   ok pour scatter plot
359
                               
fbe3c2bb   Benjamin Renard   First commit
360
361
362
363
364
365
				// Get gap for this parameter data
				if (lVerticalRange.isSet()) {
					double lTmpVerticalAxisGap = fabs(lVerticalRange.getMin() - lVerticalRange.getMax());
					// We get the maximum gap in case where there are several parameter to draw on Y axis(axes).
					lVerticalAxisGap = std::max(lVerticalAxisGap, lTmpVerticalAxisGap);
				}
fbe3c2bb   Benjamin Renard   First commit
366

c46af5a8   Benjamin Renard   Implements multi ...
367
				lXAxis = _panel->getAxis(lSeriesProperties.getXAxisId());
fbe3c2bb   Benjamin Renard   First commit
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
				if (lXAxis.get() == nullptr) {
					std::stringstream lError;
					lError << "XYPlot::calculatePlotArea" << ": X axis with id '" << lSeriesProperties.getXAxisId() << "' not found.";
					BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
				}

				lHorizontalRange = lXAxis->getRange();
				// Get gap for this parameter data
				if (lHorizontalRange.isSet()) {
					double lTmpHorizontalAxisGap = fabs(lHorizontalRange.getMin() - lHorizontalRange.getMax());
					lHorizontalAxisGap = std::max(lHorizontalAxisGap, lTmpHorizontalAxisGap);
				}
			}

			// If one of gap is 0 do nothing (gap of range can't be 0).
			if ((lVerticalAxisGap == 0) || (lHorizontalAxisGap == 0))
				continue;

			double lRequestedRatio = lVerticalAxisGap / lHorizontalAxisGap;
			double lPlotAreaRatio = bounds_._height / bounds_._width;
			double pageWidth = std::get < 1 > (_panel->_page->getSize());
			double pageHeight = std::get < 0 > (_panel->_page->getSize());
			double lPageRatio = 0;

			// Page width and height doesn't depend on orientation...
			// so we computes page ratio depending on orientation...
			if (_panel->_page->_orientation == PlotCommon::Orientation::LANDSCAPE)
				lPageRatio = pageWidth / pageHeight;
			else
				lPageRatio = pageHeight / pageWidth;

fbe3c2bb   Benjamin Renard   First commit
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
			// Calculate width of plot area to keep uniformity of the two scale (axes).
			double lPlotAreaIsoW = ((bounds_._height * lPageRatio) / lRequestedRatio);
			// Calculate height of plot area to keep uniformity of the two scale (axes).
			double lPlotAreaIsoH = ((bounds_._width / lPageRatio) * lRequestedRatio);

			// When ratio is lower than 1. it signifies that plot area width is bigger than height.
			// So reduce width.
			// The upper condition is only valid if new width is purely lower than current plot area width.
			// Otherwise we must modify height.
			if ( ((lPlotAreaRatio < 1.) && (lPlotAreaIsoW < bounds_._width)) ||
  				 ((lPlotAreaRatio > 1.) && (lPlotAreaIsoH > bounds_._height))) {

				// Calculate X min bounds of plot area if it can be centered.
				double lPlotAreaIsoX = ((lPanelBounds._width - lPlotAreaIsoW) / 2.) + lPanelBounds._x;

				// Check if plot area can be centered.
				if (lPlotAreaIsoX  > bounds_._x) {
					bounds_._x = lPlotAreaIsoX;
				} else {
					bounds_._x += (bounds_._width - lPlotAreaIsoW) / 2.;
				}
				bounds_._width = lPlotAreaIsoW;
			} else {
				// Calculate Y min bounds of plot area if it can be centered.
				double lPlotAreaIsoY = ((lPanelBounds._height - lPlotAreaIsoH) / 2.) + lPanelBounds._y;

				// Check if plot area can be centered.
				if (lPlotAreaIsoY  > bounds_._y) {
					bounds_._y = lPlotAreaIsoY;
				} else {
					bounds_._y += (bounds_._height - lPlotAreaIsoH) / 2.;
				}
				bounds_._height = lPlotAreaIsoH;
			}
		}
	}
}

std::string XYPlot::formatDateTime (double dateTime, const std::string &format) {
	long int lTime = static_cast<long int>(dateTime);
	tm * lTimeTm = gmtime(&lTime);

	// Format date.
	char timeBuf[80];
	strftime (timeBuf, sizeof (timeBuf), format.c_str(), lTimeTm);

	return std::string(timeBuf);
}

void XYPlot::drawTimeTicks(SeriesProperties& pSeries, AMDA::Common::ParameterIndexComponent pParamIndex,
		double* timeValues, int nbTimeValues) {
	LOG4CXX_DEBUG(gLogger, "XYPlot::drawTimeTicks");

	TimeTickProperties ttProps = pSeries.getTimeTickProperties();

	// Draw Time ticks if required !
	if ((ttProps.getStep().compare("0") == 0) && (ttProps.getNumber() == 0))
		return;

	// Get parameter sampling, data, start time and end time
c46af5a8   Benjamin Renard   Implements multi ...
459
	ParameterAxes* xparameter = getParameterAxesByXSerieId(pSeries.getXId());
fbe3c2bb   Benjamin Renard   First commit
460

c46af5a8   Benjamin Renard   Implements multi ...
461
	XSeriesProperties& xSerie = xparameter->getXSeriePropertiesById(pSeries.getXId());
fbe3c2bb   Benjamin Renard   First commit
462

c46af5a8   Benjamin Renard   Implements multi ...
463
	std::string paramId = pSeries.getParamId();
fbe3c2bb   Benjamin Renard   First commit
464

c46af5a8   Benjamin Renard   Implements multi ...
465
	ParameterData& xData = (*_pParameterValues)[xSerie.getParamId()];
fbe3c2bb   Benjamin Renard   First commit
466
467
468
469
470
	ParameterData& yData = (*_pParameterValues)[paramId];

	double startTime = timeValues[0];
	double stopTime  = timeValues[nbTimeValues-1];
	double totalDuration = stopTime - startTime;
fbe3c2bb   Benjamin Renard   First commit
471
472
473
474
475
476
477
478
479
480

//	LOG4CXX_DEBUG(gLogger, "drawTimeTicks totalDuration " << totalDuration);

	// Compute x and y coordinates for major and minor time ticks
	int nbTotMajor = 0;
	int nbTotMinor = 0;
	int nbMinorPerMajor = ttProps.getMinor();

	if (ttProps.getStep().compare("0") != 0)
	{
c6a67968   Benjamin Renard   Fix some violatio...
481
		double deltaMajorTick, deltaMinorTick;
fbe3c2bb   Benjamin Renard   First commit
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
		if (ttProps.getStep().compare("auto") == 0) {
//			LOG4CXX_DEBUG(gLogger, "drawTimeTicks auto");
			deltaMajorTick = TimeAxis::computeAutoMajorTickSpace (startTime, stopTime);
		}
		else {
//			LOG4CXX_DEBUG(gLogger, "drawTimeTicks time step");
			deltaMajorTick = DD_Time2Double (ttProps.getStep().c_str());
		}

		nbTotMajor = (int) (totalDuration / deltaMajorTick) + 1;
		deltaMinorTick = deltaMajorTick / (nbMinorPerMajor + 1);

		// Update totalDuration & stopTime depending on the nbMinorPerMajor value
		if (nbMinorPerMajor == 0) {
			nbTotMinor = 0;
			totalDuration = (nbTotMajor - 1) * deltaMajorTick;
			stopTime = startTime + totalDuration;
		}
		else {
			nbTotMinor = (nbTotMajor-1) * nbMinorPerMajor;
			nbTotMinor += (int) ((totalDuration - (nbTotMajor - 1) * deltaMajorTick) / deltaMinorTick);
			totalDuration = (nbTotMajor + nbTotMinor - 1) * deltaMinorTick;
			stopTime = startTime + totalDuration;
		}
//		LOG4CXX_DEBUG(gLogger, "drawTimeTicks totalDuration " << totalDuration);
	}
	else if (ttProps.getNumber() != 0) {
//		LOG4CXX_DEBUG(gLogger, "drawTimeTicks step number");
		nbTotMajor = ttProps.getNumber();
fbe3c2bb   Benjamin Renard   First commit
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
		nbTotMinor = (nbTotMajor-1) * nbMinorPerMajor;
	}

//	LOG4CXX_DEBUG(gLogger, "drawTimeTicks nbMajor " << nbTotMajor);
//	LOG4CXX_DEBUG(gLogger, "drawTimeTicks nbMinor " << nbTotMinor);
//	LOG4CXX_DEBUG(gLogger, "drawTimeTicks deltaMajorDuration " << deltaMajorTick);

	// Build & compute graduation
	double *majorX, *majorY, *minorX, *minorY, *tickLabel;

	majorX = new double [nbTotMajor];
	majorY = new double [nbTotMajor];
	minorX = new double [nbTotMinor];
	minorY = new double [nbTotMinor];
	tickLabel = new double [nbTotMajor];

	int nbGrad = nbTotMajor + nbTotMinor;
	int curMajorGrad = 0;
	int curMinorGrad = 0;

11080d60   Erdogan Furkan   Previous/next but...
531
532
533
	double prevTime = 0;
	double nextTime = 0;

fbe3c2bb   Benjamin Renard   First commit
534
535
536
537
538
539
540
	for (int grad=0; grad<nbGrad; grad++) {
		double curTime = startTime + grad * (totalDuration / (nbGrad - 1));

		// Compute major graduation pos & Record tick label (based on date)
		if ((grad%(nbMinorPerMajor+1)) == 0) {
			tickLabel [curMajorGrad] = curTime;

11080d60   Erdogan Furkan   Previous/next but...
541
542
			majorX [curMajorGrad] = xData.getInterpolatedValue (curTime, xSerie.getIndex(), prevTime,nextTime );
			majorY [curMajorGrad] = yData.getInterpolatedValue (curTime, pParamIndex, prevTime,nextTime );
fbe3c2bb   Benjamin Renard   First commit
543
544
545
546
			curMajorGrad++;
		}
		// Compute minor graduation pos
		else {
11080d60   Erdogan Furkan   Previous/next but...
547
548
			minorX [curMinorGrad] = xData.getInterpolatedValue (curTime, xSerie.getIndex(), prevTime,nextTime );
			minorY [curMinorGrad] = yData.getInterpolatedValue (curTime, pParamIndex, prevTime,nextTime );
fbe3c2bb   Benjamin Renard   First commit
549
550
551
552
553
			curMinorGrad++;
		}
	}

	// Draw first time tick major symbol, other time tick major Symbols and minor time ticks symbols
6fc1d86b   Benjamin Renard   Fix bug in ticks ...
554
555
556
557
558
559

	PlWindow lPlWindow = PlWindow(_panel->getAxis(pSeries.getXAxisId())->getRange().getMin(), _panel->getAxis(pSeries.getXAxisId())->getRange().getMax(),
	_panel->getAxis(pSeries.getYAxisId())->getRange().getMin(), _panel->getAxis(pSeries.getYAxisId())->getRange().getMax());

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

fbe3c2bb   Benjamin Renard   First commit
560
561
562
	if (nbTotMajor != 0) {
		drawSymbols(
			ttProps.getFirstSymbol().getType(),
dbcd9795   Benjamin Renard   Fix error on the ...
563
			ttProps.getFirstSymbol().getSize(), 1.,
fbe3c2bb   Benjamin Renard   First commit
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
			ttProps.getFirstSymbol().getColor(),
			1, majorX, majorY);

		drawSymbols(
			ttProps.getSymbol().getType(),
			ttProps.getSymbol().getSize(), 1.,
			ttProps.getSymbol().getColor(),
			nbTotMajor-1, &majorX[1], &majorY[1]);
	}

	if (nbTotMinor != 0) {
		drawSymbols(
			ttProps.getSymbol().getType(),
			ttProps.getSymbol().getSize(), 0.5,
			ttProps.getSymbol().getColor(),
			nbTotMinor, minorX, minorY);
	}

	// Draw text Label for major tick by building a list of textPlots
	if (nbTotMajor != 0) {
		TextPlots textPlots;
		TextPlot baseTextPlot;
		baseTextPlot._color = ttProps.getColor();
f6eaec4e   Benjamin Renard   Optimize plot ele...
587
		baseTextPlot.setFont(ttProps.getFont());
fbe3c2bb   Benjamin Renard   First commit
588
589
590
591

		std::string computedTimeFormat = getPlTimeFormat (std::string("hh:mm"), startTime, stopTime, nbTotMajor);
//		LOG4CXX_DEBUG(gLogger, "drawTimeTicks computedTimeFormat" << computedTimeFormat);

fbe3c2bb   Benjamin Renard   First commit
592
593
594
595
596
597
		Range lYRange = getYAxisRange (pSeries, _panel->getAxis(pSeries.getYAxisId()));

		double deltaX = 0; // (lXRange.getMax () - lXRange.getMin ()) / 80;
		double deltaY = (lYRange.getMax () - lYRange.getMin ()) / 80;

		for (int g=0; g<nbTotMajor; g++) {
6fc1d86b   Benjamin Renard   Fix bug in ticks ...
598
599
			if (isNAN(majorX[g]) || isNAN(majorY[g]))
				continue;
fbe3c2bb   Benjamin Renard   First commit
600
601
602
603
604
605
606
607
			boost::shared_ptr<TextPlot> textPlot (new TextPlot (baseTextPlot));
			textPlot->_x = boost::lexical_cast<std::string>(majorX [g] + deltaX);
			textPlot->_y = boost::lexical_cast<std::string>(majorY [g] + deltaY);
			textPlot->_text = formatDateTime (tickLabel [g], computedTimeFormat);

			textPlots.push_back (textPlot);
		}

fbe3c2bb   Benjamin Renard   First commit
608
		// Draw textPlots for major graduations
078ec265   Benjamin Renard   Give the possibil...
609
		drawTextPlots (_panel->getAxis(pSeries.getXAxisId()), _panel->getAxis(pSeries.getYAxisId()), lPlWindow, textPlots);
fbe3c2bb   Benjamin Renard   First commit
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
	}

	// Free major & minor graduations positions
	delete [] majorX;
	delete [] majorY;
	delete [] minorX;
	delete [] minorY;
	delete [] tickLabel;
}

void XYPlot::drawSeries(double startDate, double stopDate, int intervalIndex, std::string pParamId,
		SeriesProperties& pSeries, AMDA::Common::ParameterIndexComponent pParamIndex,
		ParameterAxes& param, bool moreThanOneSerieForAxis) {
	// This will configure window, draw axes (if needed) and legend of axes.

	LOG4CXX_DEBUG(gLogger, "XYPlot::drawSeries");

	PanelPlotOutput::drawSeries(startDate, stopDate, intervalIndex, pParamId, pSeries, pParamIndex, param, moreThanOneSerieForAxis);

	//get x serie values
	double* lXData = NULL;
	double* lXTime = NULL;
	int     nbXValues = 0;

	if (!getXComputedValuesFromSerieAndInterval(startDate, stopDate, pSeries,
			&lXData, &lXTime, nbXValues))
	{
		LOG4CXX_DEBUG(gLogger, "XYPlot::drawSeries - Cannot get computed values for x serie");
		return;
	}

	//get y serie values
	double* lYData = NULL;
	double* lYTime = NULL;
	int     nbYValues = 0;

	if (!getComputedValuesFromSerieAndInterval(startDate, stopDate, pSeries,
			pParamIndex, &lYData, &lYTime, nbYValues))
	{
		LOG4CXX_DEBUG(gLogger, "XYPlot::drawSeries - Cannot get computed values for y serie");
		return;
	}

fbe3c2bb   Benjamin Renard   First commit
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
	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, "XYPlot::drawSeries - Cannot get computed values for colored parameter");
			return;
		}
	}

	PlWindow lPlWindow = PlWindow(_panel->getAxis(pSeries.getXAxisId())->getRange().getMin(), _panel->getAxis(pSeries.getXAxisId())->getRange().getMax(),
			_panel->getAxis(pSeries.getYAxisId())->getRange().getMin(), _panel->getAxis(pSeries.getYAxisId())->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,
		nbYValues, lXData, lYData, coloredComputedValues);


	drawLines(
		pSeries.getLineProperties().getType(),
		pSeries.getLineProperties().getStyle(),
		pSeries.getLineProperties().getWidth(),
		lineColor,
		nbYValues, lXData, lYData, coloredComputedValues);

	// Draw eventual Time ticks
	drawTimeTicks(pSeries, pParamIndex, lYTime, nbYValues);

	//draw interval
	drawSerieInterval(pSeries,lXData,lYData,lYTime,nbYValues,intervalIndex);

	//add serie to param legend
	addSerieToParamsLegend(pSeries,pParamIndex,param._originalParamId, lineColor,symbolColor,startDate, stopDate, intervalIndex);

	delete[] lXData;
	delete[] lYData;
	if (coloredComputedValues != NULL)
		delete[] coloredComputedValues;
}

/**
 * Draws Curve
 */
void XYPlot::drawCurvePlot(CurvePlot &curvePlot)
{
	if (curvePlot.isDrawn())
		return;

	LOG4CXX_DEBUG(gLogger, "XYPlot::drawCurvePlot");

	//retrieve associated y serie properties
	for (ParameterAxesList::iterator it = _parameterAxesList.begin();
			it != _parameterAxesList.end(); ++it)
	{
2fc1f2f8   Benjamin Renard   Full rework for s...
721
722
		std::vector<SeriesProperties>::iterator ity;
		for (ity = it->getYSeriePropertiesList().begin(); ity != it->getYSeriePropertiesList().end();
fbe3c2bb   Benjamin Renard   First commit
723
724
				++ity)
		{
2fc1f2f8   Benjamin Renard   Full rework for s...
725
			if (curvePlot._serieId == ity->getId())
fbe3c2bb   Benjamin Renard   First commit
726
727
			{
				//get curve points
2fc1f2f8   Benjamin Renard   Full rework for s...
728
				int resolution = ity->getMaxResolution();
fbe3c2bb   Benjamin Renard   First commit
729
730
731
732
				if (resolution <= 0)
					resolution = 100;
				CurveFunctionWriter::CurvePointList pointList = curvePlot.getPointList(resolution);

6fc1d86b   Benjamin Renard   Fix bug in ticks ...
733
				if (pointList.empty()) {
fbe3c2bb   Benjamin Renard   First commit
734
					return; //nothing to plot
6fc1d86b   Benjamin Renard   Fix bug in ticks ...
735
				}
fbe3c2bb   Benjamin Renard   First commit
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755

				//prepare data in relation to axis of the associated serie
				double xData[pointList.size()];
				double yData[pointList.size()];
				double xMin = DBL_MAX;
				double xMax = -DBL_MAX;
				double yMin = DBL_MAX;
				double yMax = -DBL_MAX;
				int i = 0;
				for (auto point : pointList)
				{
					xData[i] = point.x;
					xMin = std::min(xMin,point.x);
					xMax = std::max(xMax,point.x);
					yData[i] = point.y;
					yMin = std::min(yMin,point.y);
					yMax = std::max(yMax,point.y);
					++i;
				}

2fc1f2f8   Benjamin Renard   Full rework for s...
756
				boost::shared_ptr<Axis> lXAxis(_panel->getAxis(ity->getXAxisId()));
6fc1d86b   Benjamin Renard   Fix bug in ticks ...
757
758
759
760
761
762
763
764
765

				if ((lXAxis == nullptr))
				{
					std::stringstream lError;
					lError << "XYPlot::drawCurvePlot : cannot retrieve X Axis";
					BOOST_THROW_EXCEPTION(
							PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
				}

2fc1f2f8   Benjamin Renard   Full rework for s...
766
				double* lXData = _panel->getAxis(ity->getXAxisId())->getComputedValues(
fbe3c2bb   Benjamin Renard   First commit
767
768
769
770
771
772
773
774
775
776
777
									xData,
									pointList.size(), xMin, xMax);

				if(lXData == NULL)
				{
					std::stringstream lError;
					lError << "XYPlot::drawCurvePlot : no value for x, check the request x parameter definition";
					BOOST_THROW_EXCEPTION(
							PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
				}

2fc1f2f8   Benjamin Renard   Full rework for s...
778
				boost::shared_ptr<Axis> lYAxis(_panel->getAxis(ity->getYAxisId()));
6fc1d86b   Benjamin Renard   Fix bug in ticks ...
779
780
781
782
783
784
785
786
787

				if ((lYAxis == nullptr))
				{
					std::stringstream lError;
					lError << "XYPlot::drawCurvePlot : cannot retrieve Y Axis";
					BOOST_THROW_EXCEPTION(
							PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
				}

2fc1f2f8   Benjamin Renard   Full rework for s...
788
				double* lYData = _panel->getAxis(ity->getYAxisId())->getComputedValues(
fbe3c2bb   Benjamin Renard   First commit
789
790
791
									yData,
									pointList.size(), yMin, yMax);

6fc1d86b   Benjamin Renard   Fix bug in ticks ...
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
				if(lYData == NULL)
				{
					std::stringstream lError;
					lError << "XYPlot::drawCurvePlot : no value for y, check the request y parameter definition";
					BOOST_THROW_EXCEPTION(
							PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
				}


				Range lXRange = lXAxis->getRange();
				Range lYRange = lYAxis->getRange();

				PlWindow lPlWindow = PlWindow(lXRange.getMin(), lXRange.getMax(), lYRange.getMin(), lYRange.getMax());

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

fbe3c2bb   Benjamin Renard   First commit
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
				//draw the curve
				drawLines(
					LineType::LINE,
					curvePlot._style,
					curvePlot._width,
					curvePlot._color,
					pointList.size(), lXData, lYData);

				curvePlot.setDrawn(true);

				//delete data
				delete [] lXData;
				delete [] lYData;
				return;
			}
		}
	}

	//cannot retrieve the associted y serie => exception
	std::stringstream lError;
	lError << "XYPlot::drawCurvePlots : y serie with id " << curvePlot._serieId << " not found.";
	BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
}

void XYPlot::configureSeriesAxis() {
	// map<X/YAxisId, automaticRange>
	std::map<std::string, Range> lAxisRangeMap;
	Range lColorAxeRange;

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

	SeriesProperties lSeriesProperties;
	// Parse each parameter to define on which axis to draw series.
	for (auto param: _parameterAxesList) {
fbe3c2bb   Benjamin Renard   First commit
842
843
844
		// Get number of series to draw
		// For each "Y" index of parameter identify on which axis series must be drawn.
		// Also configure X axis for range.
2fc1f2f8   Benjamin Renard   Full rework for s...
845
		for(auto lSeriesProperties: param.getYSeriePropertiesList()) {
fbe3c2bb   Benjamin Renard   First commit
846
847
848
849
850
851
			boost::shared_ptr<Axis> lYAxis = _panel->getAxis(lSeriesProperties.getYAxisId());
			if (lYAxis.get() == nullptr) {
				std::stringstream lError;
				lError << "XYPlot::configureSeriesAxis" << ": Y axis with id '" << lSeriesProperties.getYAxisId() << "' not found.";
				BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
			}
08ec1dde   Benjamin Renard   Add propertie _us...
852
			lYAxis->_used = true;
fbe3c2bb   Benjamin Renard   First commit
853
854
855
856
857
858
859
860

			// Configure range for Y axis.
			Range lYRange(lYAxis->getRange());
			// If range status for this axis is set by the user do not update range "automatically".
			if (isnan(lYRange.getMin()) && isnan(lYRange.getMax())) {
				ParameterData& yData = (*_pParameterValues)[lSeriesProperties.getParamId()];

				Range lEstimatedRange(lAxisRangeMap[lYAxis->_id]);
2fc1f2f8   Benjamin Renard   Full rework for s...
861
862
863
864
865
866
867
868
869
870
871
872
				for (auto index : lSeriesProperties.getIndexList(_pParameterValues)) {
					Range lParamIndexRange(
							yData.getMin(index), yData.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()));
					}
					fixRange(lEstimatedRange, lYAxis->_scale == Axis::Scale::LOGARITHMIC);
6fcde196   Hacene SI HADJ MOHAND   ok pour scatter plot
873
874
                                                                                                    if(lEstimatedRange.getMin()==lEstimatedRange.getMax())
                                                                                                                lEstimatedRange.setMargin(0.05);
fbe3c2bb   Benjamin Renard   First commit
875
				}
fbe3c2bb   Benjamin Renard   First commit
876
877
878
879
				lEstimatedRange._extend = lYRange._extend;
				lAxisRangeMap[lYAxis->_id] = lEstimatedRange;
			}

c46af5a8   Benjamin Renard   Implements multi ...
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
			//Set XAxis range
			boost::shared_ptr<Axis> lXAxis = _panel->getAxis(lSeriesProperties.getXAxisId());
			if (lXAxis.get() == nullptr) {
				std::stringstream lError;
				lError << "XYPlot::configureSeriesAxis" << ": X axis with id '" << lSeriesProperties.getXAxisId() << "' not found.";
				BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
			}
			lXAxis->_used = true;

			Range lXRange(lXAxis->getRange());
			// If user didn't set a range, calculate range automatically (based on parameters values).
			if (!lXRange.isSet()) {
				Range lEstimatedRange(lAxisRangeMap[lXAxis->_id]);

				ParameterAxes* xparameter = getParameterAxesByXSerieId(lSeriesProperties.getXId());
				XSeriesProperties& xSerie = xparameter->getXSeriePropertiesById(lSeriesProperties.getXId());
				ParameterData& xData = (*_pParameterValues)[xSerie.getParamId()];

				Range lRangeSampling(xData.getMin(xSerie.getIndex()), xData.getMax(xSerie.getIndex()));

				if (!lEstimatedRange.isSet()) {
					lEstimatedRange = lRangeSampling;
				} else {
					lEstimatedRange.setMin(std::min(lEstimatedRange.getMin(), lRangeSampling.getMin()));
					lEstimatedRange.setMax(std::max(lEstimatedRange.getMax(), lRangeSampling.getMax()));
				}
6fcde196   Hacene SI HADJ MOHAND   ok pour scatter plot
906
907
                                                                                if(lEstimatedRange.getMin()==lEstimatedRange.getMax())
                                                                                              lEstimatedRange.setMargin(0.05);
c46af5a8   Benjamin Renard   Implements multi ...
908
909
910
911
				fixRange(lEstimatedRange, lXAxis->_scale == Axis::Scale::LOGARITHMIC);
				lAxisRangeMap[lXAxis->_id] = lEstimatedRange;
			}

fbe3c2bb   Benjamin Renard   First commit
912
913
914
915
916
			// 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...
917
					lZAxis->_used = true;
fbe3c2bb   Benjamin Renard   First commit
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
					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()));
						}
6fcde196   Hacene SI HADJ MOHAND   ok pour scatter plot
939
940
                                                                                                                        if(lEstimatedRange.getMin()==lEstimatedRange.getMax())
                                                                                                                                        lEstimatedRange.setMargin(0.05);
faf4d845   Benjamin Renard   Add common functi...
941
						fixRange(lEstimatedRange, lZAxis->_scale == Axis::Scale::LOGARITHMIC);
fbe3c2bb   Benjamin Renard   First commit
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
						lEstimatedRange._extend = lRange._extend;
						lColorAxeRange = lEstimatedRange;
					}
				}
			}
		}
	}

	// Update range of axis.
	for (auto lAxis: lAxisRangeMap) {
		_panel->getAxis(lAxis.first)->setRange(lAxis.second);
	}

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

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

	// X axis
	AxisLegendManager::configureXAxisLegendForSeries(this);

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

/*
 * @brief Get computed X values (in relation with the x axis definition) for a serie and a time interval
 * Do not forget to delete computedValues !!
 * Don't delete timeValues !!
 */
bool XYPlot::getXComputedValuesFromSerieAndInterval(double startDate, double stopDate, SeriesProperties &rSeriesProperties,
		double** computedValues, double** timeValues, int& nbValues)
{
	LOG4CXX_DEBUG(gLogger, "XYPlot::getXComputedValuesFromSerieAndInterval");

c46af5a8   Benjamin Renard   Implements multi ...
980
	ParameterAxes* xSerieParameterAxes = getParameterAxesByXSerieId(rSeriesProperties.getXId());
fbe3c2bb   Benjamin Renard   First commit
981

c46af5a8   Benjamin Renard   Implements multi ...
982
	XSeriesProperties& xSerie = xSerieParameterAxes->getXSeriePropertiesById(rSeriesProperties.getXId());
fbe3c2bb   Benjamin Renard   First commit
983
984

	//get parameter x data for this serie
c46af5a8   Benjamin Renard   Implements multi ...
985
	ParameterData& xData = (*_pParameterValues)[xSerie.getParamId()];
fbe3c2bb   Benjamin Renard   First commit
986
987

	int startIndex;
8c71f50a   Benjamin Renard   Improve execution...
988
	xData.getIntervalBounds(startDate, stopDate, startIndex, nbValues);
fbe3c2bb   Benjamin Renard   First commit
989

8c71f50a   Benjamin Renard   Improve execution...
990
	if (nbValues == 0)
fbe3c2bb   Benjamin Renard   First commit
991
992
993
994
995
	{
		LOG4CXX_DEBUG(gLogger, "XYPlot::getXComputedValuesFromSerieAndInterval - Cannot find data for the x serie");
		return false;
	}

8c71f50a   Benjamin Renard   Improve execution...
996
997
	double *valuesInterval = xData.getValues(xSerie.getIndex(),startIndex);

fbe3c2bb   Benjamin Renard   First commit
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
	//get computed data for interval [startDate, stopDate] in relation with the serie y axis
	(*computedValues) = _panel->getAxis(rSeriesProperties.getXAxisId())->getComputedValues(
			valuesInterval,
			nbValues,
			xSerie.getMin(),
			xSerie.getMax());

	//get time values
	(*timeValues) = &xData.getTimes()[startIndex];

	return true;
}

/*
 * @overload PanelPlotOutput::getSerieParamsLegendString Return the associated params legend to a xy serie
 */
std::string XYPlot::getSerieParamsLegendString(SeriesProperties &rSeriesProperties,
			AMDA::Common::ParameterIndexComponent& index, std::string originalParamId)
{
	//retrieve x axis properties

c46af5a8   Benjamin Renard   Implements multi ...
1019
1020
	ParameterAxes* xparameter = getParameterAxesByXSerieId(rSeriesProperties.getXId());
	XSeriesProperties& xSerie = xparameter->getXSeriePropertiesById(rSeriesProperties.getXId());
fbe3c2bb   Benjamin Renard   First commit
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032

	// Retrieve ParamInfo Manager
	ParamMgr 		*piMgr =ParamMgr::getInstance();

	// Build parameter text legend depending on the availability of paramInfo
	// for Y and X component

	//For y component, re-use the text of the PanelPlotOutput
	std::stringstream paramLegendText;
	paramLegendText << PanelPlotOutput::getSerieParamsLegendString(rSeriesProperties,index,originalParamId);

	// Try to retrieve informations from paramInfo for Y componenet
c6a67968   Benjamin Renard   Fix some violatio...
1033
	//ParameterSPtr p = _parameterManager.getParameter(rSeriesProperties.getParamId());
fbe3c2bb   Benjamin Renard   First commit
1034
1035
1036
1037

	paramLegendText << " = f(";

	// Try to retrieve informations from paramInfo for X component
c46af5a8   Benjamin Renard   Implements multi ...
1038
	ParameterSPtr p = _parameterManager.getParameter(xparameter->_originalParamId);
fbe3c2bb   Benjamin Renard   First commit
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
	ParamInfoSPtr paramInfo = piMgr->getParamInfoFromId(p->getInfoId());

	// Build parameter text legend depending on the availability of paramInfo
	if (paramInfo)
	{
		if ((xSerie.getIndex().getDim1Index() == -1) && (xSerie.getIndex().getDim2Index() == -1))
		{
			// parameter legend text = short_name
			paramLegendText << paramInfo->getShortName();
		}
		else
		{
			if (paramInfo->getComponents(xSerie.getIndex()).empty() == false)
				// parameter legend text = components at index index]
				paramLegendText << paramInfo->getComponents(xSerie.getIndex());
			else
			{
				// parameter legend text = short_name [index]
				paramLegendText << paramInfo->getShortName() << "[" << xSerie.getIndex().getDim1Index();
				if (xSerie.getIndex().getDim2Index() != -1)
					paramLegendText << "," << xSerie.getIndex().getDim2Index();
				paramLegendText << "]";
			}
		}
	}
	else
	{
		if ((xSerie.getIndex().getDim1Index() == -1) && (xSerie.getIndex().getDim2Index() == -1))
			// parameter legend text = _originalParamId
c46af5a8   Benjamin Renard   Implements multi ...
1068
			paramLegendText << xparameter->_originalParamId;
fbe3c2bb   Benjamin Renard   First commit
1069
1070
1071
		else
		{
			// parameter legend text = _originalParamId [index]
c46af5a8   Benjamin Renard   Implements multi ...
1072
			paramLegendText << xparameter->_originalParamId << "[" << xSerie.getIndex().getDim1Index();
fbe3c2bb   Benjamin Renard   First commit
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
			if (xSerie.getIndex().getDim2Index() != -1)
				paramLegendText << "," << xSerie.getIndex().getDim2Index();
			paramLegendText << "]";
		}
	}

	paramLegendText << ")";

	return paramLegendText.str();
}

} /* namespace plot */