Blame view

src/ParamOutputImpl/Plot/AxisLegendManager.cc 12.5 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
#include "AxisLegendManager.hh"

fbe3c2bb   Benjamin Renard   First commit
3
4
5
6
7
using namespace AMDA::Info;

namespace plot
{

c2fa3b5d   Benjamin Renard   Rework of legend ...
8
9
void AxisLegendManager::configureYAxisLegendForSpectro(
                PanelPlotOutput* plot)
fbe3c2bb   Benjamin Renard   First commit
10
{
fbe3c2bb   Benjamin Renard   First commit
11
12
13
	// Retrieve ParamInfo Manager
	ParamMgr* piMgr = ParamMgr::getInstance();

c2fa3b5d   Benjamin Renard   Rework of legend ...
14
15
16
17
	for (auto param: plot->_parameterAxesList) {
		std::shared_ptr<SpectroProperties> spectroPropertiesPtr = param.getSpectroProperties();
		if (spectroPropertiesPtr == nullptr)
			continue; //no spectro defined
fbe3c2bb   Benjamin Renard   First commit
18

c2fa3b5d   Benjamin Renard   Rework of legend ...
19
20
		if(!spectroPropertiesPtr->hasYAxis())
			continue;
fbe3c2bb   Benjamin Renard   First commit
21

c2fa3b5d   Benjamin Renard   Rework of legend ...
22
23
24
25
		std::string yAxisId = spectroPropertiesPtr->getYAxisId();
		boost::shared_ptr<Axis> lYAxis = plot->_panel->getAxis(yAxisId);
		if (lYAxis.get() == nullptr)
			continue;
fbe3c2bb   Benjamin Renard   First commit
26

c2fa3b5d   Benjamin Renard   Rework of legend ...
27
28
		if (!lYAxis->_legend._text.empty())
			continue;
fbe3c2bb   Benjamin Renard   First commit
29

c2fa3b5d   Benjamin Renard   Rework of legend ...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
		ParameterSPtr p = plot->_parameterManager.getParameter(param._originalParamId);
		AMDA::Info::ParamInfoSPtr paramInfo = piMgr->getParamInfoFromId(p->getInfoId());
		if (paramInfo == nullptr)
			continue;

		boost::shared_ptr<AMDA::Info::ParamTable> tableSPtr;
		tableSPtr = paramInfo->getTable(spectroPropertiesPtr->getRelatedDim());

		if (tableSPtr == nullptr)
			continue;

		if (((*plot->_pParameterValues)[spectroPropertiesPtr->getParamId()].getDim1Size() > 0) &&
			((*plot->_pParameterValues)[spectroPropertiesPtr->getParamId()].getDim2Size() > 0) &&
			!spectroPropertiesPtr->getIndexes().empty())
		{
			boost::shared_ptr<AMDA::Info::ParamTable> otherTableSPtr;
			int otherDimIndex;
			if (spectroPropertiesPtr->getRelatedDim() == 0)
			{
				otherTableSPtr = paramInfo->getTable(1);
				otherDimIndex = spectroPropertiesPtr->getIndexes().front().getDim2Index();
fbe3c2bb   Benjamin Renard   First commit
51
			}
c2fa3b5d   Benjamin Renard   Rework of legend ...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
			else
			{
				otherTableSPtr = paramInfo->getTable(0);
				otherDimIndex = spectroPropertiesPtr->getIndexes().front().getDim1Index();
			}
			if ((otherTableSPtr != nullptr) && !otherTableSPtr->isVariable())
			{
				if (otherTableSPtr->getName().empty())
					addInfoPart(lYAxis->_legend._text, "Table bounds");
				else
					addInfoPart(lYAxis->_legend._text, otherTableSPtr->getName());
				
				AMDA::Info::t_TableBound crtBound = otherTableSPtr->getBound(&plot->_parameterManager, otherDimIndex);
				addBoundsPart(lYAxis->_legend._text, crtBound.min, crtBound.max);
				addInfoPart(lYAxis->_legend._text, otherTableSPtr->getUnits());
			}
			else
			{
				std::string part = "Table Index: ";
				part += otherDimIndex;
				addInfoPart(lYAxis->_legend._text, part);
			}
			addBreakLine(lYAxis->_legend._text);
fbe3c2bb   Benjamin Renard   First commit
75
		}
c2fa3b5d   Benjamin Renard   Rework of legend ...
76
77
78
		addInfoPart(lYAxis->_legend._text, tableSPtr->getName());
		addInfoPart(lYAxis->_legend._text, tableSPtr->getUnits());
		return;
fbe3c2bb   Benjamin Renard   First commit
79
	}
c2fa3b5d   Benjamin Renard   Rework of legend ...
80
81
82
83
84
85
}

void AxisLegendManager::configureYAxisLegendForSeries(
		PanelPlotOutput* plot)
{
	SeriesProperties lSeriesProperties;
fbe3c2bb   Benjamin Renard   First commit
86

c2fa3b5d   Benjamin Renard   Rework of legend ...
87
88
	// Build list of all indexes used by each parameters for each y axes
	std::map<std::string, AxisParamsComponents> axesParamsComponents;
fbe3c2bb   Benjamin Renard   First commit
89
90
	for (auto param: plot->_parameterAxesList)
	{
fbe3c2bb   Benjamin Renard   First commit
91
92
93
94
95
96
97
		for(auto index: param.getYSerieIndexList(plot->_pParameterValues))
		{
			lSeriesProperties = param.getYSeriePropertiesAt(index);
			if(!lSeriesProperties.hasYAxis())
				continue;

			std::string yAxisId = lSeriesProperties.getYAxisId();
fbe3c2bb   Benjamin Renard   First commit
98
99
			boost::shared_ptr<Axis> lYAxis = plot->_panel->getAxis(yAxisId);
			if (lYAxis.get() == nullptr) {
fbe3c2bb   Benjamin Renard   First commit
100
				continue;
c2fa3b5d   Benjamin Renard   Rework of legend ...
101
 			}
fbe3c2bb   Benjamin Renard   First commit
102

c2fa3b5d   Benjamin Renard   Rework of legend ...
103
104
105
			pushComponentInList(lSeriesProperties.getParamId(), index, axesParamsComponents[yAxisId]);
		}
	}
fbe3c2bb   Benjamin Renard   First commit
106

c2fa3b5d   Benjamin Renard   Rework of legend ...
107
108
109
110
111
112
	std::list<std::string> legendLines;
	for (auto axisParamsComponents : axesParamsComponents) {
		boost::shared_ptr<Axis> lYAxis = plot->_panel->getAxis(axisParamsComponents.first);
		setAxisLegendForSeries(plot, lYAxis, axisParamsComponents.second);
	}
}
fbe3c2bb   Benjamin Renard   First commit
113

c2fa3b5d   Benjamin Renard   Rework of legend ...
114
115
116
117
118
119
120
121
122
123
124
void AxisLegendManager::pushComponentInList(std::string paramId, AMDA::Common::ParameterIndexComponent& index, AxisParamsComponents& axisParamsComponents) {
	if (index == AMDA::Common::ParameterIndexComponent(-1,-1)) {
		//All indexes of this parameter are used
		axisParamsComponents[paramId].clear();
		axisParamsComponents[paramId].push_back(index);
	}
	else {
		if (axisParamsComponents[paramId].size() > 0) {
			if (axisParamsComponents[paramId].front() == AMDA::Common::ParameterIndexComponent(-1,-1)) {
				//Skip. All components already defined for this paramter on this axis
				return;
fbe3c2bb   Benjamin Renard   First commit
125
			}
c2fa3b5d   Benjamin Renard   Rework of legend ...
126
127
128
129
130
131
		}
		if (std::find(axisParamsComponents[paramId].begin(), axisParamsComponents[paramId].end(), index) == axisParamsComponents[paramId].end()) {
			//Add this components for this axis
			axisParamsComponents[paramId].push_back(index);
		}
	}
fbe3c2bb   Benjamin Renard   First commit
132

c2fa3b5d   Benjamin Renard   Rework of legend ...
133
}
fbe3c2bb   Benjamin Renard   First commit
134

c2fa3b5d   Benjamin Renard   Rework of legend ...
135
136
137
138
void AxisLegendManager::setAxisLegendForSeries(PanelPlotOutput* plot, boost::shared_ptr<Axis>& pAxis, AxisParamsComponents& axisParamsComponents) {
	if (pAxis == nullptr || !pAxis->_legend._text.empty()) {
		return;
	}
fbe3c2bb   Benjamin Renard   First commit
139

c2fa3b5d   Benjamin Renard   Rework of legend ...
140
141
	// Retrieve ParamInfo Manager
	ParamMgr* piMgr = ParamMgr::getInstance();
fbe3c2bb   Benjamin Renard   First commit
142

c2fa3b5d   Benjamin Renard   Rework of legend ...
143
144
145
146
147
148
149
150
151
152
	//Build list of legend lines
	std::list<std::string> legendLines;
	for (auto paramsComponents : axisParamsComponents) {
		ParameterSPtr p = plot->_parameterManager.getParameter(paramsComponents.first);
		ParamInfoSPtr   paramInfo = piMgr->getParamInfoFromId(p->getInfoId());
		if (paramsComponents.second.size() == p->getDataWriterTemplate()->getParamData()->getDim1() * p->getDataWriterTemplate()->getParamData()->getDim2()) {
			//All components of this parameter are used by this axis => merge
			paramsComponents.second.clear();
			paramsComponents.second.push_back(AMDA::Common::ParameterIndexComponent(-1,-1));
		}
8c8ac7bf   Benjamin Renard   Write mission/ins...
153
		bool isFirstComponent = true;
c2fa3b5d   Benjamin Renard   Rework of legend ...
154
155
156
		for (auto components : paramsComponents.second) {
			if (paramInfo == nullptr)
				continue;
8c8ac7bf   Benjamin Renard   Write mission/ins...
157
158
159
160
161
162
			if ((axisParamsComponents.size() == 1) && isFirstComponent) {
				std::string info = getMissionInstrumentInfo(paramInfo);
				if (!info.empty()) {
					legendLines.push_back(info);
				}
			}
c2fa3b5d   Benjamin Renard   Rework of legend ...
163
			legendLines.push_back(getParamLegend(paramInfo, components));
8c8ac7bf   Benjamin Renard   Write mission/ins...
164
			isFirstComponent = false;
fbe3c2bb   Benjamin Renard   First commit
165
166
167
		}
	}

c2fa3b5d   Benjamin Renard   Rework of legend ...
168
169
170
171
	//Set legend to axis
	for (auto line : legendLines) {
		addBreakLine(pAxis->_legend._text);
		pAxis->_legend._text += line;
fbe3c2bb   Benjamin Renard   First commit
172
173
174
	}
}

c2fa3b5d   Benjamin Renard   Rework of legend ...
175
void AxisLegendManager::configureColorAxisLegendForSpectro(
fbe3c2bb   Benjamin Renard   First commit
176
177
178
		PanelPlotOutput* plot)
{
	// Retrieve ParamInfo Manager
c2fa3b5d   Benjamin Renard   Rework of legend ...
179
	ParamMgr                *piMgr =ParamMgr::getInstance();
fbe3c2bb   Benjamin Renard   First commit
180

c2fa3b5d   Benjamin Renard   Rework of legend ...
181
182
183
184
	for (auto param: plot->_parameterAxesList) {
		std::shared_ptr<SpectroProperties> spectroPropertiesPtr = param.getSpectroProperties();
		if (spectroPropertiesPtr == nullptr)
			continue; //no spectro defined
fbe3c2bb   Benjamin Renard   First commit
185

c2fa3b5d   Benjamin Renard   Rework of legend ...
186
187
		if(!spectroPropertiesPtr->hasZAxis())
			continue;
fbe3c2bb   Benjamin Renard   First commit
188

c2fa3b5d   Benjamin Renard   Rework of legend ...
189
190
191
192
193
194
195
196
197
198
199
		ParameterSPtr p = plot->_parameterManager.getParameter(param._originalParamId);
		AMDA::Info::ParamInfoSPtr paramInfo = piMgr->getParamInfoFromId(p->getInfoId());
		if (paramInfo == nullptr)
			continue;

		// Build Z axis leged for spectro
		boost::shared_ptr<ColorAxis> lZAxis = plot->_panel->getColorAxis();

		if ((lZAxis != nullptr) && lZAxis->_legend._text.empty()) {
			addInfoPart(lZAxis->_legend._text, paramInfo->getName());
			addInfoPart(lZAxis->_legend._text, paramInfo->getUnits());
fbe3c2bb   Benjamin Renard   First commit
200
		}
c2fa3b5d   Benjamin Renard   Rework of legend ...
201
202

		continue;
fbe3c2bb   Benjamin Renard   First commit
203
204
205
	}
}

c2fa3b5d   Benjamin Renard   Rework of legend ...
206
207
void AxisLegendManager::configureColorAxisLegendForSeries(
		PanelPlotOutput* plot)
fbe3c2bb   Benjamin Renard   First commit
208
{
fbe3c2bb   Benjamin Renard   First commit
209
	SeriesProperties lSeriesProperties;
c2fa3b5d   Benjamin Renard   Rework of legend ...
210
211
	ColorSeriesProperties lColorSerieProperties;
	boost::shared_ptr<Axis> lZAxis = plot->_panel->getColorAxis();
fbe3c2bb   Benjamin Renard   First commit
212

c2fa3b5d   Benjamin Renard   Rework of legend ...
213
214
215
216
	// Build list of all indexes used by each parameters for each y axes
	AxisParamsComponents axisParamsComponents;
	for (auto param: plot->_parameterAxesList) {
		for(auto index: param.getYSerieIndexList(plot->_pParameterValues)) {
fbe3c2bb   Benjamin Renard   First commit
217
			lSeriesProperties = param.getYSeriePropertiesAt(index);
fbe3c2bb   Benjamin Renard   First commit
218

c2fa3b5d   Benjamin Renard   Rework of legend ...
219
220
			if(!lSeriesProperties.hasYAxis())
				continue;
fbe3c2bb   Benjamin Renard   First commit
221
222
223

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

c2fa3b5d   Benjamin Renard   Rework of legend ...
224
225
226
			//check if a colored param is defined for this serie
			if(lSeriesProperties.getColorParamId().empty())
				continue;
fbe3c2bb   Benjamin Renard   First commit
227

c2fa3b5d   Benjamin Renard   Rework of legend ...
228
			ParameterAxes* colorSerieParameterAxes = plot->getParameterAxesByColorSerieId(lSeriesProperties.getColorSerieId());
fbe3c2bb   Benjamin Renard   First commit
229

c2fa3b5d   Benjamin Renard   Rework of legend ...
230
231
			if (colorSerieParameterAxes == NULL)
				continue;
fbe3c2bb   Benjamin Renard   First commit
232

c2fa3b5d   Benjamin Renard   Rework of legend ...
233
234
235
236
237
238
			lColorSerieProperties = colorSerieParameterAxes->getColorSeriePropertiesById(lSeriesProperties.getColorSerieId());

			AMDA::Common::ParameterIndexComponent colorSerieIndex = AMDA::Common::ParameterIndexComponent(-1,-1);
			if (lColorSerieProperties.getIndex() > -1)
				colorSerieIndex = AMDA::Common::ParameterIndexComponent(lColorSerieProperties.getIndex(), -1);
			pushComponentInList(lColorSerieProperties.getColorParamIds()[yParamId], colorSerieIndex, axisParamsComponents);
fbe3c2bb   Benjamin Renard   First commit
239
240
241
		}
	}

c2fa3b5d   Benjamin Renard   Rework of legend ...
242
243
244
245
246
247
248
249
250
251
	setAxisLegendForSeries(plot, lZAxis, axisParamsComponents);
}

void AxisLegendManager::configureXAxisLegendForSeries(
		XYPlot* plot)
{
	// Build list of all indexes used by each parameters for each x axes
	std::map<std::string, AxisParamsComponents> axesParamsComponents;

	SeriesProperties lSeriesProperties;
fbe3c2bb   Benjamin Renard   First commit
252
	for (auto param: plot->_parameterAxesList) {
c2fa3b5d   Benjamin Renard   Rework of legend ...
253
		for(auto index: param.getYSerieIndexList(plot->_pParameterValues)) {
fbe3c2bb   Benjamin Renard   First commit
254
255
256
257
258
259
260
261
262
263
264
265
266
267
			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) {
c2fa3b5d   Benjamin Renard   Rework of legend ...
268
				continue;
fbe3c2bb   Benjamin Renard   First commit
269
270
			}

c2fa3b5d   Benjamin Renard   Rework of legend ...
271
272
273
274
			AMDA::Common::ParameterIndexComponent xIndex = xSerie.getIndex();
			pushComponentInList(xSerie.getXParamIds()[yParamId], xIndex, axesParamsComponents[xAxisId]);
		}
	}
fbe3c2bb   Benjamin Renard   First commit
275

c2fa3b5d   Benjamin Renard   Rework of legend ...
276
277
278
279
280
281
	std::list<std::string> legendLines;
	for (auto axisParamsComponents : axesParamsComponents) {
		boost::shared_ptr<Axis> lXAxis = plot->_panel->getAxis(axisParamsComponents.first);
		setAxisLegendForSeries(plot, lXAxis, axisParamsComponents.second);
        }
}
fbe3c2bb   Benjamin Renard   First commit
282

