QQ登录

只需一步,快速开始

上位机MFC扩展编程实例图表组合创建与显示

[ 复制链接 ]
上位机MFC扩展编程实例图表组合创建与显示


在前面例程中图表的创建可以通过自自定变量CBCGPChartCtrl m_wndChart直接实现。
但在多个图表统一管理时,一个一个的创建图表变显得效率底下。
当前例程实现通过容器来管理多图表功能,界面如下:
2020-01-08_113343.jpg
例程创建了三个图表,每个图表类型不同,相互独立但又创建在一容器内。
容器统一管理图表的布局,下面是例程实现过程的介绍。

先是添加容器与图表变量,并添加虚函数,在函数内创建变量。
        CBCGPVisualContainerCtrl            m_wndContainer;
        CBCGPChartVisualObject*                        m_pChart1;
        CBCGPChartVisualObject*                        m_pChart2;
        CBCGPChartVisualObject*                m_pChart3;

  1. <div><div>BOOL CMy123View::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)</div><div>{</div><div><span style="white-space:pre">        </span>BOOL bRst = CBCGPFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);;</div><div>
  2. </div><div><span style="white-space:pre">        </span>m_wndContainer.SubclassDlgItem(IDC_CHART,this);</div><div><span style="white-space:pre">        </span>CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();</div><div><span style="white-space:pre">        </span>m_pChart1 = new CBCGPChartVisualObject(pContainer);</div><div><span style="white-space:pre">        </span>m_pChart2 = new CBCGPChartVisualObject(pContainer);</div><div><span style="white-space:pre">        </span>m_pChart3 = new CBCGPChartVisualObject(pContainer);</div><div><span style="white-space:pre">        </span>// </div><div><span style="white-space:pre">        </span>// 设置第一个图表;</div><div><span style="white-space:pre">        </span>m_pChart1->SetChartTitle(_T("图表一"));</div><div><span style="white-space:pre">        </span>CBCGPChartSeries* pSeries1 = m_pChart1->CreateSeries(_T("Series 1"));</div><div><span style="white-space:pre">        </span>CBCGPChartSeries* pSeries2 = m_pChart1->CreateSeries(_T("Series 2"));</div><div><span style="white-space:pre">        </span>pSeries1->AddDataPoint(1.);</div><div><span style="white-space:pre">        </span>pSeries1->AddDataPoint(2.);</div><div><span style="white-space:pre">        </span>pSeries1->AddDataPoint(3.);</div><div><span style="white-space:pre">        </span>pSeries1->AddDataPoint(2.);</div><div><span style="white-space:pre">        </span>pSeries1->AddDataPoint(4.);</div><div><span style="white-space:pre">        </span>pSeries2->AddDataPoint(3.);</div><div><span style="white-space:pre">        </span>pSeries2->AddDataPoint(4.);</div><div><span style="white-space:pre">        </span>pSeries2->AddDataPoint(6.);</div><div><span style="white-space:pre">        </span>pSeries2->AddDataPoint(3.);</div><div><span style="white-space:pre">        </span>pSeries2->AddDataPoint(8.);</div><div><span style="white-space:pre">        </span>m_pChart1->ShowDataMarkers(TRUE, -1, BCGPChartMarkerOptions::MS_RHOMBUS);</div><div><span style="white-space:pre">        </span>// 设备第二个图表;</div><div><span style="white-space:pre">        </span>m_pChart2->SetChartTitle(_T("图表二"));</div><div><span style="white-space:pre">        </span>m_pChart2->SetChartType(BCGPChartDoughnut3D);</div><div><span style="white-space:pre">        </span>m_pChart2->AddChartData(1.);</div><div><span style="white-space:pre">        </span>m_pChart2->AddChartData(2.);</div><div><span style="white-space:pre">        </span>m_pChart2->AddChartData(5.);</div><div><span style="white-space:pre">        </span>m_pChart2->AddChartData(3.);</div><div><span style="white-space:pre">        </span>m_pChart2->AddChartData(2.);</div><div><span style="white-space:pre">        </span>for (int i = 0; i < m_pChart2->GetSeriesCount(); i++)</div><div><span style="white-space:pre">        </span>{</div><div><span style="white-space:pre">                </span>m_pChart2->GetSeries(i)->SetDefaultFillGradientType(CBCGPBrush::BCGP_GRADIENT_BEVEL);</div><div><span style="white-space:pre">        </span>}</div><div><span style="white-space:pre">        </span>//</div><div><span style="white-space:pre">        </span>m_pChart3->SetChartType(BCGPChartArea);</div><div><span style="white-space:pre">        </span>m_pChart3->ShowAxisIntervalInterlacing(BCGP_CHART_X_PRIMARY_AXIS);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(0.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(1.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(2.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(5.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(3.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(2.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(4.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(3.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(8.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(7.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(9.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(6.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(11.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(5.);</div><div><span style="white-space:pre">        </span>m_pChart3->AddChartData(0.);</div><div><span style="white-space:pre">        </span>m_pChart3->SetCurveType(BCGPChartFormatSeries::CCT_SPLINE);</div><div><span style="white-space:pre">        </span>m_pChart3->SetThemeOpacity(60);</div><div><span style="white-space:pre">        </span>m_pChart3->ShowDataMarkers();</div><div><span style="white-space:pre">        </span>m_pChart3->ShowDataLabels();</div><div><span style="white-space:pre">        </span>//设置三图表区域;</div><div><span style="white-space:pre">        </span>const double margin = 1;</div><div><span style="white-space:pre">        </span>CBCGPRect rc = pContainer->GetRect();</div><div><span style="white-space:pre">        </span>CBCGPRect rectChart1 = rc;</div><div><span style="white-space:pre">        </span>rectChart1.SetSize(rc.Width() / 2, rc.Height() / 2);</div><div><span style="white-space:pre">        </span>rectChart1.DeflateRect(margin, margin);</div><div><span style="white-space:pre">        </span>m_pChart1->SetRect(rectChart1);</div><div>
  3. </div><div><span style="white-space:pre">        </span>CBCGPRect rectChart2 = rectChart1;</div><div><span style="white-space:pre">        </span>rectChart2.OffsetRect(rc.Width() / 2, 0);</div><div><span style="white-space:pre">        </span>m_pChart2->SetRect(rectChart2);</div><div>
  4. </div><div><span style="white-space:pre">        </span>CBCGPRect rectChart3 = rc;</div><div><span style="white-space:pre">        </span>rectChart3.top += rc.Height() / 2;</div><div><span style="white-space:pre">        </span>rectChart3.DeflateRect(margin, margin);</div><div><span style="white-space:pre">        </span>m_pChart3->SetRect(rectChart3);</div><div>
  5. </div><div><span style="white-space:pre">        </span>//添加到布局管理器统一管理布局;</div><div><span style="white-space:pre">        </span>if (GetLayout() == NULL)</div><div><span style="white-space:pre">                </span>return 0;</div><div><span style="white-space:pre">        </span>CBCGPStaticLayout* pLayout = (CBCGPStaticLayout*)GetLayout();</div><div><span style="white-space:pre">        </span>if (pLayout == NULL)</div><div><span style="white-space:pre">                </span>return 0;</div><div><span style="white-space:pre">        </span>pLayout->AddAnchor(m_wndContainer.GetDlgCtrlID(), CBCGPStaticLayout::e_MoveTypeNone, CBCGPStaticLayout::e_SizeTypeBoth);</div><div><span style="white-space:pre">        </span>//</div><div><span style="white-space:pre">        </span>return bRst;</div><div>}</div></div><div></div>
