/*********************************************************************
* BarChart.cpp
* implementation of
* CChartBar,
* CChartData,
* CChartPrinter,
* CChartGCalc,
* CGDIGrid,
*
* Auther: Hamed.M.
* EMail : HamedMosavi @ gmail.com
*
* CAUTION Disclaimer:
* This code is provided as is without any warranty of
* any kind. The author is not responsible for whatever
* problem or damages that may occur as aresault of using
* this code. Use this entirely at your own risk.
* So don't try to find me if this code made your dog
* Sound like a frog, or burned your car, or even made your
* girl/boy friend shout on you. I am not responsible ;)
*
*
********************************************************************/
/********************************************************************
* History:
*
* Released : 2007/01/11
*
* BugFixes:
* 2007/01/11
* unsigned long double converted to -> double
* Some scaling bugs fixed
* Tooltips added
*
* 2007/01/15
* Some function changed.
* Tooltip updated.
* Some comments added.
*
* 2007/01/20
* Tooltips now behave much like tooltips(!)
*
* 2007/01/24
* Database connectivity added.
*
* 2007/01/26
* Tooltip bug fixed (SetCapture added).
********************************************************************/
#include "stdafx.h"
#include "BarChart.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////
// CBarChart Class
//
// Represents a chart control. The chart shows some bars
// Each bar has a value. Bars heights will be calculated
// Regarding other bars. The bars are able to show a percentage
// Instead of a value. The bar with maximum value has 100% and
// Percentage of other bars will be calculated in accordance to
// This maximum value.
//
///////////////////////////////////////////////////////////////////////
CBarChart::CBarChart()
{
m_rctClient = CRect(0, 0, 0, 0);
SetBKColor(RGB(240, 240, 240));
m_pBmpBars = NULL;
m_pBmpOld = NULL;
ShowLabel(TRUE);
ShowGrid(TRUE);
ShowBarText(1, TRUE);
SetAutoScale(FALSE);
ShowTooltip(TRUE);
m_szTitle = "";
m_szLastErr = "";
}
CBarChart::~CBarChart()
{
if (m_brFrame.GetSafeHandle())
{
m_brFrame.DeleteObject();
}
if (m_pBmpOld)
{
m_dcMem.SelectObject(m_pBmpOld);
}
if (m_pBmpBars)
{
m_pBmpBars->DeleteObject();
delete m_pBmpBars;
m_pBmpBars = NULL;
}
if (m_dcMem.GetSafeHdc())
{
m_dcMem.DeleteDC();
}
}
BEGIN_MESSAGE_MAP(CBarChart, CWnd)
//{{AFX_MSG_MAP(CBarChart)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBarChart message handlers
// Creates the child control.
// Parameters:
// rect : Defines bounding rectangle of the control
// pParentWnd : A pointer to the window on which this control will be shown
// nControlID : Identifyer of the control
// szChartTitle: Defines the title at the bottom of the control, can be a simple description of the chart.
BOOL CBarChart::Create(const RECT &rect, CWnd *pParentWnd, UINT nControlID, CString szChartTitle)
{
if (GetSafeHwnd())
{
return FALSE;
}
m_rctClient = rect;
m_szTitle = szChartTitle;
BOOL bRes = CWnd::Create( NULL, NULL, WS_CHILD|WS_VISIBLE, rect, pParentWnd, nControlID );
if (TRUE == bRes)
{
InitToolTips();
}
return bRes;
}
BOOL CBarChart::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CBarChart::OnPaint()
{
CPaintDC dc(this);
GetClientRect(&m_rctClient);
if (m_dcMem.GetSafeHdc())
{
if (m_bAutoScale)
{
dc.StretchBlt(0, 0, m_rctClient.Width(), m_rctClient.Height(),
&m_dcMem, 0, 0, m_rctMemPic.Width(), m_rctMemPic.Height(), SRCCOPY);
}
else
{
dc.BitBlt(0, 0, m_rctClient.Width(), m_rctClient.Height(),
&m_dcMem, 0, 0, SRCCOPY);
}
}
else
{
RedrawChart(&dc);
}
}
// Prints the content of the child control (WYSIWYG)
// Parameters:
// bCompatible :
// Default is set to FALSE
// Set this TRUE if you have any difficaulty printing your chart.
// It's defined since some printers might not be able to print DDB
// Bitmaps, setting this to TRUE force the function to use DIB instead.
void CBarChart::Print(BOOL bCompatible)
{
CClientDC dc(this);
m_printer.Print(&dc, &m_dcMem, m_pBmpBars,
(CString)AfxGetAppName() + (CString)" - " + (CString)m_szTitle,
m_rctMemPic, m_bAutoScale, bCompatible);
}
void CBarChart::RedrawChart(CDC* pDC)
{
if (!m_dcMem.GetSafeHdc())
{
m_dcMem.CreateCompatibleDC(pDC);
}
// Deselect and destroy old bitmap, and create a new one
ResetBitmap(pDC);
Refresh();
}
void CBarChart::ResetBitmap(CDC* pDC)
{
// Deselect and destroy old bitmap
if (m_pBmpOld)
{
m_dcMem.SelectObject(m_pBmpOld);
}
if (m_pBmpBars)
{
m_pBmpBars->DeleteObject();
delete m_pBmpBars;
m_pBmpBars = NULL;
}
// Re create bitmap
m_rctMemPic.top = 0;
m_rctMemPic.left = 0;
m_rctMemPic.bottom = m_bars.GetBarHeight();
m_rctMemPic.right = m_bars.GetBarWidth();
if (m_rctMemPic.Width()<m_rctClient.Width())
{
m_rctMemPic.right = m_rctClient.Width();
}
if (m_rctMemPic.Height()<m_rctClient.Height())
{
m_rctMemPic.bottom = m_rctClient.Height();
}
m_pBmpBars = new CBitmap;
m_pBmpBars->CreateCompatibleBitmap(pDC, m_rctMemPic.right, m_rctMemPic.bottom);
m_pBmpOld = (CBitmap*)m_dcMem.SelectObject(m_pBmpBars);
}
// Sets/Resets the scale capability
// Parameters:
// bAutoScale :
// If set to TRUE force the chart to scale itself to the parent
// Window, regardless of it's initial bounding rectangle.
// If set to FALSE returns the size of the chart to the original
// Bounding rectangle.
// Note that chart saves it's initial state of creation.
void CBarChart::SetAutoScale(BOOL bAutoScale)
{
m_bAutoScale = bAutoScale;
}
// Sets the color of the background rectangle of the chart
// Parameters:
// colorBK :
// The color in COLORREF format(eg.RGB(250,250,250))
void CBarChart::SetBKColor(COLORREF colorBK)
{
// Reset background color
m_colorBK = colorBK;
// Reset frame brush
if (m_brFrame.GetSafeHandle())
{
m_brFrame.DeleteObject();
}
m_brFrame.CreateSolidBrush(DifColor(colorBK, CHART_BORDER_DARKNESS));
// Reset grid
m_grid.SetColor(DifColor(colorBK, 50));
// Redraw window, if it is created
if (GetSafeHwnd())
{
Invalidate();
}
}
// Adds a bar to the chart
// Parameters:
// udValue : Value of the bar
// szLabel : Label of the bar, will be shown under the bar
// color : Color of the bar. This color will be used to create a gradient.
// bRepaint: If set to TRUE, forces the chart to show changes on the screen
// Default is set to false. It is recommended to set it to true just
// For last Add bar of the chart, so the chart won't be forced to
// Show changes every time a single bar is added.
void CBarChart::AddBar(double udValue, CString szLabel, COLORREF color, BOOL bRepaint)
{
m_bars.AddBar(udValue, szLabel, color);
if (bRepaint)
{
CClientDC dc(this);
RedrawChart(&dc);
Invalidate();
}
}
// Shows/Hides the labels under the chart control
// Parameters:
// bShow : Set to TRUE to show labels
// bRedraw: If set to TRUE, forces the chart to show changes on the screen
// Default is set to false.
void CBarChart::ShowLabel(BOOL bShow, BOOL bRedraw)
{
m_bars.ShowLabel(bShow);
if(bRedraw)
{
Refresh();
}
}
// Shows/Hides the grid behind t
没有合适的资源?快使用搜索试试~ 我知道了~
图表类 BarChart
共17个文件
h:5个
cpp:4个
rc:1个
5星 · 超过95%的资源 需积分: 13 15 下载量 13 浏览量
2016-08-02
18:32:02
上传
评论
收藏 40KB ZIP 举报
温馨提示
支持设置标题 背景颜色 网格 增加图表 图表标题 提示 保存bmp 打印 读取数据库显示
资源推荐
资源详情
资源评论
收起资源包目录
BarChart_demo .zip (17个子文件)
BarChartCtrl
BarChartCtrl.dsp 4KB
resource.h 2KB
BarChartCtrl.clw 3KB
BarChartCtrl.plg 2KB
BarChartCtrl.rc 8KB
BarChart.cpp 54KB
BarChartCtrl.cpp 2KB
res
BarChartCtrl.ico 1KB
BarChartCtrl.rc2 404B
BarChartCtrlDlg.cpp 11KB
BarChartCtrlDlg.h 2KB
BarChartCtrl.opt 60KB
BarChartCtrl.dsw 549B
StdAfx.cpp 214B
StdAfx.h 1KB
BarChart.h 14KB
BarChartCtrl.h 1KB
共 17 条
- 1
资源评论
- elichen92017-03-19相当漂亮的图表,谢谢分享
kuankuan_qiao
- 粉丝: 54
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功