summaryrefslogtreecommitdiff
path: root/noncore/apps/checkbook
Unidiff
Diffstat (limited to 'noncore/apps/checkbook') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/checkbook/graph.cpp17
-rw-r--r--noncore/apps/checkbook/graph.h2
-rw-r--r--noncore/apps/checkbook/graphinfo.cpp13
-rw-r--r--noncore/apps/checkbook/graphinfo.h3
4 files changed, 26 insertions, 9 deletions
diff --git a/noncore/apps/checkbook/graph.cpp b/noncore/apps/checkbook/graph.cpp
index 389972e..72da738 100644
--- a/noncore/apps/checkbook/graph.cpp
+++ b/noncore/apps/checkbook/graph.cpp
@@ -61,86 +61,89 @@ void Graph::paintEvent( QPaintEvent * )
61 drawGraph( FALSE ); 61 drawGraph( FALSE );
62} 62}
63 63
64void Graph::resizeEvent( QResizeEvent * ) 64void Graph::resizeEvent( QResizeEvent * )
65{ 65{
66 drawGraph( TRUE ); 66 drawGraph( TRUE );
67} 67}
68 68
69void Graph::initGraph() 69void Graph::initGraph()
70{ 70{
71 graph.resize( width(), height() ); 71 graph.resize( width(), height() );
72 graph.fill( QColor( 255, 255, 255 ) ); 72 graph.fill( QColor( 255, 255, 255 ) );
73 73
74 if ( !data ) 74 if ( !data )
75 { 75 {
76 return; 76 return;
77 } 77 }
78 78
79 // Any common stuff here (titles, ???) 79 // Any common stuff here (titles, ???)
80 80
81 switch ( data->graphType() ) 81 switch ( data->graphType() )
82 { 82 {
83 case GraphInfo::BarChart : 83 case GraphInfo::BarChart :
84 { 84 {
85 drawBarChart( width(), height(), data->maxValue() ); 85 drawBarChart( width(), height(), data->maxValue(), data->minValue() );
86 } 86 }
87 break; 87 break;
88 case GraphInfo::PieChart : 88 case GraphInfo::PieChart :
89 { 89 {
90 drawPieChart( width(), height(), data->totalValue() ); 90 drawPieChart( width(), height(), data->totalValue() );
91 } 91 }
92 }; 92 };
93} 93}
94 94
95void Graph::drawBarChart( int width, int height, float max ) 95void Graph::drawBarChart( int width, int height, float max, float min )
96{ 96{
97 QPainter p( &graph ); 97 QPainter p( &graph );
98 98
99 // Try to set the font size smaller for text 99 // Try to set the font size smaller for text
100 QFont f = font(); 100 QFont f = font();
101 f.setPointSize( 8 ); 101 f.setPointSize( 8 );
102 p.setFont( f ); 102 p.setFont( f );
103 103
104 int x = 0; 104 int x = 0;
105 int i = 0; 105 int i = 0;
106 int n = data->numberDataPoints(); 106 int n = data->numberDataPoints();
107 QFontMetrics fm=fontMetrics(); 107 QFontMetrics fm=fontMetrics();
108 int fh = fm.height(); 108 int fh = fm.height();
109 int fw; 109 int fw;
110 110
111 QColor c( 0, 0, 255); 111 QColor c( 0, 0, 255);
112 p.setBrush( c ); 112 p.setBrush( c );
113 113
114 if ( min > 0 )
115 min = 0.0;
116
117 int bw = ( width - width / 4 ) / n;
118 int hoffset = int( ( height - height / 4 - 1 ) * ( min * -1 ) / ( max - min ) );
114 for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) 119 for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() )
115 { 120 {
116 int bw = ( width - width / 4 - x ) / ( n - i ); 121 int bh = int( ( height - height / 4 - 1 ) * dp->value() / ( max - min ) );
117 int bh = int( ( height - height / 4 - 1 ) * dp->value() / max ); 122 p.drawRect( width / 8 + x, height - height / 8 - 1 - hoffset - bh, bw, bh );
118 p.drawRect( width / 8 + x, height - height / 8 - 1 - bh, bw, bh );
119 fw = fm.width( dp->label() ); 123 fw = fm.width( dp->label() );
120 p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8, fw, 124 p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8 - hoffset, fw,
121 fh + height / 8, AlignTop | AlignHCenter, dp->label() ); 125 fh + height / 8, AlignTop | AlignHCenter, dp->label() );
122 // WordBreak | AlignTop | AlignHCenter, dp->label() );
123 i++; 126 i++;
124 x += bw; 127 x += bw;
125 } 128 }
126} 129}
127 130
128void Graph::drawPieChart( int width, int height, float sum ) 131void Graph::drawPieChart( int width, int height, float sum )
129{ 132{
130 QPainter p( &graph ); 133 QPainter p( &graph );
131 134
132 // Try to set the font size smaller for text 135 // Try to set the font size smaller for text
133 QFont f = font(); 136 QFont f = font();
134 f.setPointSize( 8 ); 137 f.setPointSize( 8 );
135 p.setFont( f ); 138 p.setFont( f );
136 139
137 int n = data->numberDataPoints(); 140 int n = data->numberDataPoints();
138 141
139 int apos = -90 * 16; 142 int apos = -90 * 16;
140 143
141 int xd = width - width / 5; 144 int xd = width - width / 5;
142 int yd = height - height / 5; 145 int yd = height - height / 5;
143 146
144 int i = 0; 147 int i = 0;
145 148
146 QColor c; 149 QColor c;
diff --git a/noncore/apps/checkbook/graph.h b/noncore/apps/checkbook/graph.h
index 340e910..616cbb6 100644
--- a/noncore/apps/checkbook/graph.h
+++ b/noncore/apps/checkbook/graph.h
@@ -35,29 +35,29 @@
35class GraphInfo; 35class GraphInfo;
36class QPainter; 36class QPainter;
37 37
38class Graph : public QWidget 38class Graph : public QWidget
39{ 39{
40 Q_OBJECT 40 Q_OBJECT
41 41
42 public: 42 public:
43 Graph( QWidget * = 0x0, GraphInfo * = 0x0, const QString & = 0x0, int = 0 ); 43 Graph( QWidget * = 0x0, GraphInfo * = 0x0, const QString & = 0x0, int = 0 );
44 44
45 void setGraphInfo( GraphInfo * ); 45 void setGraphInfo( GraphInfo * );
46 46
47 void drawGraph( bool = FALSE ); 47 void drawGraph( bool = FALSE );
48 48
49 protected: 49 protected:
50 void paintEvent( QPaintEvent * ); 50 void paintEvent( QPaintEvent * );
51 void resizeEvent( QResizeEvent * ); 51 void resizeEvent( QResizeEvent * );
52 52
53 private: 53 private:
54 GraphInfo *data; 54 GraphInfo *data;
55 55
56 QPixmap graph; 56 QPixmap graph;
57 57
58 void initGraph(); 58 void initGraph();
59 void drawBarChart( int, int, float ); 59 void drawBarChart( int, int, float, float );
60 void drawPieChart( int, int, float ); 60 void drawPieChart( int, int, float );
61}; 61};
62 62
63#endif 63#endif
diff --git a/noncore/apps/checkbook/graphinfo.cpp b/noncore/apps/checkbook/graphinfo.cpp
index fec6896..5b72c80 100644
--- a/noncore/apps/checkbook/graphinfo.cpp
+++ b/noncore/apps/checkbook/graphinfo.cpp
@@ -76,48 +76,61 @@ DataPointInfo *GraphInfo::firstDataPoint()
76 76
77DataPointInfo *GraphInfo::nextDataPoint() 77DataPointInfo *GraphInfo::nextDataPoint()
78{ 78{
79 return( d->next() ); 79 return( d->next() );
80} 80}
81 81
82int GraphInfo::numberDataPoints() 82int GraphInfo::numberDataPoints()
83{ 83{
84 return( d->count() ); 84 return( d->count() );
85} 85}
86 86
87float GraphInfo::maxValue() 87float GraphInfo::maxValue()
88{ 88{
89 float max = 0.0; 89 float max = 0.0;
90 for ( DataPointInfo *data = d->first(); data; data = d->next() ) 90 for ( DataPointInfo *data = d->first(); data; data = d->next() )
91 { 91 {
92 if ( data->value() > max ) 92 if ( data->value() > max )
93 { 93 {
94 max = data->value(); 94 max = data->value();
95 } 95 }
96 } 96 }
97 return max; 97 return max;
98} 98}
99 99
100float GraphInfo::minValue()
101{
102 float min = 0.0;
103 for ( DataPointInfo *data = d->first(); data; data = d->next() )
104 {
105 if ( data->value() < min )
106 {
107 min = data->value();
108 }
109 }
110 return min;
111}
112
100float GraphInfo::totalValue() 113float GraphInfo::totalValue()
101{ 114{
102 float sum = 0.0; 115 float sum = 0.0;
103 for ( DataPointInfo *data = d->first(); data; data = d->next() ) 116 for ( DataPointInfo *data = d->first(); data; data = d->next() )
104 { 117 {
105 sum += data->value(); 118 sum += data->value();
106 } 119 }
107 return sum; 120 return sum;
108} 121}
109 122
110void GraphInfo::setGraphTitle( const QString &title ) 123void GraphInfo::setGraphTitle( const QString &title )
111{ 124{
112 gt = title; 125 gt = title;
113} 126}
114 127
115void GraphInfo::setXAxisTitle( const QString &xtitle ) 128void GraphInfo::setXAxisTitle( const QString &xtitle )
116{ 129{
117 xt = xtitle; 130 xt = xtitle;
118} 131}
119 132
120void GraphInfo::setYAxisTitle( const QString &ytitle ) 133void GraphInfo::setYAxisTitle( const QString &ytitle )
121{ 134{
122 yt = ytitle; 135 yt = ytitle;
123} 136}
diff --git a/noncore/apps/checkbook/graphinfo.h b/noncore/apps/checkbook/graphinfo.h
index 41927b4..f7842c6 100644
--- a/noncore/apps/checkbook/graphinfo.h
+++ b/noncore/apps/checkbook/graphinfo.h
@@ -21,68 +21,69 @@
21 -.   .:....=;==+<; General Public License along with this file; 21 -.   .:....=;==+<; General Public License along with this file;
22  -_. . .   )=.  = see the file COPYING. If not, write to the 22  -_. . .   )=.  = see the file COPYING. If not, write to the
23    --        :-=` Free Software Foundation, Inc., 23    --        :-=` Free Software Foundation, Inc.,
24 59 Temple Place - Suite 330, 24 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA. 25 Boston, MA 02111-1307, USA.
26 26
27*/ 27*/
28 28
29#ifndef GRAPHINFO_H 29#ifndef GRAPHINFO_H
30#define GRAPHINFO_H 30#define GRAPHINFO_H
31 31
32#include <qlist.h> 32#include <qlist.h>
33#include <qstringlist.h> 33#include <qstringlist.h>
34 34
35class DataPointInfo 35class DataPointInfo
36{ 36{
37 public: 37 public:
38 DataPointInfo() 38 DataPointInfo()
39 : l( 0x0 ), v( 0.0 ) {} 39 : l( 0x0 ), v( 0.0 ) {}
40 DataPointInfo( const QString &label, float value ) 40 DataPointInfo( const QString &label, float value )
41 : l( label ), v( value ) {} 41 : l( label ), v( value ) {}
42 42
43 const QString &label() { return l; } 43 const QString &label() { return l; }
44 float value() { return v; } 44 float value() { return v; }
45 45
46 void addToValue( float value ) { v += value; } 46 void addToValue( float value ) { v += value; }
47 47
48 private: 48 private:
49 QString l; 49 QString l;
50 float v; 50 float v;
51}; 51};
52 52
53typedef QList<DataPointInfo> DataPointList; 53typedef QList<DataPointInfo> DataPointList;
54 54
55class GraphInfo 55class GraphInfo
56{ 56{
57 public: 57 public:
58 enum GraphType { BarChart, PieChart }; 58 enum GraphType { BarChart, PieChart };
59 59
60 GraphInfo( GraphType = BarChart, DataPointList * = 0x0, 60 GraphInfo( GraphType = BarChart, DataPointList * = 0x0,
61 const QString & = 0x0, const QString & = 0x0, const QString & = 0x0 ); 61 const QString & = 0x0, const QString & = 0x0, const QString & = 0x0 );
62 ~GraphInfo(); 62 ~GraphInfo();
63 63
64 GraphInfo::GraphType graphType(); 64 GraphInfo::GraphType graphType();
65 void setGraphType( GraphType ); 65 void setGraphType( GraphType );
66 66
67 DataPointList *dataPoints(); 67 DataPointList *dataPoints();
68 void setDataPoints( DataPointList * ); 68 void setDataPoints( DataPointList * );
69 DataPointInfo *firstDataPoint(); 69 DataPointInfo *firstDataPoint();
70 DataPointInfo *nextDataPoint(); 70 DataPointInfo *nextDataPoint();
71 int numberDataPoints(); 71 int numberDataPoints();
72 72
73 float maxValue(); 73 float maxValue();
74 float minValue();
74 float totalValue(); 75 float totalValue();
75 76
76 void setGraphTitle( const QString & ); 77 void setGraphTitle( const QString & );
77 void setXAxisTitle( const QString & ); 78 void setXAxisTitle( const QString & );
78 void setYAxisTitle( const QString & ); 79 void setYAxisTitle( const QString & );
79 80
80 private: 81 private:
81 GraphType t; 82 GraphType t;
82 DataPointList *d; 83 DataPointList *d;
83 QString gt; 84 QString gt;
84 QString xt; 85 QString xt;
85 QString yt; 86 QString yt;
86}; 87};
87 88
88#endif 89#endif