00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
00100
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
00137
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