Blame view

src/ParamOutputImpl/Plot/AxisLegendManager.cc 11.5 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
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
#include "AxisLegendManager.hh"

#include "ParamMgr.hh"

using namespace AMDA::Info;

namespace plot
{

void AxisLegendManager::configureYAxisLegendForSeries(
		PanelPlotOutput* plot)
{
	SeriesProperties lSeriesProperties;

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

	std::map<std::string, bool> yAxisIdHasMultiLineLegend;
	std::map<std::string, std::string> generatedYAxisLegend;

	// Parse each parameter and series to determine if axis requires
	// single or multiline legends
	for (auto param: plot->_parameterAxesList)
	{
		for(auto index: param.getYSerieIndexList(plot->_pParameterValues))
		{
			lSeriesProperties = param.getYSeriePropertiesAt(index);
			if(!lSeriesProperties.hasYAxis())
				continue;

			std::string yAxisId = lSeriesProperties.getYAxisId();

			if (yAxisIdHasMultiLineLegend.count(yAxisId) == 0) {
				yAxisIdHasMultiLineLegend [yAxisId] = true;
			}

			for (auto idx : plot->getParamUsedIndexes (lSeriesProperties.getParamId(),
					(*plot->_pParameterValues)[lSeriesProperties.getParamId()].getDim1Size(),
					(*plot->_pParameterValues)[lSeriesProperties.getParamId()].getDim2Size())) {
				// All indexes requested for the parameter,
				// one general legend is sufficient
				if (idx == AMDA::Common::ParameterIndexComponent(-1,-1)) {
					yAxisIdHasMultiLineLegend [yAxisId] = false;
				}
			}
		}
	}

	// Build single or multiline line legend for axis
	for (auto param: plot->_parameterAxesList)
	{
		// Get number of series to draw
		// For each index of parameter identify to which axis series must be drawn.
		for(auto index: param.getYSerieIndexList(plot->_pParameterValues))
		{
			lSeriesProperties = param.getYSeriePropertiesAt(index);
			if(!lSeriesProperties.hasYAxis())
				continue;

			std::string yAxisId = lSeriesProperties.getYAxisId();

			boost::shared_ptr<Axis> lYAxis = plot->_panel->getAxis(yAxisId);
			if (lYAxis.get() == nullptr) {
				std::stringstream lError;
				lError << "AxisLegendManager::configureYAxisLegend : Y axis with id '" << yAxisId << "' not found.";
				BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
			}

			// If axis legend already set, nothing to do
			if (lYAxis->_legend._text.empty() == false)
				continue;

			// Otherwise, build multi or single line legend depending on paramInfo availability
			ParameterSPtr p = plot->_parameterManager.getParameter(lSeriesProperties.getParamId());
			ParamInfoSPtr	paramInfo = piMgr->getParamInfoFromId(p->getInfoId());
			if (paramInfo == nullptr)
				continue;

			// Single line legend
			if (yAxisIdHasMultiLineLegend [yAxisId] == false)
			{
				if (generatedYAxisLegend.count(yAxisId) == 0)
				{
					// legend is build using short_name, units, coordinates_system
					std::stringstream legendInfo;
					legendInfo << paramInfo->getShortName();

					if (paramInfo->getUnits().empty() == false)
						legendInfo << ", " + paramInfo->getUnits();

					if (paramInfo->getCoordinatesSystem().empty() == false)
						legendInfo << ", " + paramInfo->getCoordinatesSystem();

					generatedYAxisLegend [yAxisId] = legendInfo.str();
				}
			}
			else
			{
				// legend is build using short_name, units, coordinates_system
				std::stringstream legendInfo;
				legendInfo << paramInfo->getShortName();

				if ((index.getDim1Index() != -1) || (index.getDim2Index() != -1))
				{
					if (paramInfo->getComponents(index).empty() == false)
						legendInfo << ", " << paramInfo->getComponents(index);
					else
					{
						legendInfo << ", " << paramInfo->getShortName() << "[" << index.getDim1Index();
						if (index.getDim2Index() != -1)
							legendInfo << "," << index.getDim2Index();
						legendInfo << "]";
					}
				}

				if (paramInfo->getUnits().empty() == false)
					legendInfo << ", " + paramInfo->getUnits();

				if (paramInfo->getCoordinatesSystem().empty() == false)
					legendInfo << ", " + paramInfo->getCoordinatesSystem();

				if (generatedYAxisLegend [yAxisId].empty() == false)
					generatedYAxisLegend [yAxisId] += Label::DELIMITER;

				generatedYAxisLegend [yAxisId] += legendInfo.str();
			}
		}
	}

	// Build generated sequence if required
	for (auto axisLegend : generatedYAxisLegend) {
		plot->_panel->getAxis (axisLegend.first)->_legend._text = axisLegend.second;
	}
}

void AxisLegendManager::configureColorAxisLegendForSeries(
		PanelPlotOutput* plot)
{
	// Retrieve ParamInfo Manager
	ParamMgr 		*piMgr =ParamMgr::getInstance();

	SeriesProperties lSeriesProperties;

	// Build Z axis leged for colored param
	boost::shared_ptr<ColorAxis> lZAxis = plot->_panel->getColorAxis();
	ColorSeriesProperties lColorSerieProperties;
	std::vector<std::string> zLegendLines;

	if ((lZAxis != nullptr) && lZAxis->_legend._text.empty())
	{
		for (auto param: plot->_parameterAxesList)
		{
			for(auto index: param.getYSerieIndexList(plot->_pParameterValues))
			{
				lSeriesProperties = param.getYSeriePropertiesAt(index);
				if(!lSeriesProperties.hasYAxis())
					continue;
				//check if a colored param is defined for this serie
				if(lSeriesProperties.getColorParamId().empty())
					continue;
				//get info about colored param
				ParameterSPtr p = plot->_parameterManager.getParameter(lSeriesProperties.getColorParamId());
				ParamInfoSPtr	paramInfo = piMgr->getParamInfoFromId(p->getInfoId());
				//get colored serie properties
				ParameterAxes* colorSerieParameterAxes = plot->getParameterAxesByColorSerieId(lSeriesProperties.getColorSerieId());
				if (colorSerieParameterAxes == NULL)
					continue;
				lColorSerieProperties = colorSerieParameterAxes->getColorSeriePropertiesById(lSeriesProperties.getColorSerieId());
				if (paramInfo != nullptr)
				{
					std::stringstream legendInfo;

					legendInfo << paramInfo->getShortName();

					if (lColorSerieProperties.getIndex() != -1)
					{
						if (paramInfo->getComponents(index).empty() == false)
							legendInfo << ", " << paramInfo->getComponents(lColorSerieProperties.getIndex());
						else
							legendInfo << ", " << paramInfo->getShortName() << "[" << lColorSerieProperties.getIndex() << "]" ;
					}

					if (paramInfo->getUnits().empty() == false)
						legendInfo << ", " + paramInfo->getUnits();

					if (paramInfo->getCoordinatesSystem().empty() == false)
						legendInfo << ", " + paramInfo->getCoordinatesSystem();

					if (std::find(zLegendLines.begin(), zLegendLines.end(), legendInfo.str()) == zLegendLines.end())
						zLegendLines.push_back(legendInfo.str());
				}
			}
		}
		bool isFirstLine = true;
		for (auto line : zLegendLines)
		{
			if (!isFirstLine)
				lZAxis->_legend._text += Label::DELIMITER;
			isFirstLine = false;
			lZAxis->_legend._text += line;
		}
	}
}

void AxisLegendManager::configureXAxisLegendForSeries(
		XYPlot* plot)
{
	// Retrieve ParamInfo Manager
	ParamMgr 		*piMgr =ParamMgr::getInstance();

	SeriesProperties lSeriesProperties;

	std::map<std::string, bool> xAxisIdHasMultiLineLegend;
	std::map<std::string, std::list<std::string>> generatedXAxisLegend;

	// Parse each parameter and series to determine if axis requires
	// single or multiline legends
	for (auto param: plot->_parameterAxesList)
	{
		for(auto index: param.getYSerieIndexList(plot->_pParameterValues))
		{
			lSeriesProperties = param.getYSeriePropertiesAt(index);
			if(!lSeriesProperties.hasYAxis() || !lSeriesProperties.hasXAxis())
				continue;

			ParameterAxes& xParameter = plot->getParameterAxeOnXAxis(lSeriesProperties.getXAxisId());

			std::string yParamId = lSeriesProperties.getParamId();

			XSeriesProperties& xSerie = plot->getSerieOnXAxisfromParameter(lSeriesProperties.getXAxisId(), xParameter);

			std::string xAxisId = xSerie.getAxisId();

			if (xAxisIdHasMultiLineLegend.count(xAxisId) == 0) {
				xAxisIdHasMultiLineLegend [xAxisId] = true;
			}

			for (auto idx : plot->getParamUsedIndexes (xSerie.getXParamIds()[yParamId],
					(*plot->_pParameterValues)[xSerie.getXParamIds()[yParamId]].getDim1Size(),
					(*plot->_pParameterValues)[xSerie.getXParamIds()[yParamId]].getDim2Size())) {
				// All indexes requested for the parameter,
				// one general legend is susficient
				if (idx == AMDA::Common::ParameterIndexComponent(-1,-1)) {
					xAxisIdHasMultiLineLegend [xAxisId] = false;
				}
			}
		}
	}

	// Build single line legend for axis
	for (auto param: plot->_parameterAxesList) {
		for(auto index: param.getYSerieIndexList(plot->_pParameterValues))
		{
			lSeriesProperties = param.getYSeriePropertiesAt(index);
			if(!lSeriesProperties.hasYAxis() || !lSeriesProperties.hasXAxis())
				continue;

			ParameterAxes& xParameter = plot->getParameterAxeOnXAxis(lSeriesProperties.getXAxisId());

			std::string yParamId = lSeriesProperties.getParamId();

			XSeriesProperties& xSerie = plot->getSerieOnXAxisfromParameter(lSeriesProperties.getXAxisId(), xParameter);

			std::string xAxisId = xSerie.getAxisId();

			boost::shared_ptr<Axis> lXAxis = plot->_panel->getAxis(xAxisId);
			if (lXAxis.get() == nullptr) {
				std::stringstream lError;
				lError << "AxisLegendManager::configureXAxisLegendForSeries X axis with id '" << xAxisId << "' not found.";
				BOOST_THROW_EXCEPTION(PanelPlotOutputException() << AMDA::ex_msg(lError.str()));
			}

			// If axis legend already set, nothing to do
			if (lXAxis->_legend._text.empty() == false)
				continue;

			// Otherwise, build multi or single line legend depending on paramInfo availability
			ParameterSPtr p = plot->_parameterManager.getParameter(xSerie.getXParamIds()[yParamId]);
			ParamInfoSPtr	paramInfo = piMgr->getParamInfoFromId(p->getInfoId());

			if (paramInfo == nullptr)
				continue;

			// Single line legend
			if (xAxisIdHasMultiLineLegend [xAxisId] == false) {
				if (generatedXAxisLegend.count(xAxisId) == 0) {
					// legend is build using short_name, units, coordinates_system
					std::stringstream legendInfo;
					legendInfo << paramInfo->getShortName();

					if (paramInfo->getUnits().empty() == false)
						legendInfo << ", " + paramInfo->getUnits();

					if (paramInfo->getCoordinatesSystem().empty() == false)
						legendInfo << ", " + paramInfo->getCoordinatesSystem();

					generatedXAxisLegend [xAxisId].push_back(legendInfo.str());
				}
			} else {
				// legend is build using short_name, units, coordinates_system
				std::stringstream legendInfo;
				legendInfo << paramInfo->getShortName();

				if ((xSerie.getIndex().getDim1Index() != -1) || (xSerie.getIndex().getDim2Index() != -1)) {
					if (paramInfo->getComponents(xSerie.getIndex()).empty() == false)
						legendInfo << ", " << paramInfo->getComponents(xSerie.getIndex());
					else
					{
						legendInfo << ", " << paramInfo->getShortName() << "[" << xSerie.getIndex().getDim1Index();
						if (xSerie.getIndex().getDim2Index() != -1)
							legendInfo << "," << xSerie.getIndex().getDim2Index();
						legendInfo << "]";
					}
				}

				if (paramInfo->getUnits().empty() == false)
					legendInfo << ", " + paramInfo->getUnits();

				if (paramInfo->getCoordinatesSystem().empty() == false)
					legendInfo << ", " + paramInfo->getCoordinatesSystem();

				if (std::find(generatedXAxisLegend [xAxisId].begin(),generatedXAxisLegend [xAxisId].end(), legendInfo.str()) == generatedXAxisLegend [xAxisId].end())
					//new legend to add
					generatedXAxisLegend [xAxisId].push_back(legendInfo.str());
			}
		}
	}

	// Build generated sequence if required
	for (auto axisLegends : generatedXAxisLegend)
	{
		std::stringstream fullLegend;
		bool isFirstLegend = true;
		for (auto legend : axisLegends.second)
		{
			if (!isFirstLegend)
				fullLegend << Label::DELIMITER;
			fullLegend << legend;
			isFirstLegend = false;
		}
		plot->_panel->getAxis (axisLegends.first)->_legend._text = fullLegend.str();
	}
}

} /* namespace plot */