QQ登录

只需一步,快速开始

MFC扩展编程实例2D3D面积范围图表创建与标签设置

[ 复制链接 ]
MFC扩展编程实例2D3D面积范围图表创建与标签设置

当前例程实现二维三维的面积范围图表创建及标签属性设置,
效果如下图:

MFC扩展编程实例2D3D面积范围图表创建与标签设置

MFC扩展编程实例2D3D面积范围图表创建与标签设置


例程初始运行时显示的是二维的面积范围图表,可以点击界面的图表类型切换按钮,
三维二维来回切换显示。
三维图表还可以通过界面的旋转控件来操作。
图表的标签与显示角度可以通过按钮来显示与设置。

下面是例程实现过程。
首先创建基于单文档的工程,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);
       
  1. void CMy123View::RotateChart(CBCGPRotationObject::RotationElement hit, double xDelta, double yDelta, double persperctiveDelta)
  2. {
  3.         CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
  4.         if (pChart == NULL)
  5.                 return;
  6.         ASSERT_VALID(pChart);

  7.         CBCGPChartDiagram3D* pDiagram3D = pChart->GetDiagram3D();
  8.         if (pDiagram3D == NULL)
  9.                 return;

  10.         double xRotation = pDiagram3D->GetXRotation();
  11.         double yRotation = pDiagram3D->GetYRotation();
  12.         double dblPerspectivePercent = pDiagram3D->GetPerspectivePercent();
  13.         switch (hit)
  14.         {
  15.         case CBCGPRotationObject::BCGP_ROTATION_UP:
  16.                 yRotation += yDelta;
  17.                 break;

  18.         case CBCGPRotationObject::BCGP_ROTATION_DOWN:
  19.                 yRotation -= yDelta;
  20.                 break;

  21.         case CBCGPRotationObject::BCGP_ROTATION_LEFT:
  22.                 xRotation -= xDelta;
  23.                 break;

  24.         case CBCGPRotationObject::BCGP_ROTATION_RIGHT:
  25.                 xRotation += xDelta;
  26.                 break;

  27.         case CBCGPRotationObject::BCGP_ROTATION_RESET:
  28.                 pDiagram3D->Reset(TRUE);
  29.                 return;

  30.         case CBCGPRotationObject::BCGP_ROTATION_NARROW_FIELD_OF_VIEW:
  31.                 dblPerspectivePercent -= persperctiveDelta;
  32.                 break;

  33.         case CBCGPRotationObject::BCGP_ROTATION_WIDEN_FIELD_OF_VIEW:
  34.                 dblPerspectivePercent += persperctiveDelta;
  35.                 break;
  36.         }
  37.         pDiagram3D->SetPosition(xRotation, yRotation, dblPerspectivePercent);
  38.         pChart->Redraw();
  39. }
复制代码

添加虚函数Create,初始化变量。
  1. inline double Rand (double dblStart, double dblFinish)
  2. {
  3.         double minVal = min(dblStart, dblFinish);
  4.         double maxVal = max(dblStart, dblFinish);
  5.         return (maxVal - minVal) * (double)rand() / (RAND_MAX + 1) + minVal;
  6. }
  7. BOOL CMy123View::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
  8. {
  9.         BOOL bRst = CBCGPFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);;
  10.         //旋转图表控件初始化;
  11.         m_wndRotate.SubclassDlgItem(IDC_ROTATE,this);
  12.         m_wndRotate.GetRotationObject()->SetAutorepeatMode(100);
  13.         m_wndRotate.GetRotationObject()->SetColorTheme(CBCGPRotationObject::BCGP_COLOR_THEME_VISUAL_MANAGER);
  14.         m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_CLOCKWISE, FALSE);
  15.         m_wndRotate.GetRotationObject()->EnablePart(CBCGPRotationObject::BCGP_ROTATION_COUNTER_CLOCKWISE, FALSE);
  16.         m_wndRotate.GetRotationObject()->EnableFlatIcons();
  17.         //图表初始化;
  18.         m_wndChart.SubclassDlgItem(IDC_CHART,this);       
  19.         CBCGPInfoTipOptions infoTipOptions;
  20.         infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;
  21.         m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);//显示提示文本;
  22.         CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
  23.         BCGPChartCategory style = BCGPChartArea;
  24.         BCGPChartType type = BCGP_CT_RANGE;  
  25.         pChart->SetChartType(style,type);//图表类型设置;
  26.         pChart->SetChartTitle(_T("面积范围图表"));//设置图表标题;
  27.         pChart->SetSeriesShadow(true);//数据系列显示阴影;
  28.         pChart->ShowDataMarkers(true, 5, BCGPChartMarkerOptions::MS_CIRCLE);//o数据标志点设置;
  29. //        pChart->ShowDataLabels(true, TRUE, TRUE, 45);//数据标签默认不显示;
  30.         pChart->SetThemeOpacity(70);//透明度设置;
  31.         pChart->SetCurveType(BCGPChartFormatSeries::CCT_SPLINE);//设置数据系列曲线样式;
  32.         //创建数据系列,添加数据;
  33.         CBCGPChartSeries* pSeries1 = pChart->CreateSeries(_T("第一年"));
  34.         CBCGPChartSeries* pSeries2 = pChart->CreateSeries(_T("第二年"));
  35.         CBCGPChartSeries* pSeries3 = pChart->CreateSeries(_T("第三年"));
  36.         COleDateTime today = COleDateTime::GetCurrentTime();

  37.         double arTemp[] = { 5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7 };
  38.         for (int nMonth = 1; nMonth <= 12; nMonth++)
  39.         {
  40.                 COleDateTime date (today.GetYear(), nMonth, 1, 0, 0, 0);
  41.                 CString strMonth = date.Format(_T("%b"));
  42.                 pChart->AddChartDataYY1(strMonth, arTemp[nMonth - 1], Rand(2, 5), 0);
  43.                 pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 1);
  44.                 pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 2);
  45.         }
  46.         //添加添加到布局管理器统一管理布局;
  47.         if (GetLayout() == NULL)
  48.                 return bRst;
  49.         CBCGPStaticLayout* pLayout = (CBCGPStaticLayout*)GetLayout();
  50.         if (pLayout == NULL)
  51.                 return bRst;
  52.         pLayout->AddAnchor(m_wndChart.GetDlgCtrlID(), CBCGPStaticLayout::e_MoveTypeNone, CBCGPStaticLayout::e_SizeTypeBoth);
  53.         //
  54.         return bRst;
  55. }
