shapespiechartcontainer.cpp

来自「最新官方例子,图形,描述副,基本控件,通讯协议,等等,」· C++ 代码 · 共 177 行

CPP
177
字号
/**
 * 
 * @brief Definition of CShapesBarChartContainer
 *
 * Copyright (c) EMCC Software Ltd 2003
 * @version 1.0
 */

// INCLUDE FILES

// Class includes
#include "ShapesPieChartContainer.h"

// System includes
#include <e32math.h>	// KPi and Math:: trigonometric functions
#include <eikenv.h>		// AKN_LAF_COLOR()

// User includes
#include "ShapesModel.h"	// CShapesModel

// CONSTANTS

#define KPieRect TRect(TPoint(40, 40), TPoint(120, 120))

const TInt KStartPointX = 0; 
const TInt KStartPointY = 70;
const TInt KMaxColorValue = 215;

// ================= MEMBER FUNCTIONS =======================

/**
 * Symbian OS 2 phase constructor.
 * Constructs the CShapesPieChartContainer using the NewLC method, popping
 * the constructed object from the CleanupStack before returning it.
 * 
 * @param aShapesModel a reference to the model
 * @param aRect The rectangle for this window
 * @return The newly constructed CShapesPieChartContainer
 */
CShapesPieChartContainer* CShapesPieChartContainer::NewL(CShapesModel& aShapesModel, const TRect& aRect)
	{
	CShapesPieChartContainer* self = CShapesPieChartContainer::NewLC(aShapesModel, aRect);
	CleanupStack::Pop(self);
	return self;
	}

/**
 * Symbian OS 2 phase constructor.
 * Constructs the CShapesPieChartContainer using the constructor and ConstructL 
 * method, leaving the constructed object on the CleanupStack before returning it.
 * 
 * @param aShapesModel a reference to the model
 * @param aRect The rectangle for this window
 * @return The newly constructed CShapesPieChartContainer
 */
CShapesPieChartContainer* CShapesPieChartContainer::NewLC(CShapesModel& aShapesModel,  const TRect& aRect)
	{
	CShapesPieChartContainer* self = new (ELeave) CShapesPieChartContainer(aShapesModel);
	CleanupStack::PushL(self);
	self->ConstructL(aRect);
	return self;
	}

/**
 * C++ constructor
 *
 * @param aShapesModel a reference to the model
 */
CShapesPieChartContainer::CShapesPieChartContainer(CShapesModel& aShapesModel)
 : iShapesModel(aShapesModel)
	{
	}

/**
 * Symbian OS 2nd phase constructor.  Creates a Window for the control to draw to.
 * Activates the control
 *
 * @param aRect The rectangle for this window
 */ 
void CShapesPieChartContainer::ConstructL(const TRect& aRect)
	{
	CreateWindowL();
	SetRect(aRect);
	ActivateL();
	}

/**
 * Destructor
 */
CShapesPieChartContainer::~CShapesPieChartContainer()
	{
	}

/**
 * Draw function calls DrawPie()
 *
 * @param aRect The rectangle to draw to (not used)
 */
void CShapesPieChartContainer::Draw(const TRect& /*aRect*/) const
	{
	DrawPie();	
	}

/**
 * Draws the pie chart using the GC DrawPie() function
 * Trigonometrical rotation functions are used to calculate correct
 * radials for each pie segment
 */
void CShapesPieChartContainer::DrawPie() const
	{
	// Clear the graphics context.
	CWindowGc& gc = SystemGc();	
	gc.Clear();
	
	// Set up variables necessary for calculations
	const TPoint centerOfPie(KPieRect.Center());

	TPoint startPoint(KStartPointX, KStartPointY);	// cartesian based coordinate system
	TPoint endPoint(startPoint);					// cartesian based coordinate system

	// Set up the random seed.
	TTime time;
	time.HomeTime();
	TInt64 colorSeed = time.Int64();

	const TReal sumOfData = iShapesModel.SumOfElements();	// TReal for floating-point division.
	const TInt numElements = iShapesModel.NumElements();

	for (TInt i = 0; i < numElements; i++)
		{
		// Obtain a value from the model
		TInt currentVal = iShapesModel.ElementAt(i);
	
		if (currentVal)
			{
			// Generate a random color and set brush parameters - note that this will result in
			// different colors being used each time this view is selected (even for the same data).
			TInt randomColor = Math::Rand(colorSeed);

			randomColor = randomColor % KMaxColorValue;		
			TRgb brushColor = AKN_LAF_COLOR(randomColor);

			gc.SetBrushColor(brushColor);
			gc.SetBrushStyle(CGraphicsContext::EForwardDiagonalHatchBrush);
		
			// Make the start of the new pie slice the end of the last one
			startPoint = endPoint;

			// Calculate the end point for the new pie segment based on rotation equations
			
			// Calculate the angle as a percentage of the data.
			const TReal angleInRadians = (currentVal * (2 * KPi)) / sumOfData;

			// Calculate the sine and cosine of the angle.
			TReal trigResultCos;
			Math::Cos(trigResultCos, angleInRadians);

			TReal trigResultSin;
			Math::Sin(trigResultSin, angleInRadians);

			// Calculate end point cartesian coordinates, casting results back to integers.
			const TInt newX = (TInt)((startPoint.iX * trigResultCos) - (startPoint.iY * trigResultSin)); 
			const TInt newY = (TInt)((startPoint.iX * trigResultSin) + (startPoint.iY * trigResultCos));
			endPoint.SetXY(newX, newY);
		
			// Adjust from Cartesian to screen coordinates
			const TPoint pieStartPoint(centerOfPie.iX + startPoint.iX, centerOfPie.iY - startPoint.iY);
			const TPoint pieEndPoint(centerOfPie.iX + endPoint.iX, centerOfPie.iY - endPoint.iY);
		
			// Draw the pie
			gc.DrawPie(KPieRect, pieStartPoint, pieEndPoint);
			}
		}
	}
	
// End of File

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?