PlotFunction.hh 8.83 KB

#ifndef PLOTFUNCTION_HH_
#define PLOTFUNCTION_HH_

// includes boost
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <fstream>
#include "NodeCfg.hh"
#include "PanelPlotOutput.hh"
#include "PlPlotUtil.hh"
#include "PlotFunctionNode.hh"
#include "plotFunctionElements/Abscisse.hh"
#include <climits>

#include "DefaultTimeAxisDecorator.hh"

#include "TimeAxisDecorator.hh"

#include "plplot/plplot.h"
#include "plplot/plplotP.h"
#include "plplot/plstream.h"
#include <map>

#include "plplot/plstream.h"

extern "C"
{
#include <plplot/qsastime.h>
}

/**
 * @brief Id pour l'axe X qui sera crée
 *
 */
#define X_AXIS_ID "xaxis_id"
#define X_AXIS_ID1 "xaxis_id1"
/**
 * @brief Id pour l'axe Y qui sera crée
 *
 */
#define Y_AXIS_ID "y-left"

namespace plot
{

    class PlotFunction : public PanelPlotOutput
    {
    public:
        /**
         * @brief enum to represent the type of fucntion to apply for plotfunction feature
         *
         */
        enum Function
        {
            NONE,
            AVG,
            FFT,
            DFT,
            SUM
        };

        PlotFunction(AMDA::Parameters::ParameterManager &manager,
                     boost::shared_ptr<Panel> panel, TimeAxisDecorator *timeAxisDecorator = new DefaultTimeAxisDecorator());
        virtual ~PlotFunction();

        /**
         * @overload plot::PanelPlotOutput
         */
        virtual const std::string typeName()
        {
            return PlotFunction_NODENAME;
        }

        /**
         * @overload plot::PanelPlotOutput
         */
        virtual const std::string subTypeName()
        {
            return PlotFunction_NODENAME;
        }

        void setAbscisse(Abscisse abscisse_)
        {
            abscisse = abscisse_;
        };

        Abscisse getAbscisse()
        {
            return abscisse;
        };

        Function getFunction()
        {
            return function;
        };

        void setFunction(Function function_)
        {
            function = function_;
        };

        void setAbscisseScale(Axis::Scale scale)
        {
            abscisseScale = scale;
        };

        Axis::Scale getAbscisseScale()
        {
            return abscisseScale;
        };

        Axis::Scale getOrdonneeScale()
        {
            return ordonneeScale;
        };

        void setOrdonneeScale(Axis::Scale scale)
        {
            ordonneeScale = scale;
        };

        static QSASConfig *qsasconfig;

        void setTimeAxisDecorator(std::shared_ptr<TimeAxisDecorator> pDecorator_)
        {
            _xdecoratorPtr = pDecorator_;
        }

        std::shared_ptr<TimeAxisDecorator> getTimeAxisDecorator()
        {
            return _xdecoratorPtr;
        }

        bool isFourier()
        {
            return function == PlotFunction::Function::FFT || function == PlotFunction::Function::DFT;
        }

        std::string functionStr()
        {
            switch (function)
            {
            case PlotFunction::Function::AVG:
                return "AVERAGE";
            case PlotFunction::Function::SUM:
                return "SUM";
            case PlotFunction::Function::FFT:
                return "FFT";
            case PlotFunction::Function::DFT:
                return "DFT";
            default:
                return "NONE";
            }
        }

    /**
	 * @overload PanelPlotOutput::getLayoutConstraint (void)
	 * @brief Defines the layout constraint to be used for the panel when used within a layout
	 */
	virtual PanelConstraint getLayoutConstraint (void) {
		return PanelConstraint::Square;
	}

    protected:
        /**
         *@brief Draw the serie of a parameter component on plot.
         */
        virtual void
        drawSeries(double startDate, double stopDate, int intervalIndex, std::string pParamId,
                   SeriesProperties &pSerie,
                   AMDA::Common::ParameterIndexComponent pParamIndex,
                   ParameterAxes &param, bool moreThanOneSerieForAxis);

        virtual void writeDataFile(double startTime, double stopTime);

