MFC扩展编程实例2D3D漏斗图表标签与颈部高设置
当前例程实现二维与三维的漏斗图表创建,设置标签显示位置,
颈部调度设置等,效果如下图。
例程运行如上图。
点击标签设置,可以让标签在图表左中右位置显示。
点击颈高按钮,可以设置漏斗的颈高度。
点击最后一个按钮,可以切换二维三维图表。
下面是例程实现过程介绍。
先创建基于MFC扩展库的单文档工程,class CMy123View : public CBCGPFormView。
在默认的对话框资源上添加一图片,修改其ID为IDC_CHART,用于显示图表。
在视窗类中添加图表变量CBCGPChartCtrl m_wndChart;
与虚函数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_wndChart.SubclassDlgItem(IDC_CHART,this);
- CBCGPInfoTipOptions infoTipOptions;
- infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;
- m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);//显示提示文本;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
- //BCGPChartFunnel3D,BCGPChartFunnel//二维,三维漏斗图表;
- pChart->SetChartType(BCGPChartFunnel3D);//
- pChart->SetChartTitle(_T("漏斗图表"));//设置图表标题;
- pChart->SetSeriesShadow(true);//数据系列显示阴影;
- //创建一数据系列,添加数据;
- pChart->AddChartData(_T("工控编程吧"), 6700);
- pChart->AddChartData(_T("头条"), 4500);
- pChart->AddChartData(_T("百度"), 2200);
- pChart->AddChartData(_T("微信"), 1500);
- pChart->AddChartData(_T("微博"), 1400);
- pChart->AddChartData(_T("阿里"), 1350);
- pChart->AddChartData(_T("其他"), 1700);
- //添加添加到布局管理器统一管理布局;
- 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 bool bShowLabel=true;//是否显示标签;
- static int nLabelPos=0;//标签位置;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- pChart->ShowDataLabels(bShowLabel);//显示标签;
- if (bShowLabel)
- {
- BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
- switch (nLabelPos)
- {
- case 0:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_DEFAULT_POS;
- break;
- case 1:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
- dataLabelOptions.m_dblAngle = -90;
- break;
- case 2:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_OUTSIDE_END;
- dataLabelOptions.m_dblAngle = 90;
- break;
- case 3:
- dataLabelOptions.m_position = BCGPChartDataLabelOptions::LP_CENTER;
- break;
- }
- pChart->SetDataLabelsOptions(dataLabelOptions);
- }
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartPyramidSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPyramidSeries, pChart->GetSeries(i));
- ASSERT_VALID(pSeries);
- pSeries->m_bDataLabelsInColumns = true;
- pSeries->SetGap(25);//金字塔每层间隔;
- }
- pChart->SetThemeOpacity(70);//设置透明度;
- pChart->Redraw();
- //
- nLabelPos++;
- nLabelPos = nLabelPos>3?0:nLabelPos;
- }
- void CMy123View::OnBnClickedButton2()
- {
- static bool b3D=false;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- BCGPChartCategory type = b3D?BCGPChartFunnel3D:BCGPChartFunnel;
- pChart->SetChartType(type);
- pChart->Redraw();
- b3D = !b3D;
- }
- void CMy123View::OnBnClickedButton3()
- {
- static bool bByPercent=false;
- static int nIndex=1;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- ASSERT_VALID(pChart);
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartFunnelSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartFunnelSeries, pChart->GetSeries(i));
- ASSERT_VALID(pSeries);
- if (bByPercent)
- {
- int nPercent = nIndex*20;
- pSeries->SetNeckHeightInPercents(nPercent);
- bByPercent = nIndex==5?false:true;
- }
- else
- {
- double dbHeightValue = nIndex*1000;
- pSeries->SetNeckHeightInChartValues(dbHeightValue);
- bByPercent = nIndex==5?true:false;
- }
- }
- pChart->SetDirty(TRUE,TRUE);
- //
- nIndex++;
- nIndex = nIndex>5?1:nIndex;
- }
复制代码 例程使用到的MFC扩展库可以网站搜索下载。
例程源代码下载地址:
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
|