summaryrefslogtreecommitdiffstats
path: root/doc/html/chart-chartform_canvas-cpp.html
blob: daf112a25868cb900425c1e243f4a39b665186f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial2.doc:10 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>chart/chartform_canvas.cpp Example File</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>

 
<pre>#include "canvastext.h"
#include "chartform.h"

#include &lt;<a href="qbrush-h.html">qbrush.h</a>&gt;
#include &lt;<a href="qcanvas-h.html">qcanvas.h</a>&gt;

#include &lt;math.h&gt; // sin, cos

#ifndef M_PI
#define M_PI 3.1415
#endif

void <a name="f167"></a>ChartForm::drawElements()
{
    <a href="qcanvasitemlist.html">QCanvasItemList</a> list = m_canvas-&gt;allItems();
    for ( QCanvasItemList::iterator it = list.<a href="qvaluelist.html#begin">begin</a>(); it != list.<a href="qvaluelist.html#end">end</a>(); ++it )
        delete *it;

        // 360 * 16 for pies; Qt works with 16ths of degrees
    int scaleFactor = m_chartType == PIE ? 5760 :
                        m_chartType == VERTICAL_BAR ? m_canvas-&gt;height() :
                            m_canvas-&gt;width();
    double biggest = 0.0;
    int count = 0;
    double total = 0.0;
    static double scales[MAX_ELEMENTS];

    for ( int i = 0; i &lt; MAX_ELEMENTS; ++i ) {
        if ( m_elements[i].isValid() ) {
            double value = m_elements[i].value();
            count++;
            total += value;
            if ( value &gt; biggest )
                biggest = value;
            scales[i] = m_elements[i].value() * scaleFactor;
        }
    }

    if ( count ) {
            // 2nd loop because of total and biggest
        for ( int i = 0; i &lt; MAX_ELEMENTS; ++i )
            if ( m_elements[i].isValid() )
                if ( m_chartType == PIE )
                    scales[i] = (m_elements[i].value() * scaleFactor) / total;
                else
                    scales[i] = (m_elements[i].value() * scaleFactor) / biggest;

        switch ( m_chartType ) {
            case PIE:
                drawPieChart( scales, total, count );
                break;
            case VERTICAL_BAR:
                drawVerticalBarChart( scales, total, count );
                break;
            case HORIZONTAL_BAR:
                drawHorizontalBarChart( scales, total, count );
                break;
        }
    }

    m_canvas-&gt;update();
}


void <a name="f168"></a>ChartForm::drawPieChart( const double scales[], double total, int )
{
    double width = m_canvas-&gt;width();
    double height = m_canvas-&gt;height();
    int size = int(width &gt; height ? height : width);
    int x = int(width / 2);
    int y = int(height / 2);
    int angle = 0;

    for ( int i = 0; i &lt; MAX_ELEMENTS; ++i ) {
        if ( m_elements[i].isValid() ) {
            int extent = int(scales[i]);
            <a href="qcanvasellipse.html">QCanvasEllipse</a> *arc = new <a href="qcanvasellipse.html">QCanvasEllipse</a>(
                                            size, size, angle, extent, m_canvas );
            arc-&gt;<a href="qcanvasitem.html#setX">setX</a>( x );
            arc-&gt;<a href="qcanvasitem.html#setY">setY</a>( y );
            arc-&gt;<a href="qcanvasitem.html#setZ">setZ</a>( 0 );
            arc-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QBrush( m_elements[i].valueColor(),
                                   BrushStyle(m_elements[i].valuePattern()) ) );
            arc-&gt;<a href="qcanvasitem.html#show">show</a>();
            angle += extent;
            <a href="qstring.html">QString</a> label = m_elements[i].label();
            if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() || m_addValues != NO ) {
                label = valueLabel( label, m_elements[i].value(), total );
                CanvasText *text = new CanvasText( i, label, m_font, m_canvas );
                double proX = m_elements[i].proX( PIE );
                double proY = m_elements[i].proY( PIE );
                if ( proX &lt; 0 || proY &lt; 0 ) {
                    // Find the centre of the pie segment
                    <a href="qrect.html">QRect</a> rect = arc-&gt;<a href="qcanvaspolygonalitem.html#boundingRect">boundingRect</a>();
                    proX = ( rect.<a href="qcanvasrectangle.html#width">width</a>() / 2 ) + rect.<a href="qcanvasitem.html#x">x</a>();
                    proY = ( rect.<a href="qcanvasrectangle.html#height">height</a>() / 2 ) + rect.<a href="qcanvasitem.html#y">y</a>();
                    // Centre text over the centre of the pie segment
                    rect = text-&gt;<a href="qcanvastext.html#boundingRect">boundingRect</a>();
                    proX -= ( rect.<a href="qcanvasrectangle.html#width">width</a>() / 2 );
                    proY -= ( rect.<a href="qcanvasrectangle.html#height">height</a>() / 2 );
                    // Make proportional
                    proX /= width;
                    proY /= height;
                }
                text-&gt;<a href="qcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
                text-&gt;<a href="qcanvasitem.html#setX">setX</a>( proX * width );
                text-&gt;<a href="qcanvasitem.html#setY">setY</a>( proY * height );
                text-&gt;<a href="qcanvasitem.html#setZ">setZ</a>( 1 );
                text-&gt;<a href="qcanvasitem.html#show">show</a>();
                m_elements[i].setProX( PIE, proX );
                m_elements[i].setProY( PIE, proY );
            }
        }
    }
}