        virtual void preparePlotArea(double startTime, double stopTime, int intervalIndex, std::vector<std::string>* ttNames = NULL);
        /**
         * @brief Get original id of the concerned parameter (for the plot) then add it to the usedParametersId_ and store it in originalParamId
         *
         * @param usedParametersId_
         */
        virtual void createParameters(std::list<std::string> &usedParametersId_);
        /**
         * @brief draw the plot for the current time interval
         */
        virtual bool draw(double startTime, double stopTime, int intervalIndex,
                          bool isFirstInterval, bool isLastInterval);

    private:
        /**
         * @brief Format a time to given format
         * 
         * @param time given time 
         * @param format given format
         * @return std::string 
         */
        std::string formatTime(double time, std::string format)
        {
            long int lTime = static_cast<long int>(time);
            tm *lTimeTm = gmtime(&lTime);
            char lTimeChr[80];

            // Format date.
            strftime(lTimeChr, 80, format.c_str(), lTimeTm);
            std::string lTimeStr = lTimeChr;
            return lTimeStr;
        }
        /**
         * @brief Add a title to panel
         * 
         * @param startTime add startime
         * @param stopTime add stoptime
         */
        void addTitleToPanel(double startTime, double stopTime);
        /**
         * @brief draw Series for compued data for given spectro
         *
         * @param startDate
         * @param stopDate
         * @param intervalIndex
         */
        void drawSeriesForSpectro(double startDate, double stopDate, int intervalIndex);
        /**
         * @brief Apply a function either for Series or Spectro
         *
         */
        void applyFunction();
        /**
         * @brief Configure axis ranges
         *
         */
        void setAxisRange();
        /**
         * @brief Configure axis legend
         *
         */
        void configureAxisLegend();
        /**
         * @brief Afficher sur le plot the starting date
         *
         * @param _timeFormat le format
         * @param startTime le start time envoyé depuis le l'ihm
         * @param stopTime le stop time envoyé depuis le l'ihm
         */
        void drawStartDate(std::string _timeFormat, double startTime, double stopTime);
        /**
         * @brief Apply log scaling on given data
         *
         * @param vect vector of data
         */
        void applyScale(std::vector<double> &vectX, std::vector<double> &vectY);
        /**
         * @brief Get the Min Max de xValuesMap et yValuesMap
         *
         * @param minToFill la variable qui store la valeur minimale
         * @param maxToFill la variable qui store la valeur maximale
         */
        void getMinMax(std::map<std::string, std::vector<double>>, double *minToFill, double *maxToFill);
        /**
         * @brief Compute function
         *
         * @param pParamIndex index of Series or (-1,-1) if it is spectro
         * @param ParameterData parameter data
         * @param pSerie pointer to series
         * @param pSpectro pointer to spectro
         * @param paramOriginID origin param ID
         */
        void compute(AMDA::Common::ParameterIndexComponent pParamIndex, plot::ParameterData &data, SeriesProperties *pSerie, SpectroProperties *pSpectro, std::string paramOriginID);
        /**
         * @brief Une map pour stocker les valeurs calculées de l'axe X associés aux pseudo paramètres crées localement
         *
         */
        std::map<std::string, std::vector<double>> xValuesMap;
        /**
         * @brief Une map pour stocker les valeurs calculées de l'axe Y associés aux pseudo paramètres crées localement
         *
         */
        std::map<std::string, std::vector<double>> yValuesMap;
        /**
         * @brief Une map pour stocker les valeurs x-originales
         *
         */
        std::map<std::string, std::vector<double>> xValuesMapOriginalValues;
        /**
         * @brief Une map pour stocker les valeurs y-orginales
         *
         */
        std::map<std::string, std::vector<double>> yValuesMapOriginalValues;
        /**
         * @brief function type to be applied
         *  @see Function
         */
        Function function;
        /**
         * @brief Abscisse type for Fourier
         * @see Abscisse
         *
         */
        Abscisse abscisse;
        /**
         * @brief Scale of x-axis
         *
         */
        Axis::Scale abscisseScale;
        /**
         * @brief Scale for y-axis
         *
         */
        Axis::Scale ordonneeScale;
        std::shared_ptr<TimeAxisDecorator> _xdecoratorPtr;
        /**
         * @brief Flag to store if is spectro
         *
         */
        bool isSpectro = false;
    };
}
#endif