上位机MFC扩展编程图表XY轴名称网格间隔显示及反向显示
XY轴也是图表编程时经常要处理的功能,
这个实例实现图表XY轴的显示,
XY轴名称,网格,间隔标识的显示,及反向显示功能。
效果如下图:
点击界面对应的单选框按钮,可以实现对应的功能,
每个名称具体指图表的哪个部位,可以运行例程查看效果,一目了然。
整体实现起来还是很简单方便的,主要是通过集成类CBCGPChartVisualObject的成员函数实现。
对应例程中实现的函数为:
- void CMy123View::UpdateChart()
- {
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- //X轴,轴名称,轴间隔交错,轴网格线显示与反向显示;
- BOOL bXShow = ((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck();
- BOOL bXNameShow = ((CButton*)GetDlgItem(IDC_CHECK2))->GetCheck();
- BOOL bXIntervalInterlacingShow = ((CButton*)GetDlgItem(IDC_CHECK3))->GetCheck();
- BOOL bXGridLineShow = ((CButton*)GetDlgItem(IDC_CHECK4))->GetCheck();
- pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS)->m_bReverseOrder=((CButton*)GetDlgItem(IDC_CHECK5))->GetCheck();
- pChart->ShowAxis(BCGP_CHART_X_PRIMARY_AXIS,bXShow);
- pChart->SetAxisName(BCGP_CHART_X_PRIMARY_AXIS, _T("X轴"));
- pChart->ShowAxisName(BCGP_CHART_X_PRIMARY_AXIS,bXNameShow);
- pChart->ShowAxisIntervalInterlacing(BCGP_CHART_X_PRIMARY_AXIS,bXIntervalInterlacingShow);
- pChart->ShowAxisGridLines(BCGP_CHART_X_PRIMARY_AXIS,bXGridLineShow);
- //Y轴,轴名称,轴间隔交错,轴网格线显示与反向显示;
- BOOL bYShow = ((CButton*)GetDlgItem(IDC_CHECK6))->GetCheck();
- BOOL bYNameShow = ((CButton*)GetDlgItem(IDC_CHECK7))->GetCheck();
- BOOL bYIntervalInterlacingShow = ((CButton*)GetDlgItem(IDC_CHECK8))->GetCheck();
- BOOL bYGridLineShow = ((CButton*)GetDlgItem(IDC_CHECK9))->GetCheck();
- pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS)->m_bReverseOrder=((CButton*)GetDlgItem(IDC_CHECK10))->GetCheck();
- pChart->ShowAxis(BCGP_CHART_Y_PRIMARY_AXIS,bYShow);
- pChart->SetAxisName(BCGP_CHART_Y_PRIMARY_AXIS, _T("Y轴"));
- pChart->ShowAxisName(BCGP_CHART_Y_PRIMARY_AXIS,bYNameShow);
- pChart->ShowAxisIntervalInterlacing(BCGP_CHART_Y_PRIMARY_AXIS,bYIntervalInterlacingShow);
- pChart->ShowAxisGridLines(BCGP_CHART_Y_PRIMARY_AXIS,bYGridLineShow);
- //图表重绘,以上设置立即生效;
- pChart->Redraw();
- }
复制代码 可以看到实现轴相关的显示隐藏主要函数有ShowAxis,SetAxisName,ShowAxisIntervalInterlacing,ShowAxisGridLines。
以及轴是否反向显示是通过对轴成员变量m_bReverseOrder的赋值。
每个单选按钮的选择函数只要调用上面的函数UpdateChart就可。
当前前提肯定得创建图表控件,添加一些数据用来演示。
例程是通过关联消息WM_INITDIALOG的处理函数实现图表初始化,
ON_MESSAGE(WM_INITDIALOG,OnInitDialog)。
- LRESULT CMy123View::OnInitDialog(WPARAM wParam, LPARAM lParam)
- {
- LRESULT lRes = CBCGPFormView::HandleInitDialog (wParam, lParam);
- //获取图表指针;
- m_wndChart.SubclassDlgItem(IDC_CHART,this);
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- //创建数据系列1
- CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("Series 1"));
- pSeries1->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE);
- //创建数据系列2
- CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("Series 2"));
- pSeries2->SetCurveType (BCGPChartFormatSeries::CCT_SPLINE_HERMITE);
- //系列1添加数据
- pSeries1->AddDataPoint(0.);
- pSeries1->AddDataPoint(2.);
- pSeries1->AddDataPoint(5.);
- pSeries1->AddDataPoint(3.);
- pSeries1->AddDataPoint(3.5);
- pSeries1->AddDataPoint(5.);
- pSeries1->AddDataPoint(8.);
- pSeries1->AddDataPoint(7.);
- //系列2添加数据
- pSeries2->AddDataPoint(2.);
- pSeries2->AddDataPoint(4.);
- pSeries2->AddDataPoint(6.);
- pSeries2->AddDataPoint(7.);
- pSeries2->AddDataPoint(6.);
- pSeries2->AddDataPoint(3.);
- pSeries2->AddDataPoint(1.);
- pSeries2->AddDataPoint(0.);
- //数据系列显示阴影;
- pChart->SetSeriesShadow(true);
- //添加添加到布局管理器统一管理布局;
- if (GetLayout() == NULL)
- return lRes;
- CBCGPStaticLayout* pLayout = (CBCGPStaticLayout*)GetLayout();
- if (pLayout == NULL)
- return lRes;
- pLayout->AddAnchor(m_wndChart.GetDlgCtrlID(), CBCGPStaticLayout::e_MoveTypeNone, CBCGPStaticLayout::e_SizeTypeBoth);
- return lRes;
- }
复制代码 具体可以下载例程编译,操作例程查看效果与细节。
MFC扩展库可事先本博客下载BCGP,进行环境搭建。
源代码下载地址:
|