XYPlot.cc 44.7 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 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 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 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 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 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 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 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 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 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 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 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 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
/*
 * 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 "Axis.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.
	if (_panel->getTitle()->_text.empty() || _panel->_updateTitleOnNextInterval) {
		//Title must be updated during the next interval plot
		_panel->_updateTitleOnNextInterval = true;
		// Set start date and end date.
		std::string lTimeFormat("%Y/%m/%d %H:%M:%S");

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

		// Format date.
		strftime(lStartTimeChr, 80, lTimeFormat.c_str(), lStartTimeTm);
		std::string lStartTimeStr= lStartTimeChr;
		lStartTimeStr += '.'+std::to_string(startTime -lStartTime ).substr(2,3);

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

		// Format date.
		strftime(lStopTimeChr, 80, lTimeFormat.c_str(), lStopTimeTm);
		std::string lStopTimeStr= lStopTimeChr;
		lStopTimeStr += '.'+std::to_string(stopTime -lStopTime ).substr(2,3);

		std::string titleText = lStartTimeStr + " - " + lStopTimeStr;
		_panel->setTitleText(titleText.c_str());
	}

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


void XYPlot::getUsedParameters(AMDA::Parameters::ParameterSPtr originalXParam_,AMDA::Parameters::ParameterSPtr originalYParam_,
								AMDA::Parameters::ParameterSPtr originalZParam_, ResamplingProperties &resamplingProperties_,
								int maxResolution_,AMDA::Parameters::ParameterSPtr &usedXParam_,
								AMDA::Parameters::ParameterSPtr &usedYParam_,AMDA::Parameters::ParameterSPtr &usedZParam_)
{
	double samplingYValue = getSamplingInTreeParameter(originalYParam_);
	double samplingXValue = getSamplingInTreeParameter(originalXParam_);

	switch (resamplingProperties_.getType())
	{
	case ResamplingType::MANUAL :
		{
			LOG4CXX_DEBUG(gLogger, "XYPlot::getUsedParameters - ResamplingType::MANUAL");
			//create resampling parameters for xparam
			usedXParam_ = createSampledParameter(originalXParam_, resamplingProperties_.getValue());
			
			if (originalXParam_ == originalYParam_)
			{
				//same original parameter => re-use xparam
				usedYParam_ = usedXParam_;
			}
			else
			{
				//create resampling parameters for xparam
				usedYParam_ = createSampledParameter(originalYParam_, resamplingProperties_.getValue());
			}
		}
		break;
	case ResamplingType::AUTO   :
	case ResamplingType::XPARAM :
		{
			LOG4CXX_DEBUG(gLogger, "XYPlot::getUsedParameters - ResamplingType::XPARAM");
			double correctedSamplingValue = getCorrectedSamplingValue(maxResolution_, samplingXValue);
			if (abs(samplingXValue - correctedSamplingValue) > 1.)
			{
				//create resampling parameter for xparam
				usedXParam_ = createSampledParameter(originalXParam_, correctedSamplingValue);
				if (originalXParam_ == originalYParam_)
				{
					//same original parameter => re-use xparam
					usedYParam_ = usedXParam_;
				}
				else
				{
					//create resampling parameter for yparam
					usedYParam_ = createSampledParameter(originalYParam_, correctedSamplingValue);
				}
			}
			else
			{
				//get original parameter for xparam
				usedXParam_ = originalXParam_;
				if (originalXParam_ == originalYParam_)
				{
					//same original parameter => re-use xparam
					usedYParam_ = usedXParam_;
				}
				else
				{
					//create resampled parameter under times of xparam for yparam
					usedYParam_ = createSampledParameterUnderReferenceParameter(originalYParam_, originalXParam_);
				}
			}
		}
		break;
	case ResamplingType::YPARAM :
		LOG4CXX_DEBUG(gLogger, "XYPlot::getUsedParameters - ResamplingType::YPARAM");
		double correctedSamplingValue = getCorrectedSamplingValue(maxResolution_, samplingYValue);
		if (abs(samplingYValue - correctedSamplingValue) > 1.)
		{
			//create resampling parameter for xparam
			usedXParam_ = createSampledParameter(originalXParam_, correctedSamplingValue);
			if (originalXParam_ == originalYParam_)
			{
				//same original parameter => re-use xparam
				usedYParam_ = usedXParam_;
			}
			else
			{
				//create resampling parameter for yparam
				usedYParam_ = createSampledParameter(originalYParam_, correctedSamplingValue);
			}
		}
		else
		{
			//get original parameter for yparam
			usedYParam_ = originalYParam_;
			if (originalXParam_ == originalYParam_)
			{
				//same original parameter => re-use yparam
				usedXParam_ = usedYParam_;
			}
			else
			{
				//create resampled parameter under times of yparam for cparam
				usedXParam_ = createSampledParameterUnderReferenceParameter(originalXParam_, originalYParam_);
			}
		}
		break;
	}
	if (originalZParam_ != nullptr)
	{
		usedZParam_ = createSampledParameterUnderReferenceParameter(originalZParam_, usedYParam_);
	}
}

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

	for (ParameterAxesList::iterator it = _parameterAxesList.begin();
				it != _parameterAxesList.end(); ++it)
	{
		std::vector<SeriesProperties>::iterator ity;
		AMDA::Parameters::ParameterSPtr originalYParam =
			_parameterManager.getParameter(it->_originalParamId);

		double samplingXValue = 0.;
		AMDA::Parameters::ParameterSPtr usedXParam;
		AMDA::Parameters::ParameterSPtr usedYParam;
		AMDA::Parameters::ParameterSPtr usedColorParam;

		if(it->getHistogram2DSeriesProperties() != nullptr){
			ParameterAxes* xSerieParameterAxes = getParameterAxesByXSerieId(it->getHistogram2DSeriesProperties()->getXId());
			XSeriesProperties& xSerie = xSerieParameterAxes->getXSeriePropertiesById(it->getHistogram2DSeriesProperties()->getXId());
			AMDA::Parameters::ParameterSPtr originalXSerieParam = _parameterManager.getParameter(xSerieParameterAxes->_originalParamId);
			AMDA::Parameters::ParameterSPtr originalColorParam;

			if(it->getHistogram2DSeriesProperties()->getHistotypeProperties().getParamId() != ""){
				originalColorParam = _parameterManager.getParameter(it->getHistogram2DSeriesProperties()->getHistotypeProperties().getParamId());
			}

			getUsedParameters(originalXSerieParam,originalYParam,originalColorParam,it->getHistogram2DSeriesProperties()->getResamplingProperties(),-1,
								usedXParam,usedYParam,usedColorParam );

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

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

			if (originalColorParam != nullptr)
			{
				if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedColorParam->getId()) == usedParametersId_.end())
					usedParametersId_.push_back(usedColorParam->getId());

				it->getHistogram2DSeriesProperties()->getHistotypeProperties().setParamId(usedColorParam->getId());
				//link this color parameter to the y serie
				//ity->setColorParamId(usedColorParam->getId());
				//link the used parameter to the color serie
				//colorSerieParameterAxes->getColorSeriePropertiesById(ity->getColorSerieId()).addParamId(usedYParam->getId(), usedColorParam->getId());
				//activate the Z Axis
				
			}
			it->getHistogram2DSeriesProperties()->setZAxis(true);
			//link serie to this resampled parameter
			it->getHistogram2DSeriesProperties()->setParamId(usedYParam->getId());
			xSerie.setParamId(usedXParam->getId());
		}

		for (ity = it->getYSeriePropertiesList().begin(); ity != it->getYSeriePropertiesList().end();
				++ity)
		{
			ParameterAxes* xSerieParameterAxes = getParameterAxesByXSerieId(ity->getXId());

			if (xSerieParameterAxes == NULL) {
				continue;
			}

			XSeriesProperties& xSerie = xSerieParameterAxes->getXSeriePropertiesById(ity->getXId());
			AMDA::Parameters::ParameterSPtr originalXSerieParam = _parameterManager.getParameter(xSerieParameterAxes->_originalParamId);

			ParameterAxes* colorSerieParameterAxes = getParameterAxesByColorSerieId(ity->getColorSerieId());

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

			if (!ity->getComputeExpression().empty())
			{
				if (samplingXValue == 0.)
					samplingXValue = getSamplingInTreeParameter(originalXSerieParam);

				double correctedSamplingValue = getCorrectedSamplingValue(ity->getMaxResolution(), samplingXValue);

				if (abs(samplingXValue - correctedSamplingValue) > 1.)
				{
					//create resampling parameter for xparam
					usedXParam = createSampledParameter(originalXSerieParam, correctedSamplingValue);
				}
				else
				{
					usedXParam = originalXSerieParam;
				}

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

				std::string expr = ity->getComputeExpression();
				boost::replace_all(expr, OrbitParamComponentName, usedXParam->getId());

				//create parameter from expression
				usedYParam = _parameterManager.getParameterFromExpression(expr, usedXParam->getGapThreshold(), true);

				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);
				AMDA::Info::ParamInfoSPtr paramXInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(originalXSerieParam->getInfoId(),true);
				if (paramYInfo != nullptr)
				{
					paramYInfo->setShortName(ity->getComputeExpressionName());
					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
				ity->setParamId(usedYParam->getId());
				xSerie.setParamId(usedXParam->getId());

				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
					ity->setColorParamId(usedColorParam->getId());
					//link the used parameter to the color serie
					colorSerieParameterAxes->getColorSeriePropertiesById(ity->getColorSerieId()).addParamId(usedYParam->getId(), usedColorParam->getId());
					//activate the Z Axis
					ity->setZAxis(true);
				}

				usedYParam.reset();

				continue;
			}


			getUsedParameters(originalXSerieParam,originalYParam,originalColorParam,ity->getResamplingProperties(),ity->getMaxResolution(),
								usedXParam,usedYParam,usedColorParam );

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

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

			if (originalColorParam != nullptr)
			{
				if (std::find (usedParametersId_.begin(),usedParametersId_.end(),usedColorParam->getId()) == usedParametersId_.end())
					usedParametersId_.push_back(usedColorParam->getId());

				//link this color parameter to the y serie
				ity->setColorParamId(usedColorParam->getId());
				//link the used parameter to the color serie
				colorSerieParameterAxes->getColorSeriePropertiesById(ity->getColorSerieId()).addParamId(usedYParam->getId(), usedColorParam->getId());
				//activate the Z Axis
				ity->setZAxis(true);
			}

			//link serie to this resampled parameter
			ity->setParamId(usedYParam->getId());
			xSerie.setParamId(usedXParam->getId());
		}
	}
}

/*
 * Dumps properties for test.
 */
