-rw-r--r-- | noncore/apps/checkbook/graph.cpp | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/noncore/apps/checkbook/graph.cpp b/noncore/apps/checkbook/graph.cpp index 72da738..279285a 100644 --- a/noncore/apps/checkbook/graph.cpp +++ b/noncore/apps/checkbook/graph.cpp | |||
@@ -4,170 +4,172 @@ | |||
4 | .=l. Copyright (c) 2002 Dan Williams <drw@handhelds.org> | 4 | .=l. Copyright (c) 2002 Dan Williams <drw@handhelds.org> |
5 | .>+-= | 5 | .>+-= |
6 | _;:, .> :=|. This file is free software; you can | 6 | _;:, .> :=|. This file is free software; you can |
7 | .> <`_, > . <= redistribute it and/or modify it under | 7 | .> <`_, > . <= redistribute it and/or modify it under |
8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public | 8 | :`=1 )Y*s>-.-- : the terms of the GNU General Public |
9 | .="- .-=="i, .._ License as published by the Free Software | 9 | .="- .-=="i, .._ License as published by the Free Software |
10 | - . .-<_> .<> Foundation; either version 2 of the License, | 10 | - . .-<_> .<> Foundation; either version 2 of the License, |
11 | ._= =} : or (at your option) any later version. | 11 | ._= =} : or (at your option) any later version. |
12 | .%`+i> _;_. | 12 | .%`+i> _;_. |
13 | .i_,=:_. -<s. This file is distributed in the hope that | 13 | .i_,=:_. -<s. This file is distributed in the hope that |
14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 14 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
15 | : .. .:, . . . without even the implied warranty of | 15 | : .. .:, . . . without even the implied warranty of |
16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 16 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General | 17 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU General |
18 | ..}^=.= = ; Public License for more details. | 18 | ..}^=.= = ; Public License for more details. |
19 | ++= -. .` .: | 19 | ++= -. .` .: |
20 | : = ...= . :.=- You should have received a copy of the GNU | 20 | : = ...= . :.=- You should have received a copy of the GNU |
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 | #include "graph.h" | 29 | #include "graph.h" |
30 | #include "graphinfo.h" | 30 | #include "graphinfo.h" |
31 | 31 | ||
32 | #include <qpainter.h> | 32 | #include <qpainter.h> |
33 | 33 | ||
34 | #include <math.h> | 34 | #include <math.h> |
35 | 35 | ||
36 | Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags ) | 36 | Graph::Graph( QWidget *parent, GraphInfo *d, const QString &name, int flags ) |
37 | : QWidget( parent, name, flags ) | 37 | : QWidget( parent, name, flags ) |
38 | { | 38 | { |
39 | data = d; | 39 | data = d; |
40 | 40 | ||
41 | graph.setOptimization( QPixmap::BestOptim ); | 41 | graph.setOptimization( QPixmap::BestOptim ); |
42 | } | 42 | } |
43 | 43 | ||
44 | void Graph::setGraphInfo( GraphInfo *d ) | 44 | void Graph::setGraphInfo( GraphInfo *d ) |
45 | { | 45 | { |
46 | data = d; | 46 | data = d; |
47 | } | 47 | } |
48 | 48 | ||
49 | void Graph::drawGraph( bool regen ) | 49 | void Graph::drawGraph( bool regen ) |
50 | { | 50 | { |
51 | if ( regen ) | 51 | if ( regen ) |
52 | { | 52 | { |
53 | initGraph(); | 53 | initGraph(); |
54 | } | 54 | } |
55 | QPainter p( this ); | 55 | QPainter p( this ); |
56 | p.drawPixmap( 0, 0, graph ); | 56 | p.drawPixmap( 0, 0, graph ); |
57 | } | 57 | } |
58 | 58 | ||
59 | void Graph::paintEvent( QPaintEvent * ) | 59 | void Graph::paintEvent( QPaintEvent * ) |
60 | { | 60 | { |
61 | drawGraph( FALSE ); | 61 | drawGraph( FALSE ); |
62 | } | 62 | } |
63 | 63 | ||
64 | void Graph::resizeEvent( QResizeEvent * ) | 64 | void Graph::resizeEvent( QResizeEvent * ) |
65 | { | 65 | { |
66 | drawGraph( TRUE ); | 66 | drawGraph( TRUE ); |
67 | } | 67 | } |
68 | 68 | ||
69 | void Graph::initGraph() | 69 | void 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(), data->minValue() ); | 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 | ||
95 | void Graph::drawBarChart( int width, int height, float max, float min ) | 95 | void 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 | // it would be nice to get _pCfg->getUseSmallFont here | ||
100 | QFont f = font(); | 101 | QFont f = font(); |
101 | f.setPointSize( 8 ); | 102 | f.setPointSize( f.pointSize()-1 ); |
102 | p.setFont( f ); | 103 | p.setFont( f ); |
103 | 104 | ||
104 | int x = 0; | 105 | int x = 0; |
105 | int i = 0; | 106 | int i = 0; |
106 | int n = data->numberDataPoints(); | 107 | int n = data->numberDataPoints(); |
107 | QFontMetrics fm=fontMetrics(); | 108 | QFontMetrics fm=fontMetrics(); |
108 | int fh = fm.height(); | 109 | int fh = fm.height(); |
109 | int fw; | 110 | int fw; |
110 | 111 | ||
111 | QColor c( 0, 0, 255); | 112 | QColor c( 0, 0, 255); |
112 | p.setBrush( c ); | 113 | p.setBrush( c ); |
113 | 114 | ||
114 | if ( min > 0 ) | 115 | if ( min > 0 ) |
115 | min = 0.0; | 116 | min = 0.0; |
116 | 117 | ||
117 | int bw = ( width - width / 4 ) / n; | 118 | int bw = ( width - width / 4 ) / n; |
118 | int hoffset = int( ( height - height / 4 - 1 ) * ( min * -1 ) / ( max - min ) ); | 119 | int hoffset = int( ( height - height / 4 - 1 ) * ( min * -1 ) / ( max - min ) ); |
119 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) | 120 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) |
120 | { | 121 | { |
121 | int bh = int( ( height - height / 4 - 1 ) * dp->value() / ( max - min ) ); | 122 | int bh = int( ( height - height / 4 - 1 ) * dp->value() / ( max - min ) ); |
122 | p.drawRect( width / 8 + x, height - height / 8 - 1 - hoffset - bh, bw, bh ); | 123 | p.drawRect( width / 8 + x, height - height / 8 - 1 - hoffset - bh, bw, bh ); |
123 | fw = fm.width( dp->label() ); | 124 | fw = fm.width( dp->label() ); |
124 | p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8 - hoffset, fw, | 125 | p.drawText( width / 8 + x - fw / 2 + bw / 2, height - height / 8 - hoffset, fw, |
125 | fh + height / 8, AlignTop | AlignHCenter, dp->label() ); | 126 | fh + height / 8, AlignTop | AlignHCenter, dp->label() ); |
126 | i++; | 127 | i++; |
127 | x += bw; | 128 | x += bw; |
128 | } | 129 | } |
129 | } | 130 | } |
130 | 131 | ||
131 | void Graph::drawPieChart( int width, int height, float sum ) | 132 | void Graph::drawPieChart( int width, int height, float sum ) |
132 | { | 133 | { |
133 | QPainter p( &graph ); | 134 | QPainter p( &graph ); |
134 | 135 | ||
135 | // Try to set the font size smaller for text | 136 | // Try to set the font size smaller for text |
137 | // it would be nice to get _pCfg->getUseSmallFont here | ||
136 | QFont f = font(); | 138 | QFont f = font(); |
137 | f.setPointSize( 8 ); | 139 | f.setPointSize( f.pointSize()-1 ); |
138 | p.setFont( f ); | 140 | p.setFont( f ); |
139 | 141 | ||
140 | int n = data->numberDataPoints(); | 142 | int n = data->numberDataPoints(); |
141 | 143 | ||
142 | int apos = -90 * 16; | 144 | int apos = -90 * 16; |
143 | 145 | ||
144 | int xd = width - width / 5; | 146 | int xd = width - width / 5; |
145 | int yd = height - height / 5; | 147 | int yd = height - height / 5; |
146 | 148 | ||
147 | int i = 0; | 149 | int i = 0; |
148 | 150 | ||
149 | QColor c; | 151 | QColor c; |
150 | 152 | ||
151 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) | 153 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) |
152 | { | 154 | { |
153 | c.setHsv( ( i *255) / n, 255, 255 ); | 155 | c.setHsv( ( i *255) / n, 255, 255 ); |
154 | p.setBrush( c ); | 156 | p.setBrush( c ); |
155 | 157 | ||
156 | int a = int( ( dp->value() * 360.0 ) / sum * 16.0 + 0.5 ); | 158 | int a = int( ( dp->value() * 360.0 ) / sum * 16.0 + 0.5 ); |
157 | p.drawPie( width/10, height/10, xd, yd, -apos, -a ); | 159 | p.drawPie( width/10, height/10, xd, yd, -apos, -a ); |
158 | apos += a; | 160 | apos += a; |
159 | i++; | 161 | i++; |
160 | } | 162 | } |
161 | 163 | ||
162 | double apos2 = -90 * 3.14159 / 180; | 164 | double apos2 = -90 * 3.14159 / 180; |
163 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) | 165 | for (DataPointInfo *dp = data->firstDataPoint(); dp; dp = data->nextDataPoint() ) |
164 | { | 166 | { |
165 | double a = dp->value() *360 / sum * 3.14159 / 180; | 167 | double a = dp->value() *360 / sum * 3.14159 / 180; |
166 | int x = int( cos( apos2 + a/2 ) * width * 5/16 + width/2 + 0.5 ); | 168 | int x = int( cos( apos2 + a/2 ) * width * 5/16 + width/2 + 0.5 ); |
167 | int y = int( sin( apos2 + a/2 ) * height * 5/16 + height/2 + 0.5 ); | 169 | int y = int( sin( apos2 + a/2 ) * height * 5/16 + height/2 + 0.5 ); |
168 | p.drawText( x - width/8, y - height/8, width/4, height/4, WordBreak | AlignCenter, | 170 | p.drawText( x - width/8, y - height/8, width/4, height/4, WordBreak | AlignCenter, |
169 | dp->label() ); | 171 | dp->label() ); |
170 | apos2 += a; | 172 | apos2 += a; |
171 | } | 173 | } |
172 | } | 174 | } |
173 | 175 | ||