00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "whiskerplot.h"
00019
00020 #include <algorithm>
00021
00022 #include <qpainter.h>
00023
00024 #include "fungmath.h"
00025 #include "statinfo.h"
00026
00027 WhiskerPlot::WhiskerPlot(std::vector<double> & xlist, bool show, const QColor & color) :
00028 PlotModule(show,color),
00029 _xlist(xlist)
00030 {
00031 std::sort(_xlist.begin(), _xlist.end() );
00032
00033 _median = StatInfo::median(_xlist);
00034 _first_quartile = StatInfo::first_quartile(_xlist);
00035 _third_quartile = StatInfo::third_quartile(_xlist);
00036 _min = StatInfo::min(_xlist);
00037 _max = StatInfo::max(_xlist);
00038 }
00039
00040 void WhiskerPlot::drawPlot( QPainter *painter, int *index, StatPlotGraph *graph, double, double )
00041 {
00042 painter->setPen(QPen(color(),2));
00043 const int whiskerAxis = -1 - boxAndWhiskerHeight/2;
00044 int totalWhiskerPlots = *index;
00045 const int y_coord_middle = whiskerAxis-totalWhiskerPlots*(boxAndWhiskerHeight+2);
00046 const int y_coord_top = y_coord_middle+boxAndWhiskerHeight/2;
00047 const int y_coord_bottom = y_coord_middle-boxAndWhiskerHeight/2;
00048
00049 painter->fillRect(toPixelXCoord(graph,_min)-3,toPixelYCoord(graph,y_coord_middle)-3,6,6,QBrush(color(),Qt::Dense1Pattern));
00050 painter->fillRect(toPixelXCoord(graph,_max)-3,toPixelYCoord(graph,y_coord_middle)-3,6,6,QBrush(color(),Qt::Dense1Pattern));
00051 painter->drawLine(
00052 toPixelXCoord(graph,_median),
00053 toPixelYCoord(graph,y_coord_top),
00054 toPixelXCoord(graph,_median),
00055 toPixelYCoord(graph,y_coord_bottom));
00056 painter->drawLine(
00057 toPixelXCoord(graph,_first_quartile),
00058 toPixelYCoord(graph,y_coord_top),
00059 toPixelXCoord(graph,_first_quartile),
00060 toPixelYCoord(graph,y_coord_bottom));
00061 painter->drawLine(
00062 toPixelXCoord(graph,_third_quartile),
00063 toPixelYCoord(graph,y_coord_top),
00064 toPixelXCoord(graph,_third_quartile),
00065 toPixelYCoord(graph,y_coord_bottom));
00066 painter->drawLine(
00067 toPixelXCoord(graph,_first_quartile),
00068 toPixelYCoord(graph,y_coord_bottom),
00069 toPixelXCoord(graph,_third_quartile),
00070 toPixelYCoord(graph,y_coord_bottom));
00071 painter->drawLine(
00072 toPixelXCoord(graph,_third_quartile),
00073 toPixelYCoord(graph,y_coord_top),
00074 toPixelXCoord(graph,_first_quartile),
00075 toPixelYCoord(graph,y_coord_top));
00076 painter->drawLine(
00077 toPixelXCoord(graph,_first_quartile),
00078 toPixelYCoord(graph,y_coord_middle),
00079 toPixelXCoord(graph,_min),
00080 toPixelYCoord(graph,y_coord_middle));
00081 painter->drawLine(
00082 toPixelXCoord(graph,_third_quartile),
00083 toPixelYCoord(graph,y_coord_middle),
00084 toPixelXCoord(graph,_max),
00085 toPixelYCoord(graph,y_coord_middle));
00086
00087 _vertical = *index;
00088
00089 *index += 1;
00090 }
00091
00092 void WhiskerPlot::trace( double x, double, double *new_x, double *new_y )
00093 {
00094 if ( x <= (_min + _first_quartile)/2 )
00095 *new_x = _min;
00096 else if ( x > (_min+_first_quartile)/2 && x<=(_first_quartile+_median)/2 )
00097 *new_x = _first_quartile;
00098 else if ( x > (_first_quartile+_median)/2 && x<=(_median+_third_quartile)/2 )
00099 *new_x = _median;
00100 else if ( x > (_median+_third_quartile)/2 && x<=(_third_quartile+_max)/2 )
00101 *new_x = _third_quartile;
00102 else if ( x > (_third_quartile+_max)/2 )
00103 *new_x = _max;
00104
00105 *new_y = (-1 - boxAndWhiskerHeight/2) - ( _vertical * (boxAndWhiskerHeight+2) );
00106 }
00107
00108 std::string WhiskerPlot::updateCoords( double x, double )
00109 {
00110 std::string message;
00111
00112 if ( x == _min )
00113 message = "Minimum: ";
00114 if ( x == _max )
00115 message = "Maximum: ";
00116 if ( x == _first_quartile )
00117 message = "First Quartile: ";
00118 if ( x == _third_quartile )
00119 message = "Third Quartile: ";
00120 if ( x == _median )
00121 message = "Median: ";
00122
00123 char *s = new char( num_digits( static_cast<int>(x) ) + 3 );
00124 dtoa(x,s);
00125 message.append( s );
00126 delete[] s;
00127
00128 return message;
00129 }
00130
00131 void WhiskerPlot::zoomStat( double *xmin, double *xmax, double *, double * )
00132 {
00133 *xmin = _min-1;
00134 *xmax = _max+1;
00135 }
00136
00137