MFC扩展编程实例3D线形图表创建与交互操作
例程创建了一个三维的线形图表,界面如下。
25.MFC扩展编程实例3D线形图表创建与交互操作
界面显示有三个数据系列,也就是三根三维的线条。
线条的标签显示角度也可以设置,三维线条的宽度也可以设置。
通过界面圆形控件可以旋转图表,也可以通过鼠标右键来旋转图表。
下面是例程实现过程与关键代码。
先创建基于class CMy123View : public CBCGPFormView的单文档工程。
在视窗对话框资源中添加两个图片控件,修改ID为IDC_CHART,IDC_ROTATE,
用于显示图表与操作图表 。
在视窗类中添加变量,成员函数与虚函数Create。
CBCGPChartCtrl m_wndChart;
CBCGPRotationCtrl m_wndRotate;
void RotateChart(CBCGPRotationObject::RotationElement hit, double xDelta = 10., double yDelta = 10., double persperctiveDelta = 0.1);
在Create函数中创建上面两变量初始化图表。
- 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_wndRotate.SubclassDlgItem(IDC_ROTATE,this);
- m_wndRotate.GetRotationObject()->SetAutorepeatMode(100);
- m_wndRotate.GetRotationObject()->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);
- m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
- m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);
- m_wndRotate.GetRotationObject()->EnableFlatIcons();
- //获取图表指针;
- m_wndChart.SubclassDlgItem(IDC_CHART,this);//子类化图表;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- pChart->SetChartTitle(_T("3D线形图表"));//图表标题设置;
- pChart->SetThemeOpacity(70);//图表主题透明度设置;
- pChart->EnableMouseTrackingMode(BCGPChartHitInfo::HIT_DIAGRAM);
- BCGPChartCategory category = BCGPChartLine3D;//图表种类,F12查看全部;
- BCGPChartType type = BCGP_CT_SIMPLE;//BCGP_CT_100STACKED;//BCGP_CT_STACKED;//图表类型,按F12查看全部;
- pChart->SetChartType(category, type);//设置图表类别;
- //设置XY轴刻度;
- CBCGPChartAxis* pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
- CBCGPChartAxis* pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
- pYAxis->m_majorTickMarkType = CBCGPChartAxis::TMT_CROSS;
- pYAxis->m_minorTickMarkType = CBCGPChartAxis::TMT_CROSS;
- pXAxis->m_majorTickMarkType = CBCGPChartAxis::TMT_CROSS;
- pXAxis->m_minorTickMarkType = CBCGPChartAxis::TMT_CROSS;
- //设置3D场景背部与底部;
- DWORD dwoFlags = CBCGPChartDiagram3D::DWO_OUTLINE_ALL;
- dwoFlags |= (CBCGPChartDiagram3D::DWO_DRAW_ALL_WALLS | CBCGPChartDiagram3D::DWO_DRAW_FLOOR);
- pChart->GetDiagram3D()->SetDrawWallOptions((CBCGPChartDiagram3D::DrawWallOptions)dwoFlags);
- pChart->GetDiagram3D()->SetThickWallsAndFloor(true);
- //创建数据系列;
- CBCGPChartSeries* pSeries1 = DYNAMIC_DOWNCAST(CBCGPChartSeries, pChart->CreateSeries(_T("能源")));
- CBCGPChartSeries* pSeries2 = DYNAMIC_DOWNCAST(CBCGPChartSeries, pChart->CreateSeries(_T("税收")));
- CBCGPChartSeries* pSeries3 = DYNAMIC_DOWNCAST(CBCGPChartSeries, pChart->CreateSeries(_T("维护")));
- //数据系列添加数据;
- srand((unsigned)time(NULL));
- COleDateTime now = COleDateTime::GetCurrentTime();
- CString sYear;
- for(int i=5;i>0;i--)
- {
- sYear.Format(_T("%d"), now.GetYear() - i);
- pSeries1->AddDataPoint(sYear, rand()%20+1);
- }
- pSeries2->AddDataPoint(9.);
- pSeries2->AddDataPoint(10.);
- pSeries2->AddDataPoint(14.);
- pSeries2->AddDataPoint(11.);
- pSeries2->AddDataPoint(23.);
- pSeries3->AddDataPoint(11.);
- pSeries3->AddDataPoint(7.);
- pSeries3->AddDataPoint(16.);
- pSeries3->AddDataPoint(10.);
- pSeries3->AddDataPoint(13.);
- //添加添加到布局管理器统一管理布局;
- 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;
- }
复制代码 创建图表时,关键代码如下。
BCGPChartCategory category = BCGPChartLine3D;//图表种类,F12查看全部;
BCGPChartType type = BCGP_CT_SIMPLE;//BCGP_CT_100STACKED;//BCGP_CT_STACKED;//图表类型,按F12查看全部;
pChart->SetChartType(category, type);//设置图表类别;
创建的是三维线形图表,所以使用了参数BCGPChartLine3D和BCGP_CT_SIMPLE。
如果想创建叠加类型的,可以使用另外参数BCGP_CT_100STACKED;//BCGP_CT_STACKED。
图表创建完成后,就是关联控件变量m_wndRotate的点击函数来旋转图表。
afx_msg void OnRotate();
ON_BN_CLICKED(IDC_ROTATE, OnRotate)
以及添加鼠标点击与平移函数来交互旋转图表。
afx_msg LRESULT OnMouseTrack(WPARAM wp, LPARAM lp);
afx_msg LRESULT OnMouseDown(WPARAM wp, LPARAM lp);
ON_REGISTERED_MESSAGE(BCGM_ON_CHART_MOUSE_TRACK, OnMouseTrack)
ON_REGISTERED_MESSAGE(BCGM_ON_CHART_MOUSE_DOWN, OnMouseDown)
图表标签显示角度与线条宽度设置,可以选择实现,对应是通过两按钮实现。
- void CMy123View::OnBnClickedButton1()//图表修改数据标签位置;
- {
- static int nAng=0;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- pChart->ShowDataLabels(true,true,true,nAng);
- pChart->Redraw();
- //
- nAng+=45;
- nAng = nAng>360?45:nAng;
- }
- void CMy123View::OnBnClickedButton2()
- {
- static int nThick = 1;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartSeries* pSeries = pChart->GetSeries(i);
- ASSERT_VALID(pSeries);
- pSeries->SetDataPoint3DLineThickness(nThick);
- }
- pChart->SetDirty(true,true);
- //
- nThick++;
- nThick = nThick>10?1:nThick;
- }
复制代码 记得在视窗类构造函数中开启布局管理器EnableLayout()。
例程用到的MFC扩展库,可以搜索BCGP下载使用。
例程源代码下载:
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|