复制代码
添加旋转控件的点击函数,旋转图表。
  1. void CMy123View::OnRotate()
  2. {
  3.         RotateChart(m_wndRotate.GetRotationObject()->GetClicked());
  4. }
复制代码
最后添加两个按钮实现对图表的设置。
  1. oid CMy123View::OnBnClickedButton1()
  2. {
  3.         static bool b3D = true;
  4.         CBCGPChartVisualObject* pChart = m_wndChart.GetChart();//获取图表指针;
  5.         BCGPChartCategory style = b3D?BCGPChartArea3D:BCGPChartArea;
  6.         BCGPChartType type = BCGP_CT_RANGE;  
  7.         pChart->SetChartType(style,type);//图表类型设置;
  8.         BCGPChartFormatSeries::ChartCurveType curve = b3D?BCGPChartFormatSeries::CCT_LINE:BCGPChartFormatSeries::CCT_SPLINE;
  9.     pChart->SetCurveType(curve);//图表曲线样式;
  10.         if(b3D)//设置3D图表背景墙相关属性;
  11.         {
  12.                 CBCGPChartDiagram3D* pDiagram3D = pChart->GetDiagram3D();
  13.                 ASSERT(pDiagram3D != NULL);
  14.                 pDiagram3D->SetDepthScalePercent(2);
  15.                 pDiagram3D->SetFrontDistancePercent(0.5);
  16.                 pDiagram3D->SetHeightScalePercent(1);
  17.                 DWORD dwoFlags = CBCGPChartDiagram3D::DWO_OUTLINE_ALL|CBCGPChartDiagram3D::DWO_DRAW_ALL_WALLS | CBCGPChartDiagram3D::DWO_DRAW_FLOOR;
  18.                 pDiagram3D->SetDrawWallOptions((CBCGPChartDiagram3D::DrawWallOptions)dwoFlags);
  19.                 pDiagram3D->SetThickWallsAndFloor(true);
  20.                
  21.         }
  22.         pChart->Redraw();
  23.         b3D = !b3D;
  24. }


  25. void CMy123View::OnBnClickedButton2()
  26. {
  27.         static int nIndex=0;
  28.         bool bShow = nIndex==8?false:true;
  29.         double dbDegree = 45*nIndex;
  30.         CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
  31.         ASSERT_VALID(pChart);
  32.         pChart->ShowDataLabels(bShow, TRUE, FALSE, dbDegree);
  33.         pChart->Redraw();
  34.         nIndex++;
  35.         nIndex=nIndex>8?0:nIndex;
  36. }
复制代码
例程使用到了MFC扩展库,库源代码可以在网站搜索下载。
例程源代码下载地址:
游客,为过滤非法行为,全站隐藏资源仅对充值会员开放进入升级

  

如果您认可,可联系功能定制!

  

如果您着急,充值会员可直接联系发您资料!

  

QQ联系我

微信扫扫联系我

  

  

上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例

  

经历1年的编程与录制点击进入查看



回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索