void XYPlot::dump(std::ostream& out_){
	PanelPlotOutput::dump(out_);
	out_ << "isotropic=" << std::boolalpha << _isIsotropic;
}

std::pair<double,double> XYPlot::getIsotropicGaps(std::string xAxisId,std::string yAxisId ){

	double lHorizontalAxisGap = 0;
	Range lHorizontalRange;
	double lVerticalAxisGap = 0;
	Range lVerticalRange;
	boost::shared_ptr<Axis> lYAxis;
	boost::shared_ptr<Axis> lXAxis;
	lYAxis = _panel->getAxis(yAxisId);
	if (lYAxis.get() == nullptr) {
		std::stringstream lError;
		lError << "XYPlot::getIsotropicGaps" << ": Y axis with id '" << yAxisId << "' not found.";
		BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
	}

	lVerticalRange = lYAxis->getRange();
					
	// 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);
	}

	lXAxis = _panel->getAxis(xAxisId);
	if (lXAxis.get() == nullptr) {
		std::stringstream lError;
		lError << "XYPlot::getIsotropicGaps" << ": X axis with id '" << xAxisId << "' 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);
	}

	return std::pair<double,double>(lHorizontalAxisGap,lVerticalAxisGap);
}

void XYPlot::calculatePlotArea(const Bounds& panelBounds_, Bounds& bounds_) {
	PanelPlotOutput::calculatePlotArea(panelBounds_, bounds_);

	if (_isIsotropic) {
		// Get bounds of panel
		Bounds lPanelBounds(_panel->getBoundsInPlPage());

		// Parse each parameter to define size of axis.
		SeriesProperties lSeriesProperties;
		std::pair<double,double> lAxisGaps;
		for (auto param: _parameterAxesList) {
			// Get Y axis
			for(auto lSeriesProperties: param.getYSeriePropertiesList()) {
				lAxisGaps = getIsotropicGaps(lSeriesProperties.getXAxisId(),lSeriesProperties.getYAxisId());
			}
			if(param.getHistogram2DSeriesProperties() != nullptr ){
				lAxisGaps = getIsotropicGaps(param.getHistogram2DSeriesProperties()->getXAxisId(),param.getHistogram2DSeriesProperties()->getYAxisId());
			}
			// If one of gap is 0 do nothing (gap of range can't be 0).
			if ((lAxisGaps.second == 0) || (lAxisGaps.first == 0))
				continue;

			double lRequestedRatio = lAxisGaps.second / lAxisGaps.first;
			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;

			// 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
	ParameterAxes* xparameter = getParameterAxesByXSerieId(pSeries.getXId());

	XSeriesProperties& xSerie = xparameter->getXSeriePropertiesById(pSeries.getXId());

	std::string paramId = pSeries.getParamId();

	ParameterData& xData = (*_pParameterValues)[xSerie.getParamId()];
	ParameterData& yData = (*_pParameterValues)[paramId];

	double startTime = timeValues[0];
	double stopTime  = timeValues[nbTimeValues-1];
	double totalDuration = stopTime - startTime;

//	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)
	{
		double deltaMajorTick, deltaMinorTick;
		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();
		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;

	double prevTime = 0;
	double nextTime = 0;

	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;

			majorX [curMajorGrad] = xData.getInterpolatedValue (curTime, xSerie.getIndex(), prevTime,nextTime );
			majorY [curMajorGrad] = yData.getInterpolatedValue (curTime, pParamIndex, prevTime,nextTime );
			curMajorGrad++;
		}
		// Compute minor graduation pos
		else {
			minorX [curMinorGrad] = xData.getInterpolatedValue (curTime, xSerie.getIndex(), prevTime,nextTime );
			minorY [curMinorGrad] = yData.getInterpolatedValue (curTime, pParamIndex, prevTime,nextTime );
			curMinorGrad++;
		}
	}

	// Draw first time tick major symbol, other time tick major Symbols and minor time ticks symbols

	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));

	if (nbTotMajor != 0) {
		drawSymbols(
			ttProps.getFirstSymbol().getType(),
			ttProps.getFirstSymbol().getSize(), 1.,
			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();
		baseTextPlot.setFont(ttProps.getFont());

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

		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++) {
			if (isNAN(majorX[g]) || isNAN(majorY[g]))
				continue;
			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);
		}

		// Draw textPlots for major graduations
		drawTextPlots (_panel->getAxis(pSeries.getXAxisId()), _panel->getAxis(pSeries.getYAxisId()), lPlWindow, textPlots);
	}

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

