Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

graph.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the OPIE Project
00003                =.
00004              .=l.            Copyright (c)  2002 Dan Williams <drw@handhelds.org>
00005            .>+-=
00006  _;:,     .>    :=|.         This file is free software; you can
00007 .> <`_,   >  .   <=          redistribute it and/or modify it under
00008 :`=1 )Y*s>-.--   :           the terms of the GNU General Public
00009 .="- .-=="i,     .._         License as published by the Free Software
00010  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00011      ._= =}       :          or (at your option) any later version.
00012     .%`+i>       _;_.
00013     .i_,=:_.      -<s.       This file is distributed in the hope that
00014      +  .  -:.       =       it will be useful, but WITHOUT ANY WARRANTY;
00015     : ..    .:,     . . .    without even the implied warranty of
00016     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00017   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU General
00018 ..}^=.=       =       ;      Public License for more details.
00019 ++=   -.     .`     .:
00020  :     =  ...= . :.=-        You should have received a copy of the GNU
00021  -.   .:....=;==+<;          General Public License along with this file;
00022   -_. . .   )=.  =           see the file COPYING. If not, write to the
00023     --        :-=`           Free Software Foundation, Inc.,
00024                              59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 #include "graph.h"
00030 #include "graphinfo.h"
00031 
00032 #include <qpainter.h>
00033 
00034 #include <math.h>
00035 
00036 Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags )
00037         : QWidget( parent, name, flags )
00038 {
00039         data = d;
00040 
00041         graph.setOptimization( QPixmap::BestOptim );
00042 }
00043 
00044 void Graph::setGraphInfo( GraphInfo *d )
00045 {
00046         data = d;
00047 }
00048 
00049 void Graph::drawGraph( bool regen )
00050 {
00051         if ( regen )
00052         {
00053                 initGraph();
00054         }
00055         QPainter p( this );
00056         p.drawPixmap( 0, 0, graph );
00057 }
00058 
00059 void Graph::paintEvent( QPaintEvent * )
00060 {
00061         drawGraph( FALSE );
00062 }
00063 
00064 void Graph::resizeEvent( QResizeEvent * )
00065 {
00066         drawGraph( TRUE );
00067 }
00068 
00069 void Graph::initGraph()
00070 {
00071         graph.resize( width(), height() );
00072         graph.fill( QColor( 255, 255, 255 ) );
00073 
00074         if ( !data )
00075         {
00076                 return;
00077         }
00078 
00079         // Any common stuff here (titles, ???)
00080 
00081         switch ( data->graphType() )
00082         {
00083                 case GraphInfo::BarChart :
00084                 {
00085                         drawBarChart( width(), height(), data->maxValue(), data->minValue() );
00086                 }
00087                 break;
00088                 case GraphInfo::PieChart :
00089                 {
00090                         drawPieChart( width(), height(), data->totalValue() );
00091                 }
00092         };
00093 }
00094 
00095 void Graph::drawBarChart( int width, int height, float max, float min )
00096 {
00097         QPainter p( &graph );
00098 
00099         // Try to set the font size smaller for text
00100         // it would be nice to get _pCfg->getUseSmallFont here
00101         QFont f = font();
00102         f.setPointSize( f.pointSize()-1 );
00103         p.setFont( f );
00104 
00105         int x = 0;
00106         int i = 0;
00107         int n = data->numberDataPoints();
00108         QFontMetrics fm=fontMetrics();
00109         int fh = fm.height();
00110         int fw;
00111 
00112         QColor c( 0, 0, 255);
00113         p.setBrush( c );
00114 
00115     if ( min > 0 )
00116         min = 0.0;
00117 
00118         int bw = ( width - width / 4 ) / n;
00119     int hoffset =  int( ( height - height / 4 - 1 ) * ( min * -1 ) / ( max - min ) );
00120         for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() )
00121         {
00122         int bh = int( ( height - height / 4 - 1 ) * dp->value() / ( max - min ) );
00123                 p.drawRect( width / 8 + x, height - height / 8 - 1 - hoffset - bh, bw, bh );
00124                 fw = fm.width( dp->label() );
00125                 p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8 - hoffset, fw,
00126                                         fh + height / 8, AlignTop | AlignHCenter, dp->label() );
00127                 i++;
00128                 x += bw;
00129         }
00130 }
00131 
00132 void Graph::drawPieChart( int width, int height, float sum )
00133 {
00134         QPainter p( &graph );
00135 
00136         // Try to set the font size smaller for text
00137         // it would be nice to get _pCfg->getUseSmallFont here
00138         QFont f = font();
00139         f.setPointSize( f.pointSize()-1 );
00140         p.setFont( f );
00141 
00142         int n = data->numberDataPoints();
00143 
00144         int apos = -90 * 16;
00145 
00146         int xd = width - width / 5;
00147         int yd = height - height / 5;
00148 
00149         int i = 0;
00150 
00151         QColor c;
00152 
00153         for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() )
00154         {
00155                 c.setHsv( ( i *255) / n, 255, 255 );
00156                 p.setBrush( c );
00157 
00158                 int a = int( ( dp->value() * 360.0 ) / sum * 16.0 + 0.5 );
00159                 p.drawPie( width/10, height/10, xd, yd, -apos, -a );
00160                 apos += a;
00161                 i++;
00162         }
00163 
00164         double apos2 = -90 * 3.14159 / 180;
00165         for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() )
00166         {
00167                 double a = dp->value() *360 / sum * 3.14159 / 180;
00168                 int x = int( cos( apos2 + a/2 ) * width * 5/16 + width/2 + 0.5 );
00169                 int y = int( sin( apos2 + a/2 ) * height * 5/16 + height/2 + 0.5 );
00170                 p.drawText( x - width/8, y - height/8, width/4, height/4, WordBreak | AlignCenter,
00171                                         dp->label() );
00172                 apos2 += a;
00173         }
00174 }
00175 

Generated on Sat Nov 5 16:16:44 2005 for OPIE by  doxygen 1.4.2