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.
269 lines
12 KiB
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 Classes</font></a>
|
|
| <a href="mainclasses.html">
|
|
<font color="#004faf">Main Classes</font></a>
|
|
| <a href="annotated.html">
|
|
<font color="#004faf">Annotated</font></a>
|
|
| <a href="groups.html">
|
|
<font color="#004faf">Grouped 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 <<a href="qbrush-h.html">ntqbrush.h</a>>
|
|
#include <<a href="tqcanvas-h.html">tqcanvas.h</a>>
|
|
|
|
#include <math.h> // sin, cos
|
|
|
|
#ifndef M_PI
|
|
#define M_PI 3.1415
|
|
#endif
|
|
|
|
void <a name="f167"></a>ChartForm::drawElements()
|
|
{
|
|
<a href="tqcanvasitemlist.html">TQCanvasItemList</a> list = m_canvas->allItems();
|
|
for ( TQCanvasItemList::iterator it = list.<a href="tqvaluelist.html#begin">begin</a>(); it != list.<a href="tqvaluelist.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->height() :
|
|
m_canvas->width();
|
|
double biggest = 0.0;
|
|
int count = 0;
|
|
double total = 0.0;
|
|
static double scales[MAX_ELEMENTS];
|
|
|
|
for ( int i = 0; i < MAX_ELEMENTS; ++i ) {
|
|
if ( m_elements[i].isValid() ) {
|
|
double value = m_elements[i].value();
|
|
count++;
|
|
total += value;
|
|
if ( value > biggest )
|
|
biggest = value;
|
|
scales[i] = m_elements[i].value() * scaleFactor;
|
|
}
|
|
}
|
|
|
|
if ( count ) {
|
|
// 2nd loop because of total and biggest
|
|
for ( int i = 0; i < 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->update();
|
|
}
|
|
|
|
|
|
void <a name="f168"></a>ChartForm::drawPieChart( const double scales[], double total, int )
|
|
{
|
|
double width = m_canvas->width();
|
|
double height = m_canvas->height();
|
|
int size = int(width > height ? height : width);
|
|
int x = int(width / 2);
|
|
int y = int(height / 2);
|
|
int angle = 0;
|
|
|
|
for ( int i = 0; i < MAX_ELEMENTS; ++i ) {
|
|
if ( m_elements[i].isValid() ) {
|
|
int extent = int(scales[i]);
|
|
<a href="tqcanvasellipse.html">TQCanvasEllipse</a> *arc = new <a href="tqcanvasellipse.html">TQCanvasEllipse</a>(
|
|
size, size, angle, extent, m_canvas );
|
|
arc-><a href="tqcanvasitem.html#setX">setX</a>( x );
|
|
arc-><a href="tqcanvasitem.html#setY">setY</a>( y );
|
|
arc-><a href="tqcanvasitem.html#setZ">setZ</a>( 0 );
|
|
arc-><a href="tqcanvaspolygonalitem.html#setBrush">setBrush</a>( TQBrush( m_elements[i].valueColor(),
|
|
BrushStyle(m_elements[i].valuePattern()) ) );
|
|
arc-><a href="tqcanvasitem.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 < 0 || proY < 0 ) {
|
|
// Find the centre of the pie segment
|
|
<a href="ntqrect.html">TQRect</a> rect = arc-><a href="tqcanvaspolygonalitem.html#boundingRect">boundingRect</a>();
|
|
proX = ( rect.<a href="tqcanvasrectangle.html#width">width</a>() / 2 ) + rect.<a href="tqcanvasitem.html#x">x</a>();
|
|
proY = ( rect.<a href="tqcanvasrectangle.html#height">height</a>() / 2 ) + rect.<a href="tqcanvasitem.html#y">y</a>();
|
|
// Centre text over the centre of the pie segment
|
|
rect = text-><a href="tqcanvastext.html#boundingRect">boundingRect</a>();
|
|
proX -= ( rect.<a href="tqcanvasrectangle.html#width">width</a>() / 2 );
|
|
proY -= ( rect.<a href="tqcanvasrectangle.html#height">height</a>() / 2 );
|
|
// Make proportional
|
|
proX /= width;
|
|
proY /= height;
|
|
}
|
|
text-><a href="tqcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
|
|
text-><a href="tqcanvasitem.html#setX">setX</a>( proX * width );
|
|
text-><a href="tqcanvasitem.html#setY">setY</a>( proY * height );
|
|
text-><a href="tqcanvasitem.html#setZ">setZ</a>( 1 );
|
|
text-><a href="tqcanvasitem.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->width();
|
|
double height = m_canvas->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 < MAX_ELEMENTS; ++i ) {
|
|
if ( m_elements[i].isValid() ) {
|
|
int extent = int(scales[i]);
|
|
int y = int(height - extent);
|
|
<a href="tqcanvasrectangle.html">TQCanvasRectangle</a> *rect = new <a href="tqcanvasrectangle.html">TQCanvasRectangle</a>(
|
|
x, y, prowidth, extent, m_canvas );
|
|
rect-><a href="tqcanvaspolygonalitem.html#setBrush">setBrush</a>( TQBrush( m_elements[i].valueColor(),
|
|
BrushStyle(m_elements[i].valuePattern()) ) );
|
|
rect-><a href="tqcanvaspolygonalitem.html#setPen">setPen</a>( pen );
|
|
rect-><a href="tqcanvasitem.html#setZ">setZ</a>( 0 );
|
|
rect-><a href="tqcanvasitem.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 < 0 || proY < 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-><a href="tqcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
|
|
text-><a href="tqcanvasitem.html#setX">setX</a>( proX * width );
|
|
text-><a href="tqcanvasitem.html#setY">setY</a>( proY * height );
|
|
text-><a href="tqcanvasitem.html#setZ">setZ</a>( 1 );
|
|
text-><a href="tqcanvasitem.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->width();
|
|
double height = m_canvas->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 < MAX_ELEMENTS; ++i ) {
|
|
if ( m_elements[i].isValid() ) {
|
|
int extent = int(scales[i]);
|
|
<a href="tqcanvasrectangle.html">TQCanvasRectangle</a> *rect = new <a href="tqcanvasrectangle.html">TQCanvasRectangle</a>(
|
|
0, y, extent, proheight, m_canvas );
|
|
rect-><a href="tqcanvaspolygonalitem.html#setBrush">setBrush</a>( TQBrush( m_elements[i].valueColor(),
|
|
BrushStyle(m_elements[i].valuePattern()) ) );
|
|
rect-><a href="tqcanvaspolygonalitem.html#setPen">setPen</a>( pen );
|
|
rect-><a href="tqcanvasitem.html#setZ">setZ</a>( 0 );
|
|
rect-><a href="tqcanvasitem.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 < 0 || proY < 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-><a href="tqcanvastext.html#setColor">setColor</a>( m_elements[i].labelColor() );
|
|
text-><a href="tqcanvasitem.html#setX">setX</a>( proX * width );
|
|
text-><a href="tqcanvasitem.html#setY">setY</a>( proY * height );
|
|
text-><a href="tqcanvasitem.html#setZ">setZ</a>( 1 );
|
|
text-><a href="tqcanvasitem.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>& 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 © 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>
|