复制代码


当窗口大小发生变化时会触发消息WM_SIZE,例程添加消息处理函数,实现图表重新布局调整大小。
  1. void CMy123View::OnSize(UINT nType, int cx, int cy)
  2. {
  3.         CBCGPFormView::OnSize(nType, cx, cy);

  4.         CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
  5.         if (pContainer == NULL || pContainer->GetCount() == 0)
  6.         {
  7.                 return;
  8.         }

  9.         const double margin = 10;
  10.         CBCGPRect rc = pContainer->GetRect();
  11.         CBCGPRect rectChart1 = rc;
  12.         rectChart1.SetSize(rc.Width() / 2, rc.Height() / 2);
  13.         rectChart1.DeflateRect(margin, margin);
  14.         m_pChart1->SetRect(rectChart1);

  15.         CBCGPRect rectChart2 = rectChart1;
  16.         rectChart2.OffsetRect(rc.Width() / 2, 0);
  17.         m_pChart2->SetRect(rectChart2);

  18.         CBCGPRect rectChart3 = rc;
  19.         rectChart3.top += rc.Height() / 2;
  20.         rectChart3.DeflateRect(margin, margin);
  21.         m_pChart3->SetRect(rectChart3);

  22.         m_wndContainer.RedrawWindow();
  23. }
复制代码
例程有用到界面布局管理器,所以在构造函数中开启管理器。
  1. CMy123View::CMy123View()
  2.         : CBCGPFormView(CMy123View::IDD)
  3. {
  4.         EnableVisualManagerStyle();
  5.         EnableLayout();
  6.         // TODO: add construction code here

  7. }
复制代码
例程所用到的扩展库可以网站搜索BCGP下载使用。
下面是例程源代码:
请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:图表组合创建与显示.rar 
文件大小:78.45 KB  售价:3金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我


  

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

  

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

  

QQ联系我

微信扫扫联系我

  


回复

使用道具 举报

快速回复 返回列表 客服中心 搜索