shapeslinechartcontainer.cpp

来自「series60 应用程序开发的源代码 series60 应用程序开发的源代码」· C++ 代码 · 共 186 行

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

// INCLUDE FILES

// Class includes
#include "ShapesLineChartContainer.h"

// System includes
#include <e32math.h>    // Math::Rand
#include <eikenv.h>        // AKN_LAF_COLOR

// User includes
#include "ShapesModel.h"

// CONSTANTS

const TInt KAxisScaleFactor = 10;
const TInt KMaxColorValue = 215;

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

/**
 * Symbian OS 2 phase constructor.
 * Constructs the CShapesLineChartContainer 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 CShapesLineChartContainer
 */
CShapesLineChartContainer* CShapesLineChartContainer::NewL(CShapesModel& aShapesModel, const TRect& aRect)
{
    CShapesLineChartContainer* self = CShapesLineChartContainer::NewLC(aShapesModel, aRect);
    CleanupStack::Pop(self);
    return self;
}

/**
 * Symbian OS 2 phase constructor.
 * Constructs the CShapesLineChartContainer 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 CShapesLineChartContainer
 */
CShapesLineChartContainer* CShapesLineChartContainer::NewLC(CShapesModel& aShapesModel, const TRect& aRect)
{
    CShapesLineChartContainer* self = new (ELeave) CShapesLineChartContainer(aShapesModel);
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    return self;
}

/**
 * C++ constructor
 *
 * @param aShapesModel a reference to the model
 */
CShapesLineChartContainer::CShapesLineChartContainer(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 CShapesLineChartContainer::ConstructL(const TRect& aRect)
{
    CreateWindowL();
    SetRect(aRect);
    ActivateL();
}

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

/**
 * Draw functions calls axis and chart drawing functions
 *
 * @param aRect Rectangular area of control
 */
void CShapesLineChartContainer::Draw(const TRect& aRect) const
{
    // Clear the graphics context.
    CWindowGc& gc = SystemGc();
    gc.Clear();

    TRect axisRect;
    DrawAxis(aRect, axisRect);
    TRAPD (err, DrawLineChartL(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 CShapesLineChartContainer::DrawAxis(const TRect& aRect, TRect& aAxisRect) const
{
    // Set the pen style and color.
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ESolidPen);
    gc.SetPenColor(KRgbBlack);

    // 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 line chart using the DrawPolyLine() function of the
 * system graphics context
 *
 * @param aAxisRect The rectangular area of the axis
 */
void CShapesLineChartContainer::DrawLineChartL(TRect& aAxisRect) const
{
    // Get a refernce to the graphics context.
    CWindowGc& gc = SystemGc();
        
    // Set up the random seed.
    TTime time;
    time.HomeTime();
    TInt64 colorSeed = time.Int64();

    // Set the pen to a random color.
    const TInt randomColor = Math::Rand(colorSeed);
    const TRgb penColor = AKN_LAF_COLOR(randomColor % KMaxColorValue);

    gc.SetPenColor(penColor);
    gc.SetPenStyle(CGraphicsContext::ESolidPen);

    // Variables needed for calculation of points
    const TInt numElements = iShapesModel.NumElements();
    const TInt maxValue = iShapesModel.GetMaxValue();

    if (!maxValue)
        return;

    const TPoint origin(aAxisRect.iTl.iX, aAxisRect.iBr.iY);    // This is 0, 0 translated
    const TInt widthOfLineSpacer = aAxisRect.Width() / (numElements + 1);

    // Create array of points (1 is added to allow for origin point)
    CArrayFix<TPoint>* polyLineArray = new (ELeave) CArrayFixFlat<TPoint>(numElements + 1);
    CleanupStack::PushL(polyLineArray);
    polyLineArray->AppendL(origin);

    // Iterate value model. Calculate points based on values and append to points array.
    for (TInt i = 0; i < numElements; i++)
    {
        // Calculated height of line based on input data
        TInt heightOfCurrentLine = (iShapesModel.ElementAt(i) * aAxisRect.Height()) / maxValue;
        TInt xOfCurrentLine = origin.iX + ((i + 1) * widthOfLineSpacer);
        TInt yOfCurrentLine = origin.iY - heightOfCurrentLine;
        polyLineArray->AppendL(TPoint(xOfCurrentLine, yOfCurrentLine));                 
    }

    gc.DrawPolyLine(polyLineArray);

    CleanupStack::PopAndDestroy(polyLineArray);
}

// End of File

⌨️ 快捷键说明

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