工控编程吧
标题:
MFC扩展编程实例圆环嵌套图表创建与标签位置设置
[打印本页]
作者:
qq263946146
时间:
2020-2-13 15:26
标题:
MFC扩展编程实例圆环嵌套图表创建与标签位置设置
MFC扩展编程实例圆环嵌套图表创建与标签位置设置
当前例程创建一个圆环的嵌套类型的图表,并可以点击界面按钮设置其标签位置,效果如下图。
(, 下载次数: 2)
上传
点击文件名下载附件
例程是基于一套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);
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
pChart->SetChartType(BCGPChartDoughnutNested);//圆环嵌套图表类型;
pChart->SetThemeOpacity(70);//图表透明度;
int nTotalYears = 3;
CString sName;
//创建数据系列,添加数据;
for (int nYear = 0; nYear < nTotalYears; nYear++)
{
pChart->AddChartData(_T("产品 A"), Rand(10000, 50000), nYear);
pChart->AddChartData(_T("产品 B"), Rand(10000, 50000), nYear);
pChart->AddChartData(_T("产品 C"), Rand(10000, 50000), nYear);
pChart->AddChartData(_T("产品 D"), Rand(10000, 50000), nYear);
pChart->AddChartData(_T("产品 E"), Rand(10000, 50000), nYear);
pChart->AddChartData(_T("产品 F"), Rand(10000, 50000), nYear);
pChart->AddChartData(_T("产品 G"), Rand(10000, 50000), nYear);
sName.Format(_T("第 %d年"),nTotalYears - nYear - 1);
pChart->SetSeriesName(sName, nYear);
}
//添加添加到布局管理器统一管理布局;
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;
}
复制代码
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;
}
复制代码
这样就简单快速的创建了圆环嵌套图表,关键代码是设置图表的类型。
pChart->SetChartType(BCGPChartDoughnutNested);//圆环嵌套图表类型;
最后是添加按钮控件,调车图表标签属性。
void CMy123View::OnBnClickedButton1()
{
static int nIndex=0;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
ASSERT_VALID(pChart);
BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
dataLabelOptions.m_position = (BCGPChartDataLabelOptions::LabelPosition)nIndex;//标签位置;
dataLabelOptions.m_bUnderlineDataLabel = TRUE;
dataLabelOptions.m_bDrawDataLabelBorder = \
dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_DEFAULT_POS \
&& dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_OUTSIDE_END;
dataLabelOptions.m_content = BCGPChartDataLabelOptions::LC_PERCENTAGE;
pChart->SetDataLabelsOptions(dataLabelOptions);
//
for (int i = 0; i < pChart->GetSeriesCount(); i++)
{
CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
ASSERT_VALID(pSeries);
pSeries->SetDefaultFillGradientType(CBCGPBrush::BCGP_GRADIENT_BEVEL);//设置渐变模式;
pSeries->SetTileCaption(CBCGPChartPieSeries::CHART_TILE_POSITION_TOP);
CBCGPChartDoughnutSeries* pDoughnutSeries = DYNAMIC_DOWNCAST(CBCGPChartDoughnutSeries, pSeries);
ASSERT_VALID(pDoughnutSeries);
pDoughnutSeries->SetDoughnutPercent(30);
}
nIndex++;
if(nIndex>4)
pChart->ShowDataLabels(false);//隐藏数据标签;
else
pChart->ShowDataLabels(true);
pChart->Redraw();
//
nIndex = nIndex>4?0:nIndex;
}
复制代码
例程源代码下载地址:
[weixinlianxi]1[/weixinlianxi]
[MFC408]1[/MFC408]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4