c2fa3b5d   Benjamin Renard   Rework of legend ...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
void AxisLegendManager::addInfoPart(std::string& legend, std::string info) {
	if (!info.empty()) {
		if (!legend.empty()) {
			if (legend.length() >= Label::DELIMITER.length()) {
				if (legend.compare(legend.length() - Label::DELIMITER.length(), Label::DELIMITER.length(), Label::DELIMITER) != 0) {
					legend += ", ";
				}
			}
			else {
				legend += ", ";
			}
		}
		legend += info;
	}
}
fbe3c2bb   Benjamin Renard   First commit
298

c2fa3b5d   Benjamin Renard   Rework of legend ...
299
300
301
302
303
304
305
306
307
void AxisLegendManager::addIndexPart(std::string& legend, AMDA::Common::ParameterIndexComponent& index) {
	legend += "[";
	legend += std::to_string(index.getDim1Index());
	if (index.getDim2Index() != -1) {
		legend += ",";
		legend += std::to_string(index.getDim2Index());
	}
	legend += "]";
}
fbe3c2bb   Benjamin Renard   First commit
308

c2fa3b5d   Benjamin Renard   Rework of legend ...
309
310
311
312
313
void AxisLegendManager::addIndexPart(std::string& legend, int index) {
	legend += "[";
	legend += std::to_string(index);
	legend += "]";
}
fbe3c2bb   Benjamin Renard   First commit
314

