16.MFC扩展编程实例2D极差柱形图设置渐变样式
例程实现了二维极差柱形图表创建,图表数据标签显示隐藏,位置设置,与数据系列渐变样式设置功能。
界面如下:
16.MFC扩展编程实例2D极差柱形图设置渐变样式
例程创建两个数据系列,每个数据系列数据以极差形式显示。
跟前面例程一样,附带也实现了数据标签位置设置与渐变设置。
下面是关键代码与实现过程。
首先创建基于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();
- BCGPChartCategory category = BCGPChartColumn;//图表种类,F12查看全部;
- BCGPChartType type = BCGP_CT_RANGE;//图表类型,按F12查看全部;
- pChart->SetChartType(category, type);//设置图表类别;
- pChart->SetChartTitle(_T("2D极差柱形图"));
- CBCGPChartBarSeries* pSeries1 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("能源")));
- CBCGPChartBarSeries* pSeries2 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("税收")));
- pSeries1->SetCustomOffsetPercent(100);
- pSeries2->SetCustomOffsetPercent(100);
- pSeries1->SetCustomSizePercent(50);
- pSeries2->SetCustomSizePercent(50);
- pChart->AddChartDataYXY1(1, 2, 2, 0);
- pChart->AddChartDataYXY1(2, 3, 2, 0);
- pChart->AddChartDataYXY1(6, 8, 2, 0);
- pChart->AddChartDataYXY1(8, 7, 2, 0);
- pChart->AddChartDataYXY1(8, 1, 2, 0);
- pChart->AddChartDataYXY1(8, 4, 6, 0);
- pChart->AddChartDataYXY1(14, 1, 3, 0);
- pChart->AddChartDataYXY1(15, 8, 1, 0);
- pChart->AddChartDataYXY1(6, 4, 2, 1);
- pChart->AddChartDataYXY1(7, 2, 1, 1);
- pChart->AddChartDataYXY1(11, 3, 2, 1);
- pChart->AddChartDataYXY1(13, 7, 2, 1);
- pChart->AddChartDataYXY1(12, 1, 2, 1);
- pChart->AddChartDataYXY1(14, 5, 4, 1);
- pChart->AddChartDataYXY1(17, 1, 3, 1);
- pChart->AddChartDataYXY1(10, 8, 1, 1);
- //添加添加到布局管理器统一管理布局;
- 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;
- }
复制代码 2D极差柱形图创建对应代码如下,通过函数SetChartType来设置。
BCGPChartCategory category = BCGPChartColumn;//图表种类,F12查看全部;
BCGPChartType type = BCGP_CT_RANGE;//图表类型,按F12查看全部;
pChart->SetChartType(category, type);//设置图表类别;
pChart->SetChartTitle(_T("2D极差柱形图"));
图表创建后,就可以根据需要来选择设置数据标签与渐变样式 。
对应代码在前面例程有使用,如下:
- 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->Redraw();
- //
- nType++;
- nType=nType>18?-1:nType;
- }
复制代码 简单一两步就实现了极差图创建。
例程有用到MFC扩展库,可以网站搜索下载使用。
例程源代码下载:
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|