工控编程吧
标题:
MFC扩展编程实例叠加与折线面积图表创显示
[打印本页]
作者:
qq263946146
时间:
2020-2-17 15:00
标题:
MFC扩展编程实例叠加与折线面积图表创显示
MFC扩展编程实例叠加与折线面积图表创显示
当前例程实现三个类型的面积 图表创建显示。
分别是普通面积图表,叠加面积图表,包含百分比叠加面积图表,还有折线面积图表。
例程界面如下。
(, 下载次数: 2)
上传
点击文件名下载附件
界面界面有两个按钮,可发分别设置面积图表的类型,曲线的显示类型。
类型可以设置为叠加,带百分比叠加,普通两种,曲线类型可以设置为直线,曲线,折线三种。
折线类型设置就可以实现折线面积图表的创建。
下面是例程实现过程。
先创建单文档工程,class CMy123View : public CBCGPFormView。
内样也是在默认的对话框资源上添加一图片控件,修改ID为IDC_CHART,用于显示图表。
然后在视窗类中添加图表变量,CBCGPChartCtrl m_wndChart;
及虚函数Create,用于创建图表。
inline double Rand (double dblStart, double dblFinish)
{
double minVal = min(dblStart, dblFinish);
double maxVal = max(dblStart, dblFinish);
return (maxVal - minVal) * (double)rand() / (RAND_MAX + 1) + minVal;
}
BOOL CMy123View::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
BOOL bRst = CBCGPFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);;
//图表初始化;
m_wndChart.SubclassDlgItem(IDC_CHART,this);
CBCGPInfoTipOptions infoTipOptions;
infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;
m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);//显示提示文本;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
BCGPChartCategory style = BCGPChartArea;
BCGPChartType type = BCGP_CT_SIMPLE;
pChart->SetChartType(style, type);//设置图表类型;
pChart->SetChartTitle(_T("面积图表"));//设置图表标题;
pChart->SetSeriesShadow(true);//数据系列显示阴影;
pChart->SetCurveType(BCGPChartFormatSeries::CCT_LINE);//图表系列线条样式;
pChart->ShowDataMarkers(true, 5, BCGPChartMarkerOptions::MS_CIRCLE);//设置数据标志点属性;
pChart->ShowDataLabels(TRUE, TRUE, TRUE, 45);//设置数据标签属性;
pChart->SetThemeOpacity(70);//设置透明度;
//创建数据系列,添加数据;
CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("第一年"));
CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("第二年"));
CBCGPChartSeries* pSeries3 = pChart->CreateSeries(_T("第三年"));
//
COleDateTime date;
CString sMonth;
COleDateTime today = COleDateTime::GetCurrentTime();
double arTemp[] = { 5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7 };
for (int nMonth = 1; nMonth <= 12; nMonth++)
{
date.SetDateTime(today.GetYear(), nMonth, 1, 0, 0, 0);
sMonth = date.Format(_T("%b"));
pSeries1->AddDataPoint(sMonth, arTemp[nMonth - 1]);
pSeries2->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
pSeries3->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
}
//添加添加到布局管理器统一管理布局;
if (GetLayout() == NULL)
return bRst;
CBCGPStaticLayout* pLayout = (CBCGPStaticLayout*)GetLayout();
if (pLayout == NULL)
return bRst;
pLayout->AddAnchor(m_wndChart.GetDlgCtrlID(), CBCGPStaticLayout::e_MoveTypeNone, CBCGPStaticLayout::e_SizeTypeBoth);
//
return bRst;
}
复制代码
最后添加设置图表类型与图表曲线类型的两按钮控件,来设置属性。
void CMy123View::OnBnClickedButton1()
{
static int nChartType=2;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
pChart->ShowSeries(true,1);
pChart->ShowSeries(true,2);
pChart->SetCurveType(BCGPChartFormatSeries::CCT_LINE);
BCGPChartCategory style = BCGPChartArea;
// BCGP_CT_DEFAULT, BCGP_CT_SIMPLE,//面积图表;
// BCGP_CT_STACKED, BCGP_CT_100STACKED,//叠加,百分比叠加面积图表;
BCGPChartType type =(BCGPChartType)nChartType;
pChart->SetChartType(style, type);//设置图表类型;
pChart->Redraw();
//
nChartType++;
nChartType = nChartType>3?1:nChartType;
}
void CMy123View::OnBnClickedButton2()
{
static int nCurveChoose=0;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
if(nCurveChoose==1)
{
pChart->ShowSeries(false,1);
pChart->ShowSeries(false,2);
}
else
{
pChart->ShowSeries(true,1);
pChart->ShowSeries(true,2);
}
BCGPChartFormatSeries::ChartCurveType curve;
if(nCurveChoose==0)
curve = BCGPChartFormatSeries::CCT_SPLINE;
else if(nCurveChoose==1)
curve = BCGPChartFormatSeries::CCT_STEP;
else
curve = BCGPChartFormatSeries::CCT_LINE;
pChart->SetCurveType(curve);
pChart->Redraw();
nCurveChoose++;
nCurveChoose = nCurveChoose>2?0:nCurveChoose;
}
复制代码
例程是基于MFC扩展库实现,可以网站内搜索下载。
例程源代码下载地址:
[weixinlianxi]1[/weixinlianxi]
[MFC408]1[/MFC408]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4