c2fa3b5d   Benjamin Renard   Rework of legend ...
315
316
void AxisLegendManager::addBoundsPart(std::string& legend, PLFLT min, PLFLT max) {
	legend += " ";
fbe3c2bb   Benjamin Renard   First commit
317

c2fa3b5d   Benjamin Renard   Rework of legend ...
318
319
320
321
	PLINT axis = 0;
	PLPointer data = NULL;
	char minCount[1024];
	char maxCount[1024];
fbe3c2bb   Benjamin Renard   First commit
322

c2fa3b5d   Benjamin Renard   Rework of legend ...
323
324
	generateDigitalLabel(axis, min, minCount, 1024, data);
	generateDigitalLabel(axis, max, maxCount, 1024, data);
fbe3c2bb   Benjamin Renard   First commit
325

c2fa3b5d   Benjamin Renard   Rework of legend ...
326
327
328
329
	legend += std::string(minCount);
	legend += ", ";
	legend += std::string(maxCount);
}
fbe3c2bb   Benjamin Renard   First commit
330

c2fa3b5d   Benjamin Renard   Rework of legend ...
331
332
333
void AxisLegendManager::addBreakLine(std::string& legend) {
	if (!legend.empty()) {
		legend += Label::DELIMITER;
fbe3c2bb   Benjamin Renard   First commit
334
	}
c2fa3b5d   Benjamin Renard   Rework of legend ...
335
}
fbe3c2bb   Benjamin Renard   First commit
336

