DrawCounter.hpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*
** This file is part of the ViTE project.
**
** This software is governed by the CeCILL-A license under French law
** and abiding by the rules of distribution of free software. You can
** use, modify and/or redistribute the software under the terms of the
** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
** URL: "http://www.cecill.info".
Mathieu Faverge's avatar
Mathieu Faverge committed
9
**
10 11 12 13 14
** As a counterpart to the access to the source code and rights to copy,
** modify and redistribute granted by the license, users are provided
** only with a limited warranty and the software's author, the holder of
** the economic rights, and the successive licensors have only limited
** liability.
Mathieu Faverge's avatar
Mathieu Faverge committed
15
**
16 17 18 19 20 21 22 23 24 25
** In this respect, the user's attention is drawn to the risks associated
** with loading, using, modifying and/or developing or reproducing the
** software by the user in light of its specific status of free software,
** that may mean that it is complicated to manipulate, and that also
** therefore means that it is reserved for developers and experienced
** professionals having in-depth computer knowledge. Users are therefore
** encouraged to load and test the software's suitability as regards
** their requirements in conditions enabling the security of their
** systems and/or data to be ensured and, more generally, to use and
** operate it in the same conditions as regards security.
Mathieu Faverge's avatar
Mathieu Faverge committed
26
**
27 28 29 30 31 32 33 34 35 36 37 38 39
** The fact that you are presently reading this means that you have had
** knowledge of the CeCILL-A license and that you accept its terms.
**
**
** ViTE developers are (for version 0.* to 1.0):
**
**        - COULOMB Kevin
**        - FAVERGE Mathieu
**        - JAZEIX Johnny
**        - LAGRASSE Olivier
**        - MARCOUEILLE Jule
**        - NOISETTE Pascal
**        - REDONDY Arthur
Mathieu Faverge's avatar
Mathieu Faverge committed
40
**        - VUCHENER Clément
41 42 43 44 45 46 47 48 49
**
*/
/*!
 *\file DrawCounter.hpp
 */

#ifndef DRAW_COUNTER_HPP
#define DRAW_COUNTER_HPP

François Trahay's avatar
François Trahay committed
50 51
#include "DrawStats.hpp"

52
#define _MAX_HEIGHT_AXIS_DEFAULT _HEIGHT_FOR_ONE_CONTAINER_DEFAULT - 50
53
#define _MAX_WIDTH_AXIS_DEFAULT  350
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
/*!
 * \class DrawCounter
 * \brief Browse the stats and call back T drawing methods
 */
template<class T>
class DrawCounter : public DrawStats<T> {
private:
    int _nb_variables_already_print;

    // Origin of the graph
    double _start_x;
    double _start_y;

