Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / MFC

Pie Chart Control

Rate me:
Please Sign up or sign in to vote.
4.25/5 (11 votes)
18 Aug 2001 117K   5.7K   57  
This control creates a simple Pie Chart in a dialog
#if !defined(AFX_PIECHARTCTRL_H__31DF2BE2_22B5_11D2_813B_A9FDC8C0FA85__INCLUDED_)
#define AFX_PIECHARTCTRL_H__31DF2BE2_22B5_11D2_813B_A9FDC8C0FA85__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

// PieChartCtrl.h : header file
//
// Written by Yuheng Zhao (yuheng@ministars.com)
// Copyright (c) 1998.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. If 
// the source code in  this file is used in any commercial application 
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.
// It's free - so you get what you pay for.

#include <afxtempl.h>
// PieChartCtrl.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CPieChartCtrl window
class CPieChartPiece : public CObject
{
public:
	CPieChartPiece() {;}
	~CPieChartPiece() {if (m_brushBack.m_hObject) m_brushBack.DeleteObject();}

	COLORREF m_colorBack;
	COLORREF m_colorText;
	CBrush m_brushBack;	
	int m_nAngle;
	CString m_strInfo;
};

typedef CTypedPtrArray <CObArray, CPieChartPiece*> CChartPieceArray;

class CPieChartCtrl : public CWnd
{
// Construction
public:
	CPieChartCtrl();

// Attributes
public:
	int m_nStartAngle; // 0-359
	COLORREF m_colorDefault;
	COLORREF m_colorLine;

	CChartPieceArray m_chartPieces;

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CPieChartCtrl)
	//}}AFX_VIRTUAL

// Implementation
public:
	int m_nTitleHeight;
	CFont m_fontTitle;
	CFont m_fontInfo;
	void SetTitle(const CString& str);
	CString m_strTitle;
	void GetItemColor(int i, COLORREF& color);
	void Reset();
	CPoint m_ptOldPt;
	BOOL m_bCaptured;
	void CountPoint(int nAngle, CPoint& pt, BOOL bPercent = FALSE);
	BOOL AddPiece(COLORREF colorBack, COLORREF colorText,
		int nAngle, const CString& str = _T(""));
	void RecalcRect();
	CRect m_rectChart;
	static BOOL RegisterWndClass(HINSTANCE hInstance);
	virtual ~CPieChartCtrl();

	// Generated message map functions
protected:
	//{{AFX_MSG(CPieChartCtrl)
	afx_msg void OnPaint();
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////


//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_PIECHARTCTRL_H__31DF2BE2_22B5_11D2_813B_A9FDC8C0FA85__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions