Home

Download

Features

Screenshots

Handbook

Browse Source

Authors

SourceForge.net Logo
Hosted by SourceForge.net

OSI Certified


Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   Related Pages   Search  

frequencyplot.cpp

00001 /***************************************************************************
00002                           frequencyplot.cpp  -  description
00003                              -------------------
00004     begin                : Sun May 11 2003
00005     copyright            : (C) 2003 by Fungmeista
00006     email                : mizunoami44@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "frequencyplot.h"
00019 
00020 #include <cmath>
00021 #include <algorithm> //sort
00022 
00023 #include <qpainter.h>
00024 
00025 #include "fungmath.h"
00026 #include "statinfo.h"
00027 
00028 FrequencyPlot::FrequencyPlot(std::vector<double> & xlist, int classes, bool show, const QColor & color) :
00029     PlotModule(show,color),
00030     _xlist(xlist),
00031     _classes(classes)
00032 {
00033     unsigned int xElements = _xlist.size();
00034 
00035     std::sort( _xlist.begin(), _xlist.end() );
00036 
00037     _interval = (_xlist[xElements-1]-_xlist[0]+1)/static_cast<double>(_classes);
00038 
00039     _frequency.resize(_classes);
00040     for (int i=0; i<_classes ; i++)
00041         _frequency[i] = 0;
00042     
00043     int x=0;
00044     for (double i=_xlist[0]; i<=_xlist[xElements-1]; i+=_interval)
00045     {
00046         for (unsigned int j=0; j<xElements; j++)
00047         {
00048             if (_xlist[j]>=i && _xlist[j]<i+_interval)
00049                 _frequency[x]++;
00050         }
00051         x++;
00052     }
00053     _min = _xlist[0];
00054 }
00055 
00056 void FrequencyPlot::drawPlot( QPainter *painter, int *, StatPlotGraph *graph, double xScale, double yScale )
00057 {
00058     int x=0;
00059     for (double i=_xlist[0]; i<=_xlist[_xlist.size()-1]; i+=_interval)
00060     {
00061         painter->setRasterOp(Qt::XorROP);
00062         painter->fillRect(
00063             toPixelXCoord(graph,i)+1,
00064             toPixelYCoord(graph,0)+(int)(ROUND(yScale*(_frequency[x]))),
00065             (int)(ROUND(xScale*_interval)),
00066             (int)(ROUND(-yScale*_frequency[x])),
00067             QBrush(color(),Qt::Dense4Pattern)
00068         );
00069         painter->setRasterOp(Qt::CopyROP);
00070         painter->setPen(QPen(QColor(0,0,0),2));
00071         painter->drawRect(
00072             toPixelXCoord(graph,i)+1,
00073             toPixelYCoord(graph,0)+(int)(ROUND(yScale*_frequency[x])),
00074             (int)(ROUND(xScale*_interval)),
00075             (int)(ROUND(-yScale*_frequency[x]))
00076         );
00077         x++;
00078     }
00079 }
00080 
00081 void FrequencyPlot::trace( double x, double, double *new_x, double *new_y )
00082 {
00083     for (double i=_min; i<_min+_interval*(_classes-1); i+=_interval)
00084     {
00085         if ( x > i && x <= i + _interval )
00086         {
00087             *new_x = i+_interval/2;
00088             *new_y = StatInfo::frequency(_xlist,i,i+_interval);
00089             break;
00090         }
00091     }
00092     if (x <= _min + _interval)
00093     {
00094         *new_x = _min+_interval/2;
00095         *new_y = StatInfo::frequency(_xlist,_min,_min+_interval);
00096     }
00097     else if (x > _min + _interval * ( _classes - 1 ) )
00098     {
00099         *new_x = _min+_interval*(_classes-1)+_interval/2;
00100         *new_y = StatInfo::frequency(_xlist,_min+_interval*(_classes-1),_min+_interval*(_classes-1)+_interval);
00101     }
00102 }
00103 
00104 std::string FrequencyPlot::updateCoords( double x, double y )
00105 {
00106     std::string message;
00107 
00108     char *s1 = new char(num_digits(static_cast<int>(y)));
00109     itoa(static_cast<int>(y),s1);
00110     message.append(s1);
00111     delete[] s1;
00112 
00113     
00114     message.append(" elements(s) between ");
00115 
00116     char *s2 = new char( num_digits( static_cast<int>(x+_interval/2) ) + 3 );
00117     dtoa(x-_interval/2,s2);
00118     message.append( s2 );
00119     delete[] s2;
00120 
00121     message.append(" and ");
00122 
00123     char *s3 = new char( num_digits( static_cast<int>(x+_interval/2) ) + 3 );
00124     dtoa(x+_interval/2,s3);
00125     message.append( s3 );
00126     delete[] s3;
00127 
00128     return message;
00129 }
00130 
00131 void FrequencyPlot::zoomStat( double *xmin, double *xmax, double *ymin, double *ymax )
00132 {
00133     *xmax = _min+(_interval*((double)_classes+1));
00134     *xmin = _min-(_interval);
00135     *ymin = 0;
00136     double maxFreq = 0;
00137     for (int i=0; i<_classes; i++)
00138         maxFreq = StatInfo::bigger(_frequency[i],maxFreq);  //go through all intervals to find the one with the most elements where yMax should be
00139     *ymax = maxFreq+1;
00140 }
00141