    // Geometrical informations about the stats shape.
    double _size_for_one_variable;
    double _pos_x_container_name;
    double _pos_y_container_name;
    double _x_axis_length;
    double _y_axis_length;

public:
    /*
     * \brief The default constructor
     */
    DrawCounter() {
        this->_size_for_one_variable  = _HEIGHT_FOR_ONE_CONTAINER_DEFAULT;
        _x_axis_length                = _MAX_WIDTH_AXIS_DEFAULT;
        _y_axis_length                = _MAX_HEIGHT_AXIS_DEFAULT;
        this->_pos_x_container_name   = _POS_X_CONTAINER_NAME;
83
        this->_pos_y_container_name   = 10;
84 85 86 87 88
        _nb_variables_already_print   = 0;
        _start_x                      = _START_HISTOGRAM_X_DEFAULT;
        _start_y                      = 20.;
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
89

90 91 92
    /*!
     * \brief The destructor
     */
93
    virtual ~DrawCounter() = default;
94 95 96 97 98 99 100 101 102 103 104 105 106 107


    /*!
     * \fn build(T* draw_object, std::vector<Container *> containers_to_print)
     * \brief The trace building function.
     * \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
     * \param containers_to_print the container's data.
     */
    void build(T* draw_object, std::vector<Container *> containers_to_print) {
        int nbcont;

        draw_object->clear();

        this->_containers_to_print = containers_to_print;
Mathieu Faverge's avatar
Mathieu Faverge committed
108

109 110 111 112 113 114 115
        nbcont = this->_containers_to_print.size();

        draw_object->start_draw();
        draw_object->set_total_width(draw_object->width());

        // For each container
        for(int i = 0 ; i < nbcont ; i ++) {
116
            const std::map<VariableType *, Variable *> *variables = this->_containers_to_print[i]->get_variables();
117
            // We print each variable
118
            for(std::map<VariableType *, Variable *>::const_iterator it = variables->begin() ;
119 120 121 122 123 124 125 126 127
                it != variables->end() ;
                it ++) {
                draw_diagram(draw_object, i, (*it).second);
                _nb_variables_already_print ++;
            }
        }

        draw_object->set_total_height((_nb_variables_already_print-1)*this->_size_for_one_variable);

Mathieu Faverge's avatar
Mathieu Faverge committed
128
        this->end_draw(draw_object);
129 130 131 132 133 134 135 136 137 138
    }

    /*!
     * \fn draw_diagram(T* draw_object, const int container_id, const Variable *var)
     * \brief Print the i-th diagram
     * \param draw_object the kind of object which will be drawn (OpenGL, SVG...).
     * \param container_id the id of the container.
     * \param var the variable printed
     */
    void draw_diagram(T* draw_object, const int container_id, const Variable *var) {
Mathieu Faverge's avatar
Mathieu Faverge committed
139
        int         pos_x, pos_y;
140 141
        std::string container_name;
        double      duration;
Mathieu Faverge's avatar
Mathieu Faverge committed
142 143 144

        duration = this->_end_time - this->_start_time;

145
        // Position for the origin of the graph
Mathieu Faverge's avatar
Mathieu Faverge committed
146 147
        pos_x = _start_x;
        pos_y = _start_y - this->_size_for_one_variable * _nb_variables_already_print;
148

Mathieu Faverge's avatar
Mathieu Faverge committed
149 150 151
        // Draw the container name
        container_name = this->_containers_to_print[container_id]->get_Name().to_string();
        draw_object->draw_text(this->_pos_x_container_name,
152
                               this->_pos_y_container_name + 30 - pos_y,
Mathieu Faverge's avatar
Mathieu Faverge committed
153
                               container_name);
154
        // Draw the variable name
Mathieu Faverge's avatar
Mathieu Faverge committed
155
        draw_object->draw_text(this->_pos_x_container_name,
156
                               this->_pos_y_container_name + 50 - pos_y,
Mathieu Faverge's avatar
Mathieu Faverge committed
157
                               var->get_type()->get_name());
158 159

        // Draw the axis
Mathieu Faverge's avatar
Mathieu Faverge committed
160
        draw_object->draw_axis(pos_x,
161 162 163 164 165 166
                               pos_y,
                               _x_axis_length,
                               _y_axis_length);

        // Draw the legend axis
        // Time unit
167 168
        draw_object->draw_text(pos_x+_x_axis_length+10.,
                               _HEIGHT_FOR_ONE_CONTAINER_DEFAULT-pos_y,
169 170
                               "time unit");
        // Variable unit
171
        if(var->get_extra_fields() != nullptr && var->get_extra_fields()->find(std::string("unit")) != var->get_extra_fields()->end()) {
172 173
            const String *var_unit = (const String *)var->get_extra_fields()->find(std::string("unit"))->second;
            draw_object->draw_text(pos_x,
174
                                   _HEIGHT_FOR_ONE_CONTAINER_DEFAULT-(pos_y+_y_axis_length+10.) ,
175 176 177 178
                                   "("+var_unit->to_string()+")");
        }
        else {
            draw_object->draw_text(pos_x,
179
                                   _HEIGHT_FOR_ONE_CONTAINER_DEFAULT-(pos_y+_y_axis_length+10.) ,
180 181 182 183 184 185 186 187 188 189 190 191
                                   "no unit");
        }

        // Draw some values for the axis
        // Formula : duration*(length/axis_length) + start_time;
        const double min_value = var->get_min().get_value();
        const double max_value = var->get_max().get_value();
        if(max_value != min_value) {
            const int nb_values = 6;
            for(int i = 0 ; i <= nb_values ; i ++) {
                // x value -> time
                draw_object->draw_text(pos_x+i*_x_axis_length/nb_values,
192
                                       _HEIGHT_FOR_ONE_CONTAINER_DEFAULT-(pos_y-15.) ,
193 194 195 196 197 198
                                       QString::number(duration*(double)i/(double)nb_values+this->_start_time, 'f', 3).toStdString());
                draw_object->draw_vertical_line(pos_x+i*_x_axis_length/nb_values,
                                                pos_y-5,
                                                5);
                // y value -> counter
                draw_object->draw_text(pos_x-30. ,
199
                                       _HEIGHT_FOR_ONE_CONTAINER_DEFAULT-(pos_y+i*_y_axis_length/nb_values),
200 201 202 203 204 205 206 207 208
                                       QString::number((max_value-min_value)*i/(double)nb_values+min_value, 'f', 1).toStdString());
                draw_object->draw_horizontal_line(pos_x-5,
                                                  pos_y+i*_y_axis_length/nb_values,
                                                  5);
            }
        }
        else {
            // Only one in the middle
            draw_object->draw_text(pos_x-30. ,
209
                                   _HEIGHT_FOR_ONE_CONTAINER_DEFAULT-(pos_y+_y_axis_length/2),
210 211 212 213 214 215
                                   QString::number(max_value, 'f', 1).toStdString());
            draw_object->draw_horizontal_line(pos_x-5,
                                              pos_y+_y_axis_length/2,
                                              5);
        }

Mathieu Faverge's avatar
Mathieu Faverge committed
216
        // Draw the variable
217
        // Formula : x = (time-begin)*(x_length)/duration + decalage
218
        const std::list<std::pair<Date, Double> > *values = var->get_values();
219 220 221 222 223 224 225 226 227 228 229 230 231
        double x          = 0. ,
               y          = 0. ,
               prev_x     = pos_x,
               prev_y     = pos_y;
        double median     = 0.;
        double time       = 0.,
               prev_time  = this->_start_time;
        double value      = 0.,
               prev_value = min_value;

        bool is_last_value = false; // If we get a time bigger than the end_time

        if(max_value != min_value) {
Mathieu Faverge's avatar
Mathieu Faverge committed
232
          for (std::list<std::pair<Date, Double> >::const_iterator it = values->begin();
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
                 it != values->end();
                 it ++) {
                time  = (*it).first.get_value();
                value = (*it).second.get_value();

                if(time < this->_start_time) {
                    continue;
                }
                if(time >= this->_end_time) {
                    time = this->_end_time;
                    is_last_value = true;
                }

                x = (time-this->_start_time)*_x_axis_length/duration + pos_x;
                y = (value-min_value)*_y_axis_length/(max_value-min_value) + pos_y;
                draw_object->draw_line(prev_x, prev_y, x, prev_y);
                draw_object->draw_line(x, prev_y, x, y);
                median += prev_value*(time-prev_time);
                prev_x = x;
                prev_y = y;
                prev_time = time;
                prev_value = value;
                if(is_last_value) {
                    break;
                }
            }

            draw_object->draw_horizontal_line(x, y,
                                              pos_x+_x_axis_length-x);

            median /= duration;


//             std::cout << "min : " << min_value <<  " max : " << max_value << " Median : " << median << std::endl;
//             // Draw the median
//             draw_object->draw_horizontal_line(pos_x,
//                                               pos_y+_y_axis_length*(median-min_value)/(max_value-min_value),
//                                               _x_axis_length);
        }
        else {
            // Draw the median
            draw_object->draw_horizontal_line(pos_x,
                                              pos_y+_y_axis_length/2,
                                              _x_axis_length);
        }
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
279

280 281 282
};

#endif