工控编程吧
标题:
MFC扩展编程实例2D3D面积范围图表创建与标签设置
[打印本页]
作者:
qq263946146
时间:
2020-2-18 15:00
标题:
MFC扩展编程实例2D3D面积范围图表创建与标签设置
MFC扩展编程实例2D3D面积范围图表创建与标签设置
当前例程实现二维三维的面积范围图表创建及标签属性设置,
效果如下图:
(, 下载次数: 1)
上传
点击文件名下载附件
例程初始运行时显示的是二维的面积范围图表,可以点击界面的图表类型切换按钮,
三维二维来回切换显示。
三维图表还可以通过界面的旋转控件来操作。
图表的标签与显示角度可以通过按钮来显示与设置。
下面是例程实现过程。
首先创建基于单文档的工程,class CMy123View : public CBCGPFormView。
在默认对话资源添加图片控件,ID设置为IDC_ROTATE,IDC_CHART用于旋转图表与显示图表。
在初始类添加自定义变量与函数。
CBCGPRotationCtrl m_wndRotate;
CBCGPChartCtrl m_wndChart;
void RotateChart(CBCGPRotationObject::RotationElement hit, double xDelta=10., double yDelta=10., double persperctiveDelta=0.1);
void CMy123View::RotateChart(CBCGPRotationObject::RotationElement hit, double xDelta, double yDelta, double persperctiveDelta)
{
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
if (pChart == NULL)
return;
ASSERT_VALID(pChart);
CBCGPChartDiagram3D* pDiagram3D = pChart->GetDiagram3D();
if (pDiagram3D == NULL)
return;
double xRotation = pDiagram3D->GetXRotation();
double yRotation = pDiagram3D->GetYRotation();
double dblPerspectivePercent = pDiagram3D->GetPerspectivePercent();
switch (hit)
{
case CBCGPRotationObject::BCGP_ROTATION_UP:
yRotation += yDelta;
break;
case CBCGPRotationObject::BCGP_ROTATION_DOWN:
yRotation -= yDelta;
break;
case CBCGPRotationObject::BCGP_ROTATION_LEFT:
xRotation -= xDelta;
break;
case CBCGPRotationObject::BCGP_ROTATION_RIGHT:
xRotation += xDelta;
break;
case CBCGPRotationObject::BCGP_ROTATION_RESET:
pDiagram3D->Reset(TRUE);
return;
case CBCGPRotationObject::BCGP_ROTATION_NARROW_FIELD_OF_VIEW:
dblPerspectivePercent -= persperctiveDelta;
break;
case CBCGPRotationObject::BCGP_ROTATION_WIDEN_FIELD_OF_VIEW:
dblPerspectivePercent += persperctiveDelta;
break;
}
pDiagram3D->SetPosition(xRotation, yRotation, dblPerspectivePercent);
pChart->Redraw();
}
复制代码
添加虚函数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_wndRotate.SubclassDlgItem(IDC_ROTATE,this);
m_wndRotate.GetRotationObject()->SetAutorepeatMode(100);
m_wndRotate.GetRotationObject()->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);
m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);
m_wndRotate.GetRotationObject()->EnableFlatIcons();
//图表初始化;
m_wndChart.SubclassDlgItem(IDC_CHART,this);
CBCGPInfoTipOptions infoTipOptions;
infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;
m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);//显示提示文本;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
BCGPChartCategory style = BCGPChartArea;
BCGPChartType type = BCGP_CT_RANGE;
pChart->SetChartType(style,type);//图表类型设置;
pChart->SetChartTitle(_T("面积范围图表"));//设置图表标题;
pChart->SetSeriesShadow(true);//数据系列显示阴影;
pChart->ShowDataMarkers(true, 5, BCGPChartMarkerOptions::MS_CIRCLE);//o数据标志点设置;
// pChart->ShowDataLabels(true, TRUE, TRUE, 45);//数据标签默认不显示;
pChart->SetThemeOpacity(70);//透明度设置;
pChart->SetCurveType(BCGPChartFormatSeries::CCT_SPLINE);//设置数据系列曲线样式;
//创建数据系列,添加数据;
CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("第一年"));
CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("第二年"));
CBCGPChartSeries* pSeries3 = pChart->CreateSeries(_T("第三年"));
COleDateTime today = COleDateTime::GetCurrentTime();
double arTemp[] = { 5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7 };
for (int nMonth = 1; nMonth <= 12; nMonth++)
{
COleDateTime date (today.GetYear(), nMonth, 1, 0, 0, 0);
CString strMonth = date.Format(_T("%b"));
pChart->AddChartDataYY1(strMonth, arTemp[nMonth - 1], Rand(2, 5), 0);
pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 1);
pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 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;
}
复制代码
添加旋转控件的点击函数,旋转图表。
void CMy123View::OnRotate()
{
RotateChart(m_wndRotate.GetRotationObject()->GetClicked());
}
复制代码
最后添加两个按钮实现对图表的设置。
oid CMy123View::OnBnClickedButton1()
{
static bool b3D = true;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
BCGPChartCategory style = b3D?BCGPChartArea3D:BCGPChartArea;
BCGPChartType type = BCGP_CT_RANGE;
pChart->SetChartType(style,type);//图表类型设置;
BCGPChartFormatSeries::ChartCurveType curve = b3D?BCGPChartFormatSeries::CCT_LINE:BCGPChartFormatSeries::CCT_SPLINE;
pChart->SetCurveType(curve);//图表曲线样式;
if(b3D)//设置3D图表背景墙相关属性;
{
CBCGPChartDiagram3D* pDiagram3D = pChart->GetDiagram3D();
ASSERT(pDiagram3D != NULL);
pDiagram3D->SetDepthScalePercent(2);
pDiagram3D->SetFrontDistancePercent(0.5);
pDiagram3D->SetHeightScalePercent(1);
DWORD dwoFlags = CBCGPChartDiagram3D::DWO_OUTLINE_ALL|CBCGPChartDiagram3D::DWO_DRAW_ALL_WALLS | CBCGPChartDiagram3D::DWO_DRAW_FLOOR;
pDiagram3D->SetDrawWallOptions((CBCGPChartDiagram3D::DrawWallOptions)dwoFlags);
pDiagram3D->SetThickWallsAndFloor(true);
}
pChart->Redraw();
b3D = !b3D;
}
void CMy123View::OnBnClickedButton2()
{
static int nIndex=0;
bool bShow = nIndex==8?false:true;
double dbDegree = 45*nIndex;
CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
ASSERT_VALID(pChart);
pChart->ShowDataLabels(bShow, TRUE, FALSE, dbDegree);
pChart->Redraw();
nIndex++;
nIndex=nIndex>8?0:nIndex;
}
复制代码
例程使用到了MFC扩展库,库源代码可以在网站搜索下载。
例程源代码下载地址:
[weixinlianxi]1[/weixinlianxi]
[MFC408]1[/MFC408]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4