c2fa3b5d   Benjamin Renard   Rework of legend ...
337
338
339
340
341
342
343
344
std::string AxisLegendManager::getParamLegend(AMDA::Info::ParamInfoSPtr paramInfo, AMDA::Common::ParameterIndexComponent& index) {
	std::string legend;
	addInfoPart(legend, paramInfo->getShortName());

	if ((index.getDim1Index() != -1) || (index.getDim2Index() != -1)) {
		if (paramInfo->getComponents(index).empty() == false)
			addInfoPart(legend, paramInfo->getComponents(index));
		else
fbe3c2bb   Benjamin Renard   First commit
345
		{
c2fa3b5d   Benjamin Renard   Rework of legend ...
346
347
			addInfoPart(legend, paramInfo->getShortName());
			addIndexPart(legend, index);
fbe3c2bb   Benjamin Renard   First commit
348
		}
fbe3c2bb   Benjamin Renard   First commit
349
	}
c2fa3b5d   Benjamin Renard   Rework of legend ...
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

	addInfoPart(legend, paramInfo->getUnits());
	addInfoPart(legend, paramInfo->getCoordinatesSystem());
	
	return legend;
}

std::string AxisLegendManager::getParamLegend(AMDA::Info::ParamInfoSPtr paramInfo, int index) {
	std::string legend;
	addInfoPart(legend, paramInfo->getShortName());

/*	if ((index.getDim1Index() != -1) || (index.getDim2Index() != -1)) {
                if (paramInfo->getComponents(index).empty() == false)
                        addInfoPart(legend, paramInfo->getComponents(index));
                else
                {
                        addInfoPart(legend, paramInfo->getShortName());
                        addIndexPart(legend, index);
                }
        }

        addInfoPart(legend, paramInfo->getUnits());
        addInfoPart(legend, paramInfo->getCoordinatesSystem());
*/
        return legend;
fbe3c2bb   Benjamin Renard   First commit
375
376
}

8c8ac7bf   Benjamin Renard   Write mission/ins...
377
378
379
380
381
382
383
384
385
std::string AxisLegendManager::getMissionInstrumentInfo(AMDA::Info::ParamInfoSPtr paramInfo) {
	std::string info = paramInfo->getInstrumentId();
	if (info.empty())
		return info;
	std::replace(info.begin(), info.end(), '_', ' ');
	std::transform(info.begin(), info.end(), info.begin(), ::toupper);
	return info;
}

fbe3c2bb   Benjamin Renard   First commit
386
} /* namespace plot */