summaryrefslogtreecommitdiff
path: root/noncore/apps/checkbook/graph.cpp
Unidiff
Diffstat (limited to 'noncore/apps/checkbook/graph.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/checkbook/graph.cpp85
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
@@ -29,13 +29,13 @@
29#include "graph.h" 29#include "graph.h"
30#include "graphinfo.h" 30#include "graphinfo.h"
31 31
32#include <qcolor.h>
33#include <qfontmetrics.h>
32#include <qpainter.h> 34#include <qpainter.h>
33 35
34Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags ) 36Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags )
35 : QWidget( parent, name, flags ) 37 : QWidget( parent, name, flags )
36{ 38{
37 setBackgroundMode( QWidget::PaletteBase );
38
39 data = d; 39 data = d;
40 40
41 graph.setOptimization( QPixmap::BestOptim ); 41 graph.setOptimization( QPixmap::BestOptim );
@@ -82,31 +82,98 @@ void Graph::initGraph()
82 { 82 {
83 case GraphInfo::BarChart : 83 case GraphInfo::BarChart :
84 { 84 {
85 drawBarChart(); 85 drawBarChart( width(), height(), data->maxValue() );
86 } 86 }
87 break; 87 break;
88 case GraphInfo::LineChart : 88 case GraphInfo::LineChart :
89 { 89 {
90 drawLineChart(); 90 //drawLineChart( p, s, min, max );
91 } 91 }
92 break; 92 break;
93 case GraphInfo::PieChart : 93 case GraphInfo::PieChart :
94 { 94 {
95 drawPieChart(); 95 drawPieChart( width(), height(), data->totalValue() );
96 } 96 }
97 }; 97 };
98} 98}
99 99
100void Graph::drawBarChart() 100void Graph::drawBarChart( int width, int height, float max )
101{ 101{
102 //Find max value in GraphInfo->dataPoints() - make function in GraphInfo!!! 102 QPainter p( &graph );
103
104 // Try to set the font size smaller for text
105 QFont f = font();
106 f.setPointSize( 8 );
107 p.setFont( f );
108
109 int x = 0;
110 int i = 0;
111 int n = data->numberDataPoints();
112 QFontMetrics fm=fontMetrics();
113 int fh = fm.height();
114 int fw;
115
116 QColor c( 0, 0, 255);
117 p.setBrush( c );
118
119 for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() )
120 {
121 int bw = ( width - width / 4 - x ) / ( n - i );
122 int bh = int( ( height - height / 4 - 1 ) * dp->value() / max );
123 p.drawRect( width / 8 + x, height - height / 8 - 1 - bh, bw, bh );
124 fw = fm.width( dp->label() );
125 p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8, fw,
126 fh + height / 8, AlignTop | AlignHCenter, dp->label() );
127 // WordBreak | AlignTop | AlignHCenter, dp->label() );
128 i++;
129 x += bw;
130 }
103} 131}
104 132
105void Graph::drawLineChart() 133void Graph::drawLineChart( int width, int height, float max )
106{ 134{
107} 135}
108 136
109void Graph::drawPieChart() 137void Graph::drawPieChart( int width, int height, float sum )
110{ 138{
139 QPainter p( &graph );
140
141 // Try to set the font size smaller for text
142 QFont f = font();
143 f.setPointSize( 8 );
144 p.setFont( f );
145
146 int n = data->numberDataPoints();
147
148 int apos = -90 * 16;
149
150 int xd = width - width / 5;
151 int yd = height - height / 5;
152
153 int i = 0;
154
155 QColor c;
156
157 for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() )
158 {
159 c.setHsv( ( i *255) / n, 255, 255 );
160 p.setBrush( c );
161
162 int a = int( ( dp->value() * 360.0 ) / sum * 16.0 + 0.5 );
163 p.drawPie( width/10, height/10, xd, yd, -apos, -a );
164 apos += a;
165 i++;
166 }
167
168 double apos2 = -90 * 3.14159 / 180;
169 for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() )
170 {
171 double a = dp->value() *360 / sum * 3.14159 / 180;
172 int x = int( cos( apos2 + a/2 ) * width * 5/16 + width/2 + 0.5 );
173 int y = int( sin( apos2 + a/2 ) * height * 5/16 + height/2 + 0.5 );
174 p.drawText( x - width/8, y - height/8, width/4, height/4, WordBreak | AlignCenter,
175 dp->label() );
176 apos2 += a;
177 }
111} 178}
112 179