上位机MFC扩展编程实例图表组合创建与显示
在前面例程中图表的创建可以通过自自定变量CBCGPChartCtrl m_wndChart直接实现。
但在多个图表统一管理时,一个一个的创建图表变显得效率底下。
当前例程实现通过容器来管理多图表功能,界面如下:
例程创建了三个图表,每个图表类型不同,相互独立但又创建在一容器内。
容器统一管理图表的布局,下面是例程实现过程的介绍。
先是添加容器与图表变量,并添加虚函数,在函数内创建变量。
CBCGPVisualContainerCtrl m_wndContainer;
CBCGPChartVisualObject* m_pChart1;
CBCGPChartVisualObject* m_pChart2;
CBCGPChartVisualObject* m_pChart3;
- <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>
- </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>
- </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>
- </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>
- </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,例程添加消息处理函数,实现图表重新布局调整大小。
- void CMy123View::OnSize(UINT nType, int cx, int cy)
- {
- CBCGPFormView::OnSize(nType, cx, cy);
- CBCGPVisualContainer* pContainer = m_wndContainer.GetVisualContainer();
- if (pContainer == NULL || pContainer->GetCount() == 0)
- {
- return;
- }
- const double margin = 10;
- CBCGPRect rc = pContainer->GetRect();
- CBCGPRect rectChart1 = rc;
- rectChart1.SetSize(rc.Width() / 2, rc.Height() / 2);
- rectChart1.DeflateRect(margin, margin);
- m_pChart1->SetRect(rectChart1);
- CBCGPRect rectChart2 = rectChart1;
- rectChart2.OffsetRect(rc.Width() / 2, 0);
- m_pChart2->SetRect(rectChart2);
- CBCGPRect rectChart3 = rc;
- rectChart3.top += rc.Height() / 2;
- rectChart3.DeflateRect(margin, margin);
- m_pChart3->SetRect(rectChart3);
- m_wndContainer.RedrawWindow();
- }
复制代码 例程有用到界面布局管理器,所以在构造函数中开启管理器。
- CMy123View::CMy123View()
- : CBCGPFormView(CMy123View::IDD)
- {
- EnableVisualManagerStyle();
- EnableLayout();
- // TODO: add construction code here
- }
复制代码 例程所用到的扩展库可以网站搜索BCGP下载使用。
下面是例程源代码:
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|