void <a name="f169"></a>ChartForm::drawVerticalBarChart(
        const double scales[], double total, int count )
{
    double width = m_canvas-&gt;width();
    double height = m_canvas-&gt;height();
    int prowidth = int(width / count);
    int x = 0;
    <a href="qpen.html">QPen</a> pen;
    pen.<a href="qpen.html#setStyle">setStyle</a>( NoPen );

    for ( int i = 0; i &lt; MAX_ELEMENTS; ++i ) {
        if ( m_elements[i].isValid() ) {
            int extent = int(scales[i]);
            int y = int(height - extent);
            <a href="qcanvasrectangle.html">QCanvasRectangle</a> *rect = new <a href="qcanvasrectangle.html">QCanvasRectangle</a>(
                                            x, y, prowidth, extent, m_canvas );
            rect-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QBrush( m_elements[i].valueColor(),
                                    BrushStyle(m_elements[i].valuePattern()) ) );
            rect-&gt;<a href="qcanvaspolygonalitem.html#setPen">setPen</a>( pen );
            rect-&gt;<a href="qcanvasitem.html#setZ">setZ</a>( 0 );
            rect-&gt;<a href="qcanvasitem.html#show">show</a>();
            <a href="qstring.html">QString</a> label = m_elements[i].label();
            if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() || m_addValues != NO ) {
                double proX = m_elements[i].proX( VERTICAL_BAR );
                double proY = m_elements[i].proY( VERTICAL_BAR );
                if ( proX &lt; 0 || proY &lt; 0 ) {
                    proX = x / width;
                    proY = y / height;
                }
                label = valueLabel( label, m_elements[i].value(), total );
                CanvasText *text = new CanvasText( i, label, m_font, m_canvas );
                text-&gt;<a href="qcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
                text-&gt;<a href="qcanvasitem.html#setX">setX</a>( proX * width );
                text-&gt;<a href="qcanvasitem.html#setY">setY</a>( proY * height );
                text-&gt;<a href="qcanvasitem.html#setZ">setZ</a>( 1 );
                text-&gt;<a href="qcanvasitem.html#show">show</a>();
                m_elements[i].setProX( VERTICAL_BAR, proX );
                m_elements[i].setProY( VERTICAL_BAR, proY );
            }
            x += prowidth;
        }
    }
}


void <a name="f170"></a>ChartForm::drawHorizontalBarChart(
        const double scales[], double total, int count )
{
    double width = m_canvas-&gt;width();
    double height = m_canvas-&gt;height();
    int proheight = int(height / count);
    int y = 0;
    <a href="qpen.html">QPen</a> pen;
    pen.<a href="qpen.html#setStyle">setStyle</a>( NoPen );

    for ( int i = 0; i &lt; MAX_ELEMENTS; ++i ) {
        if ( m_elements[i].isValid() ) {
            int extent = int(scales[i]);
            <a href="qcanvasrectangle.html">QCanvasRectangle</a> *rect = new <a href="qcanvasrectangle.html">QCanvasRectangle</a>(
                                            0, y, extent, proheight, m_canvas );
            rect-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( QBrush( m_elements[i].valueColor(),
                                    BrushStyle(m_elements[i].valuePattern()) ) );
            rect-&gt;<a href="qcanvaspolygonalitem.html#setPen">setPen</a>( pen );
            rect-&gt;<a href="qcanvasitem.html#setZ">setZ</a>( 0 );
            rect-&gt;<a href="qcanvasitem.html#show">show</a>();
            <a href="qstring.html">QString</a> label = m_elements[i].label();
            if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() || m_addValues != NO ) {
                double proX = m_elements[i].proX( HORIZONTAL_BAR );
                double proY = m_elements[i].proY( HORIZONTAL_BAR );
                if ( proX &lt; 0 || proY &lt; 0 ) {
                    proX = 0;
                    proY = y / height;
                }
                label = valueLabel( label, m_elements[i].value(), total );
                CanvasText *text = new CanvasText( i, label, m_font, m_canvas );
                text-&gt;<a href="qcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
                text-&gt;<a href="qcanvasitem.html#setX">setX</a>( proX * width );
                text-&gt;<a href="qcanvasitem.html#setY">setY</a>( proY * height );
                text-&gt;<a href="qcanvasitem.html#setZ">setZ</a>( 1 );
                text-&gt;<a href="qcanvasitem.html#show">show</a>();
                m_elements[i].setProX( HORIZONTAL_BAR, proX );
                m_elements[i].setProY( HORIZONTAL_BAR, proY );
            }
            y += proheight;
        }
    }
}


QString <a name="f171"></a>ChartForm::valueLabel(
            const <a href="qstring.html">QString</a>&amp; label, double value, double total )
{
    if ( m_addValues == NO )
        return label;

    <a href="qstring.html">QString</a> newLabel = label;
    if ( !label.<a href="qstring.html#isEmpty">isEmpty</a>() )
        if ( m_chartType == VERTICAL_BAR )
            newLabel += '\n';
        else
            newLabel += ' ';
    if ( m_addValues == YES )
        newLabel += QString::<a href="qstring.html#number">number</a>( value, 'f', m_decimalPlaces );
    else if ( m_addValues == AS_PERCENTAGE )
        newLabel += QString::<a href="qstring.html#number">number</a>( (value / total) * 100, 'f', m_decimalPlaces )
                    + '%';
    return newLabel;
}

</pre><!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>