MFC扩展编程实例3D簇状柱形图设置标签位置与渐变样式
例程实现三维的簇状柱形图创建,以及设置标签位置与渐变样式。
界面如下:
17.MFC扩展编程实例3D簇状柱形图设置标签位置与渐变样式
图表中创建有四个数据系列,四个一簇以三维的柱状类型显示。
同样也可以设置图表数据标签位置与渐变样式。
下面是创建过程与关键代码。
先创建基于class CMy123View : public CBCGPFormView的单文档工程。
添加变量CBCGPChartCtrl m_wndChart;与虚函数Create。
在函数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);//子类化图表;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- pChart->SetChartTitle(_T("3D簇状柱形图"));//图表标题设置;
- pChart->SetThemeOpacity(70);//图表主题透明度设置;
- BCGPChartCategory category = BCGPChartColumn3D;//图表种类,F12查看全部;
- BCGPChartType type = BCGP_CT_SIMPLE;//图表类型,按F12查看全部;
- pChart->SetChartType(category, type);//设置图表类别;
- pChart->GetDiagram3D()->SetGrouped(TRUE, FALSE);//设置为簇状类型图表;
- //设置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);
- //创建4个数据系列;
- CBCGPChartBarSeries* pBarSeries1 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("能源")));
- CBCGPChartBarSeries* pBarSeries2 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("税收")));
- CBCGPChartBarSeries* pBarSeries3 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("维护")));
- CBCGPChartBarSeries* pBarSeries4 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, 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);
- pBarSeries1->AddDataPoint(sYear, rand()%20+1);
- }
- pBarSeries2->AddDataPoint(10);
- pBarSeries2->AddDataPoint(12);
- pBarSeries2->AddDataPoint(15);
- pBarSeries2->AddDataPoint(17);
- pBarSeries2->AddDataPoint(12);
- pBarSeries3->AddDataPoint(5);
- pBarSeries3->AddDataPoint(7);
- pBarSeries3->AddDataPoint(11);
- pBarSeries3->AddDataPoint(14);
- pBarSeries3->AddDataPoint(19);
- pBarSeries4->AddDataPoint(2);
- pBarSeries4->AddDataPoint(3);
- pBarSeries4->AddDataPoint(5);
- pBarSeries4->AddDataPoint(3);
- pBarSeries4->AddDataPoint(2);
- //添加添加到布局管理器统一管理布局;
- 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 = BCGPChartColumn3D;//图表种类,F12查看全部;
BCGPChartType type = BCGP_CT_SIMPLE;//图表类型,按F12查看全部;
pChart->SetChartType(category, type);//设置图表类别;
可以在构造函数中调用EnableLayout();以开启布局管理器。
图表子类化时有用到一控件ID,所以还得添加一图片控件,修改其ID为ID_CHART.
最后就是两按钮的实现,可以选择是否设置数据标签显示位置与渐变样式。
- void CMy123View::OnBnClickedButton1()//图表修改数据标签位置;
- {
- static int nPos=0;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- //数据标签位置有如下几种设置;
- //enum LabelPosition
- //{
- // LP_DEFAULT_POS,//0
- // LP_CENTER,//1
- // LP_INSIDE_END,
- // LP_INSIDE_BASE,
- // LP_OUTSIDE_END
- //};
- if(nPos == -1)
- pChart->ShowDataLabels(false);
- else
- {
- pChart->ShowDataLabels(true);
- BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
- dataLabelOptions.m_position = (BCGPChartDataLabelOptions::LabelPosition)nPos;
- pChart->SetDataLabelsOptions(dataLabelOptions);
- }
- pChart->Redraw();
- //
- nPos++;
- nPos=nPos>4?-1:nPos;
- }
- void CMy123View::OnBnClickedButton2()
- {
- //渐变为枚举类型,有以下供选择使用;
- //enum BCGP_GRADIENT_TYPE
- //{
- // BCGP_GRADIENT_TYPE_FIRST = 0,
- // BCGP_NO_GRADIENT = BCGP_GRADIENT_TYPE_FIRST,
- // BCGP_GRADIENT_HORIZONTAL = 1,
- // BCGP_GRADIENT_VERTICAL = 2,
- // BCGP_GRADIENT_DIAGONAL_LEFT = 3,
- // BCGP_GRADIENT_DIAGONAL_RIGHT = 4,
- // BCGP_GRADIENT_CENTER_HORIZONTAL = 5,
- // BCGP_GRADIENT_CENTER_VERTICAL = 6,
- // BCGP_GRADIENT_RADIAL_TOP = 7,
- // BCGP_GRADIENT_RADIAL_CENTER = 8,
- // BCGP_GRADIENT_RADIAL_BOTTOM = 9,
- // BCGP_GRADIENT_RADIAL_LEFT = 10,
- // BCGP_GRADIENT_RADIAL_RIGHT = 11,
- // BCGP_GRADIENT_RADIAL_TOP_LEFT = 12,
- // BCGP_GRADIENT_RADIAL_TOP_RIGHT = 13,
- // BCGP_GRADIENT_RADIAL_BOTTOM_LEFT = 14,
- // BCGP_GRADIENT_RADIAL_BOTTOM_RIGHT = 15,
- // BCGP_GRADIENT_BEVEL = 16,
- // BCGP_GRADIENT_PIPE_VERTICAL = 17,
- // BCGP_GRADIENT_PIPE_HORIZONTAL = 18,
- // BCGP_GRADIENT_TYPE_LAST = BCGP_GRADIENT_PIPE_HORIZONTAL
- //};
- static int nType=-1;//-1为恢复缺省渐变,0为不使用渐变;
- CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
- CBCGPBrush::BCGP_GRADIENT_TYPE type = (CBCGPBrush::BCGP_GRADIENT_TYPE)nType;
- for (int i = 0; i < pChart->GetSeriesCount(); i++)
- {
- CBCGPChartSeries* pSeries = pChart->GetSeries(i);
- pSeries->SetDefaultFillGradientType(type);
- }
- pChart->SetDirty(true,true);
- //
- nType++;
- nType=nType>18?-1:nType;
- }
复制代码 例程使用了MFC扩展,可以网站搜索下载。
例程源代码下载:
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|