Axis.cc 8.35 KB
/*
 * Axis.cc
 *
 *  Created on: 20 nov. 2013
 *      Author: guillaume
 */

#include "Axis.hh"
#include "Panel.hh"
#include "DefaultPlotConfiguration.hh"
#include <sstream>
#include <iomanip>

namespace plot {

Axis::Axis(bool isZAxis) :
		_id(""),
		_color(),
		_origin(nan("")),
		_position(PlotCommon::Position::POS_TOP),
		_reverse(false),
		_tick(),
		_visible(true),
		_used(false),
		_thickness(1),
		_legend(),
		_drawn(false),

		_additionalObjDrawn(false),
		_isZAxis(isZAxis),
		_scale(Scale::LINEAR),
		_showLegend(true),
		_showTickMark(true),
		_fixedTickMarkWidth(-1),
		_labelGenerator(new LabelGenerator),
		_constantLines(),
		_range(),
		_requestedRange(),
		_tickMarkLines(1),
		_axisOffset(0),
		_axisUnits(""),
		_legendOffset(0)
{
	// Attribute position is not set because we can't tell which type of axis is.
}

Axis::Axis(const Axis& axis) :
		_id(axis._id),
		_color(axis._color),
		_origin(nan("")),
		_position(axis._position),
		_reverse(axis._reverse),
		_tick(),
		_visible(axis._visible),
		_used(axis._used),
		_thickness(axis._thickness),
		_legend(axis._legend),
		_drawn(axis._drawn),

