Blame view

src/ParamOutputImpl/Plot/Axis.cc 7.87 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
/*
 * Axis.cc
 *
 *  Created on: 20 nov. 2013
 *      Author: guillaume
 */

#include "Axis.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),
08ec1dde   Benjamin Renard   Add propertie _us...
23
		_used(false),
fbe3c2bb   Benjamin Renard   First commit
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
		_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("")
{
	// 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),
c45180e1   Benjamin Renard   Add _used propert...
53
		_used(axis._used),
fbe3c2bb   Benjamin Renard   First commit
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
		_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("")
{
}

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

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

8499fbdd   Benjamin Renard   Context file gene...
225
226
227
228
229
230
231
232
233
234
235

/**
 * @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");

85bb5117   Benjamin Renard   Give the possibil...
236
237
	writer.addAttribute("min", std::to_string(getRange().getMin()).c_str());
	writer.addAttribute("max", std::to_string(getRange().getMax()).c_str());
8499fbdd   Benjamin Renard   Context file gene...
238
239
240
241

	writer.endElement();
}

fbe3c2bb   Benjamin Renard   First commit
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
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 */