You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/html/chart-chartform_canvas-cpp....

269 lines
12 KiB

<!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">ntqbrush.h</a>&gt;
#include &lt;<a href="qcanvas-h.html">ntqcanvas.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">TQCanvasItemList</a> list = m_canvas-&gt;allItems();
for ( TQCanvasItemList::iterator it = list.<a href="ntqvaluelist.html#begin">begin</a>(); it != list.<a href="ntqvaluelist.html#end">end</a>(); ++it )
delete *it;
// 360 * 16 for pies; TQt 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">TQCanvasEllipse</a> *arc = new <a href="qcanvasellipse.html">TQCanvasEllipse</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>( TQBrush( m_elements[i].valueColor(),
BrushStyle(m_elements[i].valuePattern()) ) );
arc-&gt;<a href="qcanvasitem.html#show">show</a>();
angle += extent;
<a href="ntqstring.html">TQString</a> label = m_elements[i].label();
if ( !label.<a href="ntqstring.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="ntqrect.html">TQRect</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="ntqpen.html">TQPen</a> pen;
pen.<a href="ntqpen.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">TQCanvasRectangle</a> *rect = new <a href="qcanvasrectangle.html">TQCanvasRectangle</a>(
x, y, prowidth, extent, m_canvas );
rect-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( TQBrush( 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="ntqstring.html">TQString</a> label = m_elements[i].label();
if ( !label.<a href="ntqstring.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="ntqpen.html">TQPen</a> pen;
pen.<a href="ntqpen.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">TQCanvasRectangle</a> *rect = new <a href="qcanvasrectangle.html">TQCanvasRectangle</a>(
0, y, extent, proheight, m_canvas );
rect-&gt;<a href="qcanvaspolygonalitem.html#setBrush">setBrush</a>( TQBrush( 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="ntqstring.html">TQString</a> label = m_elements[i].label();
if ( !label.<a href="ntqstring.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;
}
}
}
TQString <a name="f171"></a>ChartForm::valueLabel(
const <a href="ntqstring.html">TQString</a>&amp; label, double value, double total )
{
if ( m_addValues == NO )
return label;
<a href="ntqstring.html">TQString</a> newLabel = label;
if ( !label.<a href="ntqstring.html#isEmpty">isEmpty</a>() )
if ( m_chartType == VERTICAL_BAR )
newLabel += '\n';
else
newLabel += ' ';
if ( m_addValues == YES )
newLabel += TQString::<a href="ntqstring.html#number">number</a>( value, 'f', m_decimalPlaces );
else if ( m_addValues == AS_PERCENTAGE )
newLabel += TQString::<a href="ntqstring.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>TQt 3.3.8</div>
</table></div></address></body>
</html>