		_additionalObjDrawn(axis._additionalObjDrawn),
		_isZAxis(axis._isZAxis),
		_scale(axis._scale),
		_showLegend(axis._showLegend),
		_showTickMark(axis._showTickMark),
		_fixedTickMarkWidth(axis._fixedTickMarkWidth),
		_labelGenerator(new LabelGenerator),
		_constantLines(axis._constantLines),
		_range(),
		_requestedRange(),
		_tickMarkLines(1),
		_axisOffset(axis._axisOffset),
		_axisUnits(""),
		_legendOffset(axis._legendOffset)
{
}

Axis::~Axis() {
	if (_labelGenerator.unique()) {
		_labelGenerator->_function = nullptr;
		if(_labelGenerator->_data) {
			_labelGenerator->_data = nullptr;
		}
	}
}

Font Axis::getLegendFont(Panel* pPanel) {
	Font legendFont(_legend.getFont());
	Font panelFont = pPanel->getFont();
	if (legendFont.getName() == "")
		legendFont.setName(panelFont.getName());
	if (legendFont.getSize() == 0)
		legendFont.setSize(panelFont.getSize());

	return legendFont;
}

void Axis::dump(std::ostream& out) {
	out << "[AXIS]" << std::endl;
	out << _id << ".axis.origin=" << _origin << std::endl;
	out << _id << ".axis.position=" << _position << std::endl;
	out << _id << ".axis.reverse=" << std::boolalpha << _reverse << std::endl;
	out << _id << ".axis.visible=" << std::boolalpha << _visible << std::endl;
	out << _id << ".axis.thickness=" << _thickness << std::endl;
	out << _id << ".axis.isZAxis=" << _isZAxis << std::endl;
	out << _id << ".axis.scale=";
	switch (_scale) {
		case Axis::Scale::LINEAR:
			out << "linear";
			break;
		case Axis::Scale::LOGARITHMIC:
			out << "logarithmic";
			break;
		default:
			break;
	}
	out << _id << ".axis.showLegend=" << _showLegend << std::endl;
	out << _id << ".axis.showTickMark=" << _showTickMark << std::endl;
	std::string prefix = _id + ".axis.";
	_range.dump(out, prefix);
	_tick.dump(out, prefix);
	_color.dump(out, prefix);
	_legend.dump(out, prefix);
	for (auto constantLine : _constantLines) {
		constantLine->dump(out, prefix);
	}
}

std::ostream& operator <<(std::ostream& out, const Axis& axis) {
	out << "[AXIS]" << std::endl;
	out << axis._id << ".axis.origin=" << axis._origin << std::endl;
	out << axis._id << ".axis.position=" << axis._position << std::endl;
	out << axis._id << ".axis.reverse=" << std::boolalpha << axis._reverse
			<< std::endl;
	out << axis._id << ".axis.visible=" << std::boolalpha << axis._visible
			<< std::endl;
	out << axis._id << ".axis.thickness=" << axis._thickness << std::endl;
	out << axis._id << ".axis.isZAxis=" << axis._isZAxis << std::endl;
	out << axis._id << ".axis.scale=";
	switch (axis._scale) {
		case Axis::Scale::LINEAR:
			out << "linear";
			break;
		case Axis::Scale::LOGARITHMIC:
			out << "logarithmic";
			break;
		default:
			break;
	}
	out << axis._id << ".axis.showLegend=" << axis._showLegend << std::endl;
	out << axis._id << ".axis.showTickMark=" << axis._showTickMark << std::endl;
	return out;
}

/**
 *  a: Draws axis, X-axis is horizontal line (y=0), and Y-axis is vertical line (x=0).
 b: Draws bottom (X) or left (Y) edge of frame.
 c: Draws top (X) or right (Y) edge of frame.
 d: Plot labels as date / time. Values are assumed to be seconds since the epoch (as used by gmtime).
 f: Always use fixed point numeric labels.
 g: Draws a grid at the major tick interval.
 h: Draws a grid at the minor tick interval.
 i: Inverts tick marks, so they are drawn outwards, rather than inwards.
 l: Labels axis logarithmically. This only affects the labels, not the data, and so it is necessary to compute the logarithms of data points before passing them to any of the drawing routines.
 m: Writes numeric labels at major tick intervals in the unconventional location (above box for X, right of box for Y).
 n: Writes numeric labels at major tick intervals in the conventional location (below box for X, left of box for Y).
 o: Use custom labelling function to generate axis label text. The custom labelling function can be defined with the plslabelfunc command.
 s: Enables subticks between major ticks, only valid if t is also specified.
 t: Draws major ticks.
 u: Exactly like "b" except don't draw edge line.
 w: Exactly like "c" except don't draw edge line.
 x: Exactly like "t" (including the side effect of the numerical labels for the major ticks) except exclude drawing the major and minor tick marks.
 */
std::string Axis::getPlotOpt() {
	std::string options;
	if (!_visible) {
		return options;
	}

	options += "t"; // draw axis and tick labels in conventional way and major ticks

	switch (_position) {
	case PlotCommon::Position::POS_RIGHT:
	case PlotCommon::Position::POS_TOP:
		options += "mc";
		break;
	case PlotCommon::Position::POS_BOTTOM:
	case PlotCommon::Position::POS_LEFT:
	default:
		options += "nb";
		break;
	}

	if (_tick._position == Tick::TickPosition::OUTWARDS) {
		options += "i";
	}

	if (!_tick.isSet() || _tick.isMinorSet()) {
		options += "s"; // draw minor ticks if auto tick
	}

	// draw grid for minor and/or major ticks or do not draw ...
	if (_tick._isMajorGridVisible) {
		options += "g";
	}
	if (_tick._isMinorGridVisible) {
		options += "h";
	}

	if (_scale == Scale::LOGARITHMIC) {
		options += "l";
	}

	return options;
}

std::pair<int, int> Axis::getTickMarkSize() {
	// If tickmarks are not visible return a null size
	if (_showTickMark == false) {
		return std::pair<int , int> (0,0);
	}

	std::pair<int, int> tickMarkSize(0, getTickMarkLines());

	// If TickMark width is fixed, return it !
	if (_fixedTickMarkWidth != -1) {
		tickMarkSize.first = _fixedTickMarkWidth;
		return tickMarkSize;
	}

	std::stringstream lStrMin;
	lStrMin << std::setprecision(8) << getRange().getMin();
	std::stringstream lStrMax;
	lStrMax << std::setprecision(8) << getRange().getMax();
	tickMarkSize.first = std::max(lStrMin.str().size(), lStrMax.str().size());

	return tickMarkSize;
}


/**
 * @brief Write axis context
 */
void Axis::writeContext(ContextFileWriter& writer) {
	writer.startElement("axis");

	writer.addAttribute("id", _id.c_str());

	writer.addAttribute("logarithmic", (_scale == Scale::LOGARITHMIC) ? "true" : "false");

	std::ostringstream outMin;
	outMin << std::setprecision(12) << getRange().getMin();
	std::ostringstream outMax;
	outMax << std::setprecision(12) << getRange().getMax();

	writer.addAttribute("min", outMin.str().c_str());
	writer.addAttribute("max", outMax.str().c_str());

	writer.endElement();
}

double getMajorTickSpace(Axis* axis, double min, double max) {
	if (axis->_tick.isMajorSet()) {
		// Major tick
		if (!isnan(axis->_tick.getMajorSpace())) {
			return axis->_tick.getMajorSpace();
		} else {
			return (max - min) / axis->_tick.getMajorNumber();
		}
	}
	return axis->getAutoMajorTickSpace(min, max); // auto tick
}

double getMajorTickNumber(Axis* axis, double min, double max) {
	if (axis->_tick.isMajorSet()) {
		if (!isnan(axis->_tick.getMajorSpace())) {
			return (max - min) / axis->_tick.getMajorSpace();
		} else {
			return axis->_tick.getMajorNumber();
		}
	}
	return abs((max - min) / axis->getAutoMajorTickSpace(min,max)); // auto tick number
}

int getMinorTickNumber(Axis* xAxis, double min, double max,
		double majorTickSpace) {
	if (xAxis->_tick.isMinorSet()) {
		// Minor tick
		if (xAxis->_tick.getMinorNumber() != -1) {
			return xAxis->_tick.getMinorNumber();
		} else if (majorTickSpace != 0) {
			// just if major ticks are not automatic
			return majorTickSpace / xAxis->_tick.getMinorSpace();
		}
	}
	return xAxis->getAutoMinorTickNumber(min, max);
}

} /* namespace plot */