Diffstat (limited to 'noncore/apps/checkbook/graph.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/checkbook/graph.cpp | 85 |
1 files changed, 76 insertions, 9 deletions
diff --git a/noncore/apps/checkbook/graph.cpp b/noncore/apps/checkbook/graph.cpp index bae92da..a0d8b78 100644 --- a/noncore/apps/checkbook/graph.cpp +++ b/noncore/apps/checkbook/graph.cpp @@ -31,2 +31,4 @@ +#include <qcolor.h> +#include <qfontmetrics.h> #include <qpainter.h> @@ -36,4 +38,2 @@ Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags ) { - setBackgroundMode( QWidget::PaletteBase ); - data = d; @@ -84,3 +84,3 @@ void Graph::initGraph() { - drawBarChart(); + drawBarChart( width(), height(), data->maxValue() ); } @@ -89,3 +89,3 @@ void Graph::initGraph() { - drawLineChart(); + //drawLineChart( p, s, min, max ); } @@ -94,3 +94,3 @@ void Graph::initGraph() { - drawPieChart(); + drawPieChart( width(), height(), data->totalValue() ); } @@ -99,13 +99,80 @@ void Graph::initGraph() -void Graph::drawBarChart() +void Graph::drawBarChart( int width, int height, float max ) +{ + QPainter p( &graph ); + + // Try to set the font size smaller for text + QFont f = font(); + f.setPointSize( 8 ); + p.setFont( f ); + + int x = 0; + int i = 0; + int n = data->numberDataPoints(); + QFontMetrics fm=fontMetrics(); + int fh = fm.height(); + int fw; + + QColor c( 0, 0, 255); + p.setBrush( c ); + + for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) + { + int bw = ( width - width / 4 - x ) / ( n - i ); + int bh = int( ( height - height / 4 - 1 ) * dp->value() / max ); + p.drawRect( width / 8 + x, height - height / 8 - 1 - bh, bw, bh ); + fw = fm.width( dp->label() ); + p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8, fw, + fh + height / 8, AlignTop | AlignHCenter, dp->label() ); +// WordBreak | AlignTop | AlignHCenter, dp->label() ); + i++; + x += bw; + } +} + +void Graph::drawLineChart( int width, int height, float max ) { - //Find max value in GraphInfo->dataPoints() - make function in GraphInfo!!! } -void Graph::drawLineChart() +void Graph::drawPieChart( int width, int height, float sum ) { + QPainter p( &graph ); + + // Try to set the font size smaller for text + QFont f = font(); + f.setPointSize( 8 ); + p.setFont( f ); + + int n = data->numberDataPoints(); + + int apos = -90 * 16; + + int xd = width - width / 5; + int yd = height - height / 5; + + int i = 0; + + QColor c; + + for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) + { + c.setHsv( ( i *255) / n, 255, 255 ); + p.setBrush( c ); + + int a = int( ( dp->value() * 360.0 ) / sum * 16.0 + 0.5 ); + p.drawPie( width/10, height/10, xd, yd, -apos, -a ); + apos += a; + i++; } -void Graph::drawPieChart() + double apos2 = -90 * 3.14159 / 180; + for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) { + double a = dp->value() *360 / sum * 3.14159 / 180; + int x = int( cos( apos2 + a/2 ) * width * 5/16 + width/2 + 0.5 ); + int y = int( sin( apos2 + a/2 ) * height * 5/16 + height/2 + 0.5 ); + p.drawText( x - width/8, y - height/8, width/4, height/4, WordBreak | AlignCenter, + dp->label() ); + apos2 += a; + } } |