shapesbarchartcontainer.cpp
来自「series60 应用程序开发的源代码 series60 应用程序开发的源代码」· C++ 代码 · 共 190 行
CPP
190 行
/**
*
* @brief Definition of CShapesBarChartContainer
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
// INCLUDE FILES
// Class includes
#include "ShapesBarChartContainer.h"
// System includes
#include <e32math.h> // Math::Rand
#include <eikenv.h> // AKN_LAF_COLOR
// User includes
#include "ShapesModel.h" // CShapesModel
// CONSTANTS
const TInt KAxisScaleFactor = 10;
const TInt KMaxColorValue = 215;
const TInt KColorBlack = 215;
// ================= MEMBER FUNCTIONS =======================
/**
* Symbian OS 2 phase constructor.
* Constructs the CShapesBarChartContainer 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 CShapesBarChartContainer
*/
CShapesBarChartContainer* CShapesBarChartContainer::NewL(CShapesModel& aShapesModel, const TRect& aRect)
{
CShapesBarChartContainer* self = CShapesBarChartContainer::NewLC(aShapesModel, aRect);
CleanupStack::Pop(self);
return self;
}
/**
* Symbian OS 2 phase constructor.
* Constructs the CShapesBarChartContainer 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 CShapesBarChartContainer
*/
CShapesBarChartContainer* CShapesBarChartContainer::NewLC(CShapesModel& aShapesModel, const TRect& aRect)
{
CShapesBarChartContainer* self = new (ELeave) CShapesBarChartContainer(aShapesModel);
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
/**
* C++ constructor
*
* @param aShapesModel a reference to the model
*/
CShapesBarChartContainer::CShapesBarChartContainer(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 CShapesBarChartContainer::ConstructL(const TRect& aRect)
{
CreateWindowL();
SetRect(aRect);
ActivateL();
}
/**
* Destructor
*/
CShapesBarChartContainer::~CShapesBarChartContainer()
{
}
/**
* Draw functions calls axis and chart drawing functions
*
* @param aRect Rectangular area of control
*/
void CShapesBarChartContainer::Draw(const TRect& aRect) const
{
// Clear the graphics context.
CWindowGc& gc = SystemGc();
gc.Clear();
TRect axisRect;
DrawAxes(aRect, axisRect);
DrawChart(axisRect);
}
/**
* Draws the axes of the chart
*
* @param aRect Rectangular area of control
* @param aAxisRect Returns a rectangular area formed by the axes generated by this function
*/
void CShapesBarChartContainer::DrawAxes(const TRect& aRect, TRect& aAxisRect) const
{
// Get a reference to the graphics context.
CWindowGc& gc = SystemGc();
// Set the pen style and color.
gc.SetPenStyle(CGraphicsContext::ESolidPen);
TRgb colorBlack = AKN_LAF_COLOR(KColorBlack);
gc.SetPenColor(colorBlack);
// Calculate the extent of the axes.
const TInt yAxisMax = aRect.Height() / KAxisScaleFactor;
const TInt originY = aRect.Height() - yAxisMax;
const TInt originX = aRect.Width() / KAxisScaleFactor;
const TInt xAxisMax = aRect.Width() - originX;
// Draw axes and set extent of aAxisRect.
gc.DrawLine(TPoint(originX, yAxisMax), TPoint(originX, originY));
gc.DrawLine(TPoint(originX, originY), TPoint(xAxisMax, originY));
aAxisRect.SetRect(TPoint(originX, yAxisMax), TPoint(xAxisMax, originY));
}
/**
* Draws the bar chart through the DrawRect() function of the
* system graphics context
*
* @param aAxisRect The rectangular area of the axis
*/
void CShapesBarChartContainer::DrawChart(TRect& aAxisRect) const
{
// Get a refernce to the graphics context.
CWindowGc& gc = SystemGc();
// Don't use the pen.
gc.SetPenStyle(CGraphicsContext::ENullPen);
// Set up the random seed.
TTime time;
time.HomeTime();
TInt64 colorSeed = time.Int64();
// Calculate values.
const TInt numElements = iShapesModel.NumElements();
const TInt maxValue = iShapesModel.GetMaxValue();
if (!maxValue)
return;
TPoint originOfCurrentBar(aAxisRect.iTl.iX, aAxisRect.iBr.iY); // This is 0,0 translated
const TInt widthOfBarAndSpacer = aAxisRect.Width() / KAxisScaleFactor;
// Iterate value model and draw bars.
for (TInt i = 0; i < numElements; i++)
{
// Calculate height of bar based on input data
TInt heightOfCurrentBar = (iShapesModel.ElementAt(i) * aAxisRect.Height()) / maxValue;
originOfCurrentBar.iX += widthOfBarAndSpacer;
TRect rectOfCurrentBar(
TPoint(originOfCurrentBar.iX, (originOfCurrentBar.iY - heightOfCurrentBar)),
TPoint(originOfCurrentBar.iX + widthOfBarAndSpacer, originOfCurrentBar.iY));
// Generate a random color and set brush parameters
const TInt randomColor = Math::Rand(colorSeed);
TRgb brushColor = AKN_LAF_COLOR(randomColor % KMaxColorValue);
gc.SetBrushColor(brushColor);
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
// Draw Rectangle
gc.DrawRect(rectOfCurrentBar);
originOfCurrentBar.iX += widthOfBarAndSpacer;
}
}
// End of File
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?