| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
|---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial2.doc:10 -->
|
|---|
| 3 | <html>
|
|---|
| 4 | <head>
|
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|---|
| 6 | <title>chart/chartform_canvas.cpp Example File</title>
|
|---|
| 7 | <style type="text/css"><!--
|
|---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
|---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
|---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
|---|
| 11 | body { background: #ffffff; color: black; }
|
|---|
| 12 | --></style>
|
|---|
| 13 | </head>
|
|---|
| 14 | <body>
|
|---|
| 15 |
|
|---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
|---|
| 17 | <tr bgcolor="#E5E5E5">
|
|---|
| 18 | <td valign=center>
|
|---|
| 19 | <a href="index.html">
|
|---|
| 20 | <font color="#004faf">Home</font></a>
|
|---|
| 21 | | <a href="classes.html">
|
|---|
| 22 | <font color="#004faf">All Classes</font></a>
|
|---|
| 23 | | <a href="mainclasses.html">
|
|---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
|---|
| 25 | | <a href="annotated.html">
|
|---|
| 26 | <font color="#004faf">Annotated</font></a>
|
|---|
| 27 | | <a href="groups.html">
|
|---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
|---|
| 29 | | <a href="functions.html">
|
|---|
| 30 | <font color="#004faf">Functions</font></a>
|
|---|
| 31 | </td>
|
|---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>chart/chartform_canvas.cpp Example File</h1>
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | <pre>#include "canvastext.h"
|
|---|
| 36 | #include "chartform.h"
|
|---|
| 37 |
|
|---|
| 38 | #include <<a href="qbrush-h.html">qbrush.h</a>>
|
|---|
| 39 | #include <<a href="qcanvas-h.html">qcanvas.h</a>>
|
|---|
| 40 |
|
|---|
| 41 | #include <math.h> // sin, cos
|
|---|
| 42 |
|
|---|
| 43 | #ifndef M_PI
|
|---|
| 44 | #define M_PI 3.1415
|
|---|
| 45 | #endif
|
|---|
| 46 |
|
|---|
| 47 | void <a name="f167"></a>ChartForm::drawElements()
|
|---|
| 48 | {
|
|---|
| 49 | <a href="qcanvasitemlist.html">QCanvasItemList</a> list = m_canvas->allItems();
|
|---|
| 50 | for ( QCanvasItemList::iterator it = list.<a href="qvaluelist.html#begin">begin</a>(); it != list.<a href="qvaluelist.html#end">end</a>(); ++it )
|
|---|
| 51 | delete *it;
|
|---|
| 52 |
|
|---|
| 53 | // 360 * 16 for pies; Qt works with 16ths of degrees
|
|---|
| 54 | int scaleFactor = m_chartType == PIE ? 5760 :
|
|---|
| 55 | m_chartType == VERTICAL_BAR ? m_canvas->height() :
|
|---|
| 56 | m_canvas->width();
|
|---|
| 57 | double biggest = 0.0;
|
|---|
| 58 | int count = 0;
|
|---|
| 59 | double total = 0.0;
|
|---|
| 60 | static double scales[MAX_ELEMENTS];
|
|---|
| 61 |
|
|---|
| 62 | for ( int i = 0; i < MAX_ELEMENTS; ++i ) {
|
|---|
| 63 | if ( m_elements[i].isValid() ) {
|
|---|
| 64 | double value = m_elements[i].value();
|
|---|
| 65 | count++;
|
|---|
| 66 | total += value;
|
|---|
| 67 | if ( value > biggest )
|
|---|
| 68 | biggest = value;
|
|---|
| 69 | scales[i] = m_elements[i].value() * scaleFactor;
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | if ( count ) {
|
|---|
| 74 | // 2nd loop because of total and biggest
|
|---|
| 75 | for ( int i = 0; i < MAX_ELEMENTS; ++i )
|
|---|
| 76 | if ( m_elements[i].isValid() )
|
|---|
| 77 | if ( m_chartType == PIE )
|
|---|
| 78 | scales[i] = (m_elements[i].value() * scaleFactor) / total;
|
|---|
| 79 | else
|
|---|
| 80 | scales[i] = (m_elements[i].value() * scaleFactor) / biggest;
|
|---|
| 81 |
|
|---|
| 82 | switch ( m_chartType ) {
|
|---|
| 83 | case PIE:
|
|---|
| 84 | drawPieChart( scales, total, count );
|
|---|
| 85 | break;
|
|---|
| 86 | case VERTICAL_BAR:
|
|---|
| 87 | drawVerticalBarChart( scales, total, count );
|
|---|
| 88 | break;
|
|---|
| 89 | case HORIZONTAL_BAR:
|
|---|
| 90 | drawHorizontalBarChart( scales, total, count );
|
|---|
| 91 | break;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | m_canvas->update();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | void <a name="f168"></a>ChartForm::drawPieChart( const double scales[], double total, int )
|
|---|
| 100 | {
|
|---|
| 101 | double width = m_canvas->width();
|
|---|
| 102 | double height = m_canvas->height();
|
|---|
| 103 | int size = int(width > height ? height : width);
|
|---|
| 104 | int x = int(width / 2);
|
|---|
| 105 | int y = int(height / 2);
|
|---|
| 106 | int angle = 0;
|
|---|
| 107 |
|
|---|
| 108 | for ( int i = 0; i < MAX_ELEMENTS; ++i ) {
|
|---|
| 109 | if ( m_elements[i].isValid() ) {
|
|---|
| 110 | int extent = int(scales[i]);
|
|---|
| 111 | <a href="qcanvasellipse.html">QCanvasEllipse</a> *arc = new <a href="qcanvasellipse.html">QCanvasEllipse</a>(
|
|---|
| 112 | size, size, angle, extent, m_canvas );
|
|---|
| 113 | arc-><a href="qcanvasitem.html#setX">setX</a>( x );
|
|---|
| 114 | arc-><a href="qcanvasitem.html#setY">setY</a>( y );
|
|---|
| 115 | arc-><a href="qcanvasitem.html#setZ">setZ</a>( 0 );
|
|---|
| 116 | arc-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QBrush( m_elements[i].valueColor(),
|
|---|
| 117 | BrushStyle(m_elements[i].valuePattern()) ) );
|
|---|
| 118 | arc-><a href="qcanvasitem.html#show">show</a>();
|
|---|
| 119 | angle += extent;
|
|---|
| 120 | <a href="qstring.html">QString</a> label = m_elements[i].label();
|
|---|
| 121 | if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() || m_addValues != NO ) {
|
|---|
| 122 | label = valueLabel( label, m_elements[i].value(), total );
|
|---|
| 123 | CanvasText *text = new CanvasText( i, label, m_font, m_canvas );
|
|---|
| 124 | double proX = m_elements[i].proX( PIE );
|
|---|
| 125 | double proY = m_elements[i].proY( PIE );
|
|---|
| 126 | if ( proX < 0 || proY < 0 ) {
|
|---|
| 127 | // Find the centre of the pie segment
|
|---|
| 128 | <a href="qrect.html">QRect</a> rect = arc-><a href="qcanvaspolygonalitem.html#boundingRect">boundingRect</a>();
|
|---|
| 129 | proX = ( rect.<a href="qcanvasrectangle.html#width">width</a>() / 2 ) + rect.<a href="qcanvasitem.html#x">x</a>();
|
|---|
| 130 | proY = ( rect.<a href="qcanvasrectangle.html#height">height</a>() / 2 ) + rect.<a href="qcanvasitem.html#y">y</a>();
|
|---|
| 131 | // Centre text over the centre of the pie segment
|
|---|
| 132 | rect = text-><a href="qcanvastext.html#boundingRect">boundingRect</a>();
|
|---|
| 133 | proX -= ( rect.<a href="qcanvasrectangle.html#width">width</a>() / 2 );
|
|---|
| 134 | proY -= ( rect.<a href="qcanvasrectangle.html#height">height</a>() / 2 );
|
|---|
| 135 | // Make proportional
|
|---|
| 136 | proX /= width;
|
|---|
| 137 | proY /= height;
|
|---|
| 138 | }
|
|---|
| 139 | text-><a href="qcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
|
|---|
| 140 | text-><a href="qcanvasitem.html#setX">setX</a>( proX * width );
|
|---|
| 141 | text-><a href="qcanvasitem.html#setY">setY</a>( proY * height );
|
|---|
| 142 | text-><a href="qcanvasitem.html#setZ">setZ</a>( 1 );
|
|---|
| 143 | text-><a href="qcanvasitem.html#show">show</a>();
|
|---|
| 144 | m_elements[i].setProX( PIE, proX );
|
|---|
| 145 | m_elements[i].setProY( PIE, proY );
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 | void <a name="f169"></a>ChartForm::drawVerticalBarChart(
|
|---|
| 153 | const double scales[], double total, int count )
|
|---|
| 154 | {
|
|---|
| 155 | double width = m_canvas->width();
|
|---|
| 156 | double height = m_canvas->height();
|
|---|
| 157 | int prowidth = int(width / count);
|
|---|
| 158 | int x = 0;
|
|---|
| 159 | <a href="qpen.html">QPen</a> pen;
|
|---|
| 160 | pen.<a href="qpen.html#setStyle">setStyle</a>( NoPen );
|
|---|
| 161 |
|
|---|
| 162 | for ( int i = 0; i < MAX_ELEMENTS; ++i ) {
|
|---|
| 163 | if ( m_elements[i].isValid() ) {
|
|---|
| 164 | int extent = int(scales[i]);
|
|---|
| 165 | int y = int(height - extent);
|
|---|
| 166 | <a href="qcanvasrectangle.html">QCanvasRectangle</a> *rect = new <a href="qcanvasrectangle.html">QCanvasRectangle</a>(
|
|---|
| 167 | x, y, prowidth, extent, m_canvas );
|
|---|
| 168 | rect-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QBrush( m_elements[i].valueColor(),
|
|---|
| 169 | BrushStyle(m_elements[i].valuePattern()) ) );
|
|---|
| 170 | rect-><a href="qcanvaspolygonalitem.html#setPen">setPen</a>( pen );
|
|---|
| 171 | rect-><a href="qcanvasitem.html#setZ">setZ</a>( 0 );
|
|---|
| 172 | rect-><a href="qcanvasitem.html#show">show</a>();
|
|---|
| 173 | <a href="qstring.html">QString</a> label = m_elements[i].label();
|
|---|
| 174 | if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() || m_addValues != NO ) {
|
|---|
| 175 | double proX = m_elements[i].proX( VERTICAL_BAR );
|
|---|
| 176 | double proY = m_elements[i].proY( VERTICAL_BAR );
|
|---|
| 177 | if ( proX < 0 || proY < 0 ) {
|
|---|
| 178 | proX = x / width;
|
|---|
| 179 | proY = y / height;
|
|---|
| 180 | }
|
|---|
| 181 | label = valueLabel( label, m_elements[i].value(), total );
|
|---|
| 182 | CanvasText *text = new CanvasText( i, label, m_font, m_canvas );
|
|---|
| 183 | text-><a href="qcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
|
|---|
| 184 | text-><a href="qcanvasitem.html#setX">setX</a>( proX * width );
|
|---|
| 185 | text-><a href="qcanvasitem.html#setY">setY</a>( proY * height );
|
|---|
| 186 | text-><a href="qcanvasitem.html#setZ">setZ</a>( 1 );
|
|---|
| 187 | text-><a href="qcanvasitem.html#show">show</a>();
|
|---|
| 188 | m_elements[i].setProX( VERTICAL_BAR, proX );
|
|---|
| 189 | m_elements[i].setProY( VERTICAL_BAR, proY );
|
|---|
| 190 | }
|
|---|
| 191 | x += prowidth;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | void <a name="f170"></a>ChartForm::drawHorizontalBarChart(
|
|---|
| 198 | const double scales[], double total, int count )
|
|---|
| 199 | {
|
|---|
| 200 | double width = m_canvas->width();
|
|---|
| 201 | double height = m_canvas->height();
|
|---|
| 202 | int proheight = int(height / count);
|
|---|
| 203 | int y = 0;
|
|---|
| 204 | <a href="qpen.html">QPen</a> pen;
|
|---|
| 205 | pen.<a href="qpen.html#setStyle">setStyle</a>( NoPen );
|
|---|
| 206 |
|
|---|
| 207 | for ( int i = 0; i < MAX_ELEMENTS; ++i ) {
|
|---|
| 208 | if ( m_elements[i].isValid() ) {
|
|---|
| 209 | int extent = int(scales[i]);
|
|---|
| 210 | <a href="qcanvasrectangle.html">QCanvasRectangle</a> *rect = new <a href="qcanvasrectangle.html">QCanvasRectangle</a>(
|
|---|
| 211 | 0, y, extent, proheight, m_canvas );
|
|---|
| 212 | rect-><a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QBrush( m_elements[i].valueColor(),
|
|---|
| 213 | BrushStyle(m_elements[i].valuePattern()) ) );
|
|---|
| 214 | rect-><a href="qcanvaspolygonalitem.html#setPen">setPen</a>( pen );
|
|---|
| 215 | rect-><a href="qcanvasitem.html#setZ">setZ</a>( 0 );
|
|---|
| 216 | rect-><a href="qcanvasitem.html#show">show</a>();
|
|---|
| 217 | <a href="qstring.html">QString</a> label = m_elements[i].label();
|
|---|
| 218 | if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() || m_addValues != NO ) {
|
|---|
| 219 | double proX = m_elements[i].proX( HORIZONTAL_BAR );
|
|---|
| 220 | double proY = m_elements[i].proY( HORIZONTAL_BAR );
|
|---|
| 221 | if ( proX < 0 || proY < 0 ) {
|
|---|
| 222 | proX = 0;
|
|---|
| 223 | proY = y / height;
|
|---|
| 224 | }
|
|---|
| 225 | label = valueLabel( label, m_elements[i].value(), total );
|
|---|
| 226 | CanvasText *text = new CanvasText( i, label, m_font, m_canvas );
|
|---|
| 227 | text-><a href="qcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
|
|---|
| 228 | text-><a href="qcanvasitem.html#setX">setX</a>( proX * width );
|
|---|
| 229 | text-><a href="qcanvasitem.html#setY">setY</a>( proY * height );
|
|---|
| 230 | text-><a href="qcanvasitem.html#setZ">setZ</a>( 1 );
|
|---|
| 231 | text-><a href="qcanvasitem.html#show">show</a>();
|
|---|
| 232 | m_elements[i].setProX( HORIZONTAL_BAR, proX );
|
|---|
| 233 | m_elements[i].setProY( HORIZONTAL_BAR, proY );
|
|---|
| 234 | }
|
|---|
| 235 | y += proheight;
|
|---|
| 236 | }
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | QString <a name="f171"></a>ChartForm::valueLabel(
|
|---|
| 242 | const <a href="qstring.html">QString</a>& label, double value, double total )
|
|---|
| 243 | {
|
|---|
| 244 | if ( m_addValues == NO )
|
|---|
| 245 | return label;
|
|---|
| 246 |
|
|---|
| 247 | <a href="qstring.html">QString</a> newLabel = label;
|
|---|
| 248 | if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() )
|
|---|
| 249 | if ( m_chartType == VERTICAL_BAR )
|
|---|
| 250 | newLabel += '\n';
|
|---|
| 251 | else
|
|---|
| 252 | newLabel += ' ';
|
|---|
| 253 | if ( m_addValues == YES )
|
|---|
| 254 | newLabel += QString::<a href="qstring.html#number">number</a>( value, 'f', m_decimalPlaces );
|
|---|
| 255 | else if ( m_addValues == AS_PERCENTAGE )
|
|---|
| 256 | newLabel += QString::<a href="qstring.html#number">number</a>( (value / total) * 100, 'f', m_decimalPlaces )
|
|---|
| 257 | + '%';
|
|---|
| 258 | return newLabel;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | </pre><!-- eof -->
|
|---|
| 262 | <p><address><hr><div align=center>
|
|---|
| 263 | <table width=100% cellspacing=0 border=0><tr>
|
|---|
| 264 | <td>Copyright © 2007
|
|---|
| 265 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
|---|
| 266 | <td align=right><div align=right>Qt 3.3.8</div>
|
|---|
| 267 | </table></div></address></body>
|
|---|
| 268 | </html>
|
|---|