void XYPlot::drawHistogram2D(double startDate, double stopDate, std::string pParamId, Histogram2DSeriesProperties &pHistogram2DProperties){
	
	// Get X, Y and Z axis.
	boost::shared_ptr<Axis> lXAxis(_panel->getAxis(pHistogram2DProperties.getXAxisId()));
	boost::shared_ptr<Axis> lYAxis(_panel->getAxis(pHistogram2DProperties.getYAxisId()));
	//boost::shared_ptr<Axis> lZAxis(_panel->getAxis(pHistogram2DProperties.getZAxisId()));
	boost::shared_ptr<ColorAxis> lZAxis = _panel->getColorAxis();

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

	Color minValColor = lZAxis->getMinValColor();
	Color maxValColor = lZAxis->getMaxValColor();

	GridPart grid;
	MatrixGrid matrixGrid;
	int smoothFactor = pHistogram2DProperties.getHistotypeProperties().getSmoothFactor();

	unsigned int xBinNumber = pHistogram2DProperties.getManualProperties().getXBinNumber();
	unsigned int yBinNumber = pHistogram2DProperties.getManualProperties().getYBinNumber();

	

	double xBinSize = (lXRange.getMax() - lXRange.getMin())/ xBinNumber;
	double yBinSize = (lYRange.getMax() - lYRange.getMin())/ yBinNumber;

	//get parameter x data for this serie
	ParameterAxes* xSerieParameterAxes = getParameterAxesByXSerieId(pHistogram2DProperties.getXId());
	XSeriesProperties& xSerie = xSerieParameterAxes->getXSeriePropertiesById(pHistogram2DProperties.getXId());

	ParameterData &xData = (*_pParameterValues)[xSerie.getParamId()];
	ParameterData &yData = (*_pParameterValues)[pHistogram2DProperties.getParamId()];
	ParameterData &zData = (*_pParameterValues)[pHistogram2DProperties.getHistotypeProperties().getParamId()];


	int xStartIndex;
	int yStartIndex;
	int zStartIndex;
	int xNbValues;
	int yNbValues;
	int zNbValues;
	double zMin = NAN ; 
	double zMax = NAN ;
	
	xData.getIntervalBounds(startDate, stopDate, xStartIndex, xNbValues);
	yData.getIntervalBounds(startDate, stopDate, yStartIndex, yNbValues);
	zData.getIntervalBounds(startDate, stopDate, zStartIndex, zNbValues);

	double* xValues;
	double* yValues;
	double* zValues;
	if(lXAxis->_scale==Axis::Scale::LOGARITHMIC)
		xValues =  lXAxis->getComputedValues(xData.getValues(xSerie.getIndex(), xStartIndex),xNbValues,exp10(lXRange.getMin()), exp10(lXRange.getMax()));
	else
		xValues = xData.getValues(xSerie.getIndex(), xStartIndex);
	if(lYAxis->_scale==Axis::Scale::LOGARITHMIC)
		yValues =  lYAxis->getComputedValues(yData.getValues(pHistogram2DProperties.getIndex(), yStartIndex),yNbValues,exp10(lYRange.getMin()), exp10(lYRange.getMax()));
	else
		yValues = yData.getValues(pHistogram2DProperties.getIndex(), yStartIndex);
	
	zValues = zData.getValues(pHistogram2DProperties.getHistotypeProperties().getIndex(), zStartIndex);

	for (unsigned int i(0); i < xBinNumber; ++i)
	{
		for (unsigned int j(0); j < yBinNumber; ++j)
		{
			grid.x[0] =  lXRange.getMin()+xBinSize*i;
			grid.x[1] = grid.x[0]+xBinSize;
			grid.y[0] = lYRange.getMin() + yBinSize*j;
			grid.y[1] = grid.y[0]+yBinSize;
			grid.value = NAN;
			grid.isColorIndex= false;

			matrixGrid.push_back(grid);
		}
	}

	pHistogram2DProperties.getHistotypeProperties().getHisto2DFunction()->apply(matrixGrid, xValues, yValues, zValues, xNbValues,lXRange,lYRange,
													xBinNumber, yBinNumber, zMin, zMax, smoothFactor);
	if(lXAxis->_scale==Axis::Scale::LOGARITHMIC)
		free(xValues);
	if(lYAxis->_scale==Axis::Scale::LOGARITHMIC)
		free(yValues);
	
	if(std::isnan(lZRange.getMin()))
		lZRange.setMin(zMin);
	if(std::isnan(lZRange.getMax()))
		lZRange.setMax(zMax);
	
	lZRange._extend = lZAxis->isExtended();
	lZAxis->setRange(lZRange);

	PanelPlotOutput::drawHistogram2D(startDate,stopDate,pParamId, pHistogram2DProperties);
	PanelPlotOutput::drawMatrix(matrixGrid, zMin, zMax, minValColor,maxValColor, lZAxis->_color._colorMapIndex, false);
}

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;
	}

	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)
	{
		std::vector<SeriesProperties>::iterator ity;
		for (ity = it->getYSeriePropertiesList().begin(); ity != it->getYSeriePropertiesList().end();
				++ity)
		{
			if (curvePlot._serieId == ity->getId())
			{
				//get curve points
				int resolution = ity->getMaxResolution();
				if (resolution <= 0)
					resolution = 100;
				CurveFunctionWriter::CurvePointList pointList = curvePlot.getPointList(resolution);

				if (pointList.empty()) {
					return; //nothing to plot
				}

				//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;
				}

				boost::shared_ptr<Axis> lXAxis(_panel->getAxis(ity->getXAxisId()));

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

				double* lXData = _panel->getAxis(ity->getXAxisId())->getComputedValues(
									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()));
				}

				boost::shared_ptr<Axis> lYAxis(_panel->getAxis(ity->getYAxisId()));

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

				double* lYData = _panel->getAxis(ity->getYAxisId())->getComputedValues(
									yData,
									pointList.size(), yMin, yMax);

				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));

				//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) {
		if(param.getHistogram2DSeriesProperties() != nullptr){
			
			boost::shared_ptr<Axis> lYAxis = _panel->getAxis(param.getHistogram2DSeriesProperties()->getYAxisId());
			boost::shared_ptr<Axis> lXAxis = _panel->getAxis(param.getHistogram2DSeriesProperties()->getXAxisId());
			boost::shared_ptr<Axis> lZAxis = _panel->getAxis(param.getHistogram2DSeriesProperties()->getZAxisId());
			lXAxis->_used = true;
			lYAxis->_used = true;
			lZAxis->_used = true;
		}
		// 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.
		for(auto lSeriesProperties: param.getYSeriePropertiesList()) {
			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()));
			}
			lYAxis->_used = true;

			// 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]);
				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);
                                                                                                    if(lEstimatedRange.getMin()==lEstimatedRange.getMax())
                                                                                                                lEstimatedRange.setMargin(0.05);
				}
				lEstimatedRange._extend = lYRange._extend;
				lAxisRangeMap[lYAxis->_id] = lEstimatedRange;
			}

			//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()));
				}
                                                                                if(lEstimatedRange.getMin()==lEstimatedRange.getMax())
                                                                                              lEstimatedRange.setMargin(0.05);
				fixRange(lEstimatedRange, lXAxis->_scale == Axis::Scale::LOGARITHMIC);
				lAxisRangeMap[lXAxis->_id] = lEstimatedRange;
			}

			// Set ZAxis range if a color param is defined for this serie
			if (lZAxis != nullptr)
			{
				if (!lSeriesProperties.getColorParamId().empty())
				{
					lZAxis->_used = true;
					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()));
						}
                                                                                                                        if(lEstimatedRange.getMin()==lEstimatedRange.getMax())
                                                                                                                                        lEstimatedRange.setMargin(0.05);
						fixRange(lEstimatedRange, lZAxis->_scale == Axis::Scale::LOGARITHMIC);
						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");

	ParameterAxes* xSerieParameterAxes = getParameterAxesByXSerieId(rSeriesProperties.getXId());

	XSeriesProperties& xSerie = xSerieParameterAxes->getXSeriePropertiesById(rSeriesProperties.getXId());

	//get parameter x data for this serie
	ParameterData& xData = (*_pParameterValues)[xSerie.getParamId()];

	int startIndex;
	xData.getIntervalBounds(startDate, stopDate, startIndex, nbValues);

	if (nbValues == 0)
	{
		LOG4CXX_DEBUG(gLogger, "XYPlot::getXComputedValuesFromSerieAndInterval - Cannot find data for the x serie");
		return false;
	}

	double *valuesInterval = xData.getValues(xSerie.getIndex(),startIndex);

	//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

	ParameterAxes* xparameter = getParameterAxesByXSerieId(rSeriesProperties.getXId());
	XSeriesProperties& xSerie = xparameter->getXSeriePropertiesById(rSeriesProperties.getXId());

	// 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
	//ParameterSPtr p = _parameterManager.getParameter(rSeriesProperties.getParamId());

	paramLegendText << " = f(";

	// Try to retrieve informations from paramInfo for X component
	ParameterSPtr p = _parameterManager.getParameter(xparameter->_originalParamId);
	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
			paramLegendText << xparameter->_originalParamId;
		else
		{
			// parameter legend text = _originalParamId [index]
			paramLegendText << xparameter->_originalParamId << "[" << xSerie.getIndex().getDim1Index();
			if (xSerie.getIndex().getDim2Index() != -1)
				paramLegendText << "," << xSerie.getIndex().getDim2Index();
			paramLegendText << "]";
		}
	}

	paramLegendText << ")";

	return paramLegendText.str();
}

} /* namespace plot */