AxisLegendManager.cc 11.9 KB
#include "AxisLegendManager.hh"

using namespace AMDA::Info;

namespace plot
{

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

	for (auto param: plot->_parameterAxesList) {
		std::shared_ptr<SpectroProperties> spectroPropertiesPtr = param.getSpectroProperties();
		if (spectroPropertiesPtr == nullptr)
			continue; //no spectro defined

		if(!spectroPropertiesPtr->hasYAxis())
			continue;

		std::string yAxisId = spectroPropertiesPtr->getYAxisId();
		boost::shared_ptr<Axis> lYAxis = plot->_panel->getAxis(yAxisId);
		if (lYAxis.get() == nullptr)
			continue;

		if (!lYAxis->_legend._text.empty())
			continue;

		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();
			}
			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);
		}
		addInfoPart(lYAxis->_legend._text, tableSPtr->getName());
		addInfoPart(lYAxis->_legend._text, tableSPtr->getUnits());
		return;
	}
}

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

	// Build list of all indexes used by each parameters for each y axes
	std::map<std::string, AxisParamsComponents> axesParamsComponents;
	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();
			boost::shared_ptr<Axis> lYAxis = plot->_panel->getAxis(yAxisId);
			if (lYAxis.get() == nullptr) {
				continue;
 			}

			pushComponentInList(lSeriesProperties.getParamId(), index, axesParamsComponents[yAxisId]);
		}
	}

	std::list<std::string> legendLines;
	for (auto axisParamsComponents : axesParamsComponents) {
		boost::shared_ptr<Axis> lYAxis = plot->_panel->getAxis(axisParamsComponents.first);
		setAxisLegendForSeries(plot, lYAxis, axisParamsComponents.second);
	}
}

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;
			}
		}
		if (std::find(axisParamsComponents[paramId].begin(), axisParamsComponents[paramId].end(), index) == axisParamsComponents[paramId].end()) {
			//Add this components for this axis
			axisParamsComponents[paramId].push_back(index);
		}
	}

}

void AxisLegendManager::setAxisLegendForSeries(PanelPlotOutput* plot, boost::shared_ptr<Axis>& pAxis, AxisParamsComponents& axisParamsComponents) {
	if (pAxis == nullptr || !pAxis->_legend._text.empty()) {
		return;
	}

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

	//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));
		}
		for (auto components : paramsComponents.second) {
			if (paramInfo == nullptr)
				continue;
			legendLines.push_back(getParamLegend(paramInfo, components));
		}
	}

	//Set legend to axis
	for (auto line : legendLines) {
		addBreakLine(pAxis->_legend._text);
		pAxis->_legend._text += line;
	}
}

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

	for (auto param: plot->_parameterAxesList) {
		std::shared_ptr<SpectroProperties> spectroPropertiesPtr = param.getSpectroProperties();
		if (spectroPropertiesPtr == nullptr)
			continue; //no spectro defined

		if(!spectroPropertiesPtr->hasZAxis())
			continue;

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

		continue;
	}
}

void AxisLegendManager::configureColorAxisLegendForSeries(
		PanelPlotOutput* plot)
{
	SeriesProperties lSeriesProperties;
	ColorSeriesProperties lColorSerieProperties;
	boost::shared_ptr<Axis> lZAxis = plot->_panel->getColorAxis();

	// 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)) {
			lSeriesProperties = param.getYSeriePropertiesAt(index);

			if(!lSeriesProperties.hasYAxis())
				continue;

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

			//check if a colored param is defined for this serie
			if(lSeriesProperties.getColorParamId().empty())
				continue;

			ParameterAxes* colorSerieParameterAxes = plot->getParameterAxesByColorSerieId(lSeriesProperties.getColorSerieId());

			if (colorSerieParameterAxes == NULL)
				continue;

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

	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;
	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) {
				continue;
			}

			AMDA::Common::ParameterIndexComponent xIndex = xSerie.getIndex();
			pushComponentInList(xSerie.getXParamIds()[yParamId], xIndex, axesParamsComponents[xAxisId]);
		}
	}

	std::list<std::string> legendLines;
	for (auto axisParamsComponents : axesParamsComponents) {
		boost::shared_ptr<Axis> lXAxis = plot->_panel->getAxis(axisParamsComponents.first);
		setAxisLegendForSeries(plot, lXAxis, axisParamsComponents.second);
        }
}

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

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 += "]";
}

void AxisLegendManager::addIndexPart(std::string& legend, int index) {
	legend += "[";
	legend += std::to_string(index);
	legend += "]";
}

void AxisLegendManager::addBoundsPart(std::string& legend, PLFLT min, PLFLT max) {
	legend += " ";

	PLINT axis = 0;
	PLPointer data = NULL;
	char minCount[1024];
	char maxCount[1024];

	generateDigitalLabel(axis, min, minCount, 1024, data);
	generateDigitalLabel(axis, max, maxCount, 1024, data);

	legend += std::string(minCount);
	legend += ", ";
	legend += std::string(maxCount);
}

void AxisLegendManager::addBreakLine(std::string& legend) {
	if (!legend.empty()) {
		legend += Label::DELIMITER;
	}
}

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
		{
			addInfoPart(legend, paramInfo->getShortName());
			addIndexPart(legend, index);
		}
	}

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

} /* namespace plot */