HistogramSeriesNode.hh 4.27 KB
/*
 * HistogramSeriesNode.hh
 *
 *  Created on: Jan 27, 2023
 *      Author: AKKODIS
 */

#ifndef HISTOGRAMSERIESNODE_HH_
#define HISTOGRAMSERIESNODE_HH_

#include <libxml/globals.h>
#include <libxml/tree.h>
#include <libxml/xmlstring.h>
#include <iosfwd>

#include "FileConfigurator.hh"
#include "DrawingPropertiesNode.hh"
#include "PlotLogger.hh"
#include "NodeCfg.hh"
#include "HistogramSeriesProperties.hh"

namespace plot {


/**
 * Class that handle a <histogram1d> and <histogram2d>  xml node.
 * Template class that should be instanciated for each subclass of PanelOutputPanel.
 */


template<class PlotType>
class HistogramSeriesNode: public plot::DrawingPropertiesNode<PlotType> {
public:
	HistogramSeriesNode(std::string histogramType) :
			DrawingPropertiesNode<PlotType>(), 
			_histogramType(histogramType) {
			DrawingPropertiesNode<PlotType>::getChildList()["bins"] = AMDA::XMLConfigurator::NodeCfgSPtr(new BinsNode(histogramType));
			DrawingPropertiesNode<PlotType>::getChildList()["histotype"] = AMDA::XMLConfigurator::NodeCfgSPtr(new HistotypeNode(histogramType));
	}
	virtual ~HistogramSeriesNode() {
	}
	void proceed(xmlNodePtr pNode_,
			const AMDA::Parameters::CfgContext& pContext_) {
		LOG4CXX_DEBUG(gLogger, "HistogramSeriesNode::proceed - " );
		PlotType* plotOutput = pContext_.get<PlotType*>();
		xmlChar* name = pContext_.get<xmlChar*>();

		// initialize series with default properties
		DrawingProperties defaultProps = plotOutput->getParameter(
				(const char*) name).getDefaultProperties();

		std::shared_ptr<HistogramSeriesProperties> histogramPropsPtr =
				std::shared_ptr<HistogramSeriesProperties>(new HistogramSeriesProperties(defaultProps,_histogramType));

		// parse attributes...
		parseAttributes(pNode_, *histogramPropsPtr.get());
		// add series definition to parameter
		histogramPropsPtr->setParamId((const char*)name);

		plotOutput->getParameter((const char*) name).addHistogramSeriesProperties(histogramPropsPtr);

		//parse children nodes
		AMDA::Parameters::CfgContext context;
		context.push<xmlChar*>(name);
		context.push<HistogramSeriesProperties*>(histogramPropsPtr.get());
		context.push<DrawingProperties*>(histogramPropsPtr.get());
		AMDA::XMLConfigurator::NodeGrpCfg::proceed(pNode_, context);

	}
protected:
	void parseAttributes(xmlNodePtr pNode_, DrawingProperties& props_) {
		// read parent attributes
		DrawingPropertiesNode<PlotType>::parseAttributes(pNode_, props_);
		// specific series node attributes :
		xmlChar * value = NULL;

		value = xmlGetProp(pNode_, (const xmlChar*) "index");
		if (value) {
			AMDA::Common::ParameterIndexComponentList indexList;
			if (!AMDA::Common::ParameterIndexesTool::parse((const char*) value, -1, -1, indexList) || indexList.empty())
			{
				LOG4CXX_ERROR(gLogger, "HistogramSeriesNode::parseAttributes - Bad index definition " << (const char*) value);
				xmlFree(value);
				BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_ERROR_UNKNOWN) << AMDA::ex_msg("Bad HistogramSerieNode index definition"));
			}
			((HistogramSeriesProperties&) props_).setIndex(indexList.front());
			xmlFree(value);
		} else {
			((HistogramSeriesProperties&) props_).setIndex(AMDA::Common::ParameterIndexComponent(-1,-1)); // default case: if no index attribute, we use default index value -1 which means for all series.
		}

		value = xmlGetProp(pNode_, (const xmlChar*) "id");
		if (value) {
			((HistogramSeriesProperties&) props_).setId(atoi((const char*) value));
			xmlFree(value);
		}

		// only available for 1D histo
		if(_histogramType == "histogram1d"){
			// -- color
			Color color(0, 255, 0);
			value = xmlGetProp(pNode_, (const xmlChar *)"color");
			if (value)
			{
				try
				{
					std::string strValue((const char *)value);
					createColor(color, strValue);
					((HistogramSeriesProperties &)props_).setColor(color);
				}
				catch (std::logic_error &e)
				{
					LOG4CXX_WARN(gLogger, "Histogram1D Color : " << e.what());
				}
				xmlFree(value);
			}
		}

		// only available for 2D histo
		if(_histogramType == "histogram2d"){
			// -- associated x serie id
			value = xmlGetProp(pNode_, (const xmlChar*) "xId");
			if (value) {
				((HistogramSeriesProperties&) props_).setXId(atoi((const char*) value));
				xmlFree(value);
			}
		}
	}

	std::string _histogramType;
};

/* namespace plot */
}
#endif /* HISTOGRAMSERIESNODE_HH_ */