MFC扩展编程实例散点图创建与XY轴位置设置
当前例程实现散点图表的创建及图表XY轴的设置。
效果如下图。
MFC扩展编程实例散点图创建与XY轴位置设置
散点图表外观主要有数据标识点形式的设置,
例程在创建初始化时使用的是三角形的,要以自行设置为其他形状。
标签显示角度也可进行设置,例程使用45度显示。
图表XY轴显示的位置也有多种设置,其中一种是自定义的位置,可以根据自己需要来设置。
下面是例程实现过程介绍。
先创建基于MFC扩展库工程,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();//获取图表指针;
- pChart->SetChartType(BCGPChartLine, BCGP_CT_SIMPLE);//
- pChart->SetChartTitle(_T("散点图"));//设置图表标题;
- pChart->SetSeriesShadow(true);//数据系列显示阴影;
- pChart->SetLegendPosition(BCGPChartLayout::LP_NONE);//隐藏图例;
- pChart->ShowDataMarkers(TRUE, 7, BCGPChartMarkerOptions::MS_TRIANGLE);//数据标识点显示;
- pChart->ShowDataLabels(TRUE, TRUE, TRUE, 45);//45度显示数据标签;
- //创建数据系列,添加数据;
- #define MAX_POINT_SERIES 4
- #define MAX_POINTS_IN_SERIES 6
- #define MIN_Y_VAL 0.
- #define MAX_Y_VAL 20.
- #define X_RANGE_PER_SERIES 3
- #define X_VALUE_GRADE 100.
- for (int i = 0; i < MAX_POINT_SERIES; i++)
- {
- int nXRangeLow = (int)(i - MAX_POINT_SERIES / 2.) * X_RANGE_PER_SERIES;
- int nXRangeHigh = nXRangeLow + X_RANGE_PER_SERIES;
- for (int j = 0; j < MAX_POINTS_IN_SERIES; j++)
- {
- double dblX = Rand(nXRangeLow / X_VALUE_GRADE, nXRangeHigh / X_VALUE_GRADE);
- double dblY = Rand(MIN_Y_VAL, MAX_Y_VAL);
- pChart->AddChartDataYX(dblY, dblX, i);
- }
- CBCGPChartSeries* pSeries = pChart->GetSeries(i);
- ASSERT_VALID(pSeries);
- BCGPChartFormatSeries style = pSeries->GetSeriesFormat();
- style.m_curveType = BCGPChartFormatSeries::CCT_NO_LINE;
- pSeries->SetSeriesFormat(style);
- pSeries->SetDataLabelDataFormat(_T("%.2f"));
- }
- //xy轴设置;
- CBCGPChartAxis* pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
- ASSERT_VALID(pYAxis);
- pYAxis->m_majorTickMarkType = CBCGPChartAxis::TMT_CROSS;
- pYAxis->m_minorTickMarkType = CBCGPChartAxis::TMT_CROSS;
- pYAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;
- //
- CBCGPChartAxis* pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
- ASSERT_VALID(pXAxis);
- pXAxis->m_minorTickMarkType = CBCGPChartAxis::TMT_OUTSIDE;
- pXAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;
- //添加添加到布局管理器统一管理布局;
- 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;
- }
复制代码
初始化图表后,就可以编译运行例程了,最后还可以添加一按钮控件,用于设置XY轴的显示位置。
- void CMy123View::OnBnClickedButton1()
- {
- static int nAxisType=0;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- CBCGPChartAxis* pYAxis = pChart->GetChartAxis(BCGP_CHART_Y_PRIMARY_AXIS);
- ASSERT_VALID(pYAxis);
- CBCGPChartAxis* pXAxis = pChart->GetChartAxis(BCGP_CHART_X_PRIMARY_AXIS);
- ASSERT_VALID(pXAxis);
- //
- switch (nAxisType)
- {
- case 0:
- pYAxis->m_crossType = CBCGPChartAxis::CT_AUTO;
- pXAxis->m_crossType = CBCGPChartAxis::CT_AUTO;
- break;
- case 1:
- pYAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;
- pXAxis->m_crossType = CBCGPChartAxis::CT_MAXIMUM_AXIS_VALUE;
- break;
- case 2:
- pYAxis->m_crossType = CBCGPChartAxis::CT_MINIMUM_AXIS_VALUE;
- pXAxis->m_crossType = CBCGPChartAxis::CT_MINIMUM_AXIS_VALUE;
- break;
- case 3:
- pYAxis->m_crossType = CBCGPChartAxis::CT_AXIS_VALUE;
- pYAxis->m_dblCrossOtherAxisValue = 0.02;
- pXAxis->m_crossType = CBCGPChartAxis::CT_AXIS_VALUE;
- pXAxis->m_dblCrossOtherAxisValue = 7.0;
- break;
- }
- pChart->SetDirty(TRUE,TRUE);
- nAxisType++;
- nAxisType= nAxisType>3?0:nAxisType;
- }
复制代码
例程用到的MFC扩展库可以网站下载。
例程源代码下载地址:
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
|