TimeAxis.cc 4.85 KB
/*
 * TimeAxis.cc
 *
 *  Created on: 27 nov. 2013
 *      Author: CS
 */

#include "TimeAxis.hh"
#include <iostream>
#include <map>

namespace plot {

const double TimeAxis::SECONDS_IN_MINUTES = 60.0;
const double TimeAxis::SECONDS_IN_HOUR = 60.0 * SECONDS_IN_MINUTES;
const double TimeAxis::SECONDS_IN_DAY = 24.0 * SECONDS_IN_HOUR;

/**
 * @overload Axis::getAutoMajorTickSpace
 */
double TimeAxis::getAutoMajorTickSpace(double min, double max) {
	return TimeAxis::computeAutoMajorTickSpace (min, max);
}

/**
 * @overload Axis::getAutoMajorTickSpace
 */
double TimeAxis::computeAutoMajorTickSpace(double min, double max) {
	double interval = abs(max - min);
	double basicInterval;
	double factor;
	if (interval < 3 * TimeAxis::SECONDS_IN_MINUTES) {
		// second
		factor = 1;
		basicInterval = interval;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_HOUR) {
		// minutes
		factor = SECONDS_IN_MINUTES;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY) {
		// days
		factor = SECONDS_IN_HOUR;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 7) {
		// weeks
		factor = SECONDS_IN_DAY;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 365.25) {
		// years
		factor = SECONDS_IN_DAY * 7;
		basicInterval = interval / factor;
	} else {
		// a lot of years
		factor = SECONDS_IN_DAY * 365.25;
		basicInterval = interval / factor;
	}

	double t1 = log10(basicInterval);
	double np = floor(t1);
	t1 = t1 - np;

	double t2;
	if (t1 > 0.7781512503) {
		t2 = 2.0;
	} else if (t1 > 0.4771212549) {
		t2 = 1.0;
	} else if (t1 > 0.1760912591) {
		t2 = 5.0;
		np = np - 1;
	} else {
		t2 = 2.0;
		np = np - 1;
	}

	return t2 * pow(10.0, (double) np) * factor;
}

/**
 * @overload Axis::getAutoMinorTickNumber
 */
double TimeAxis::getAutoMinorTickNumber(double min, double max) {
	double interval = abs(max - min);
	double basicInterval;
	double factor;
	if (interval < 3 * TimeAxis::SECONDS_IN_MINUTES) {
		// second
		factor = 1;
		basicInterval = interval;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_HOUR) {
		// minutes
		factor = SECONDS_IN_MINUTES;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY) {
		// days
		factor = SECONDS_IN_HOUR;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 7) {
		// weeks
		factor = SECONDS_IN_DAY;
		basicInterval = interval / factor;
	} else if (interval < 3 * TimeAxis::SECONDS_IN_DAY * 365.25) {
		// years
		factor = SECONDS_IN_DAY * 7;
		basicInterval = interval / factor;
	} else {
		// a lot of years
		factor = SECONDS_IN_DAY * 365.25;
		basicInterval = interval / factor;
	}

	double t1 = log10(basicInterval);
	double np = floor(t1);
	t1 = t1 - np;

	double ns;
	if (t1 > 0.7781512503) {
		ns = 4;
	} else if (t1 > 0.4771212549) {
		ns = 5;
	} else if (t1 > 0.1760912591) {
		ns = 5;
		//np = np - 1;
	} else {
		ns = 4;
	}

	return ns;
}

std::string getPlTimeFormat(std::string userFormat, double startTime,
		double stopTime, double numberOfMajorTicks) {
	double interval = abs(stopTime - startTime);
	// interval is less than a minute, show seconds
	if (interval <= TimeAxis::SECONDS_IN_MINUTES) {
		return "%H:%M:%S%3%";
	} // interval is less than an hour, show minutes
	else if (interval < TimeAxis::SECONDS_IN_HOUR) {
		/*if (!std::isnan(numberOfMajorTicks)
				&& interval / TimeAxis::SECONDS_IN_MINUTES
						< numberOfMajorTicks) {
			return "%M:%S%3%";
		}*/
		return "%H:%M:%S";

	} // interval is less than an day, show hours
	else if (interval < TimeAxis::SECONDS_IN_DAY) {
		/*if (!std::isnan(numberOfMajorTicks)
				&& interval / TimeAxis::SECONDS_IN_HOUR < numberOfMajorTicks) {

			return "%H:%M:%S";
		}*/
		return "%H:%M";
	} //interval is one day and start time is at 00:00
	else if ((interval == TimeAxis::SECONDS_IN_DAY) && ((int)startTime%(int)TimeAxis::SECONDS_IN_DAY == 0)) {
		return "%H:%M";
	} // interval is more than a day, show days
	else {
		if (userFormat == "ddd/yy") {
			if (!std::isnan(numberOfMajorTicks)
					&& interval / TimeAxis::SECONDS_IN_DAY
							< numberOfMajorTicks) {
				return "%j/%H:%M";
			}
			return "%j/%y";
		} else {
			if (!std::isnan(numberOfMajorTicks)
					&& interval / TimeAxis::SECONDS_IN_DAY
							< numberOfMajorTicks) {
				return "%d/%H:%M";
			}
			return "%d/%m/%y";
		}
	}
}

std::string getPlStartTimeFormat(std::string userFormat, double /*startTime*/, double /*stopTime*/) {
	//double interval = stopTime - startTime;
	std::string dateStr;

	/*if (interval >= TimeAxis::SECONDS_IN_DAY && ((int)startTime%(int)TimeAxis::SECONDS_IN_DAY != 0)) {
		dateStr = "%b %Y";
	}
	else*/ if (userFormat == "ddd/yy") {
		//dateStr = "%b %d (%j) %Y";
		dateStr = "%j %Y";
	} else {
		//dateStr = "%b %d %Y";
		dateStr = "%d %b %Y";
	}

	/*if (interval < TimeAxis::SECONDS_IN_HOUR) {
		dateStr += " %H:%M";
	}*/
	return dateStr;
}

} /* namespace plot */