void CChartLegend::Draw(CDC *pDC) { if (!pDC->GetSafeHdc()) return; if (!m_bIsVisible) return; if (m_ObjectRect.IsRectEmpty()) return; CPen SolidPen(PS_SOLID,0,RGB(0,0,0)); CPen* pOldPen; CFont* pOldFont; CFont NewFont; NewFont.CreatePointFont(m_iFontSize,m_strFontName.c_str(),pDC); // Draw the shadow if (m_bShadow) { CRect ShadowRect = m_ObjectRect; ShadowRect.OffsetRect(m_iShadowDepth,m_iShadowDepth); CBrush BrushShadow; BrushShadow.CreateSolidBrush(m_ShadowColor) ; pDC->FillRect(ShadowRect,&BrushShadow); } if (!m_bIsTransparent) { //Fill back color CBrush BrushBack; BrushBack.CreateSolidBrush(m_ObjectColor) ; pDC->FillRect(m_ObjectRect,&BrushBack); } pOldFont = pDC->SelectObject(&NewFont); pOldPen = pDC->SelectObject(&SolidPen); //Draw rectangle: pDC->MoveTo(m_ObjectRect.left,m_ObjectRect.top); pDC->LineTo(m_ObjectRect.right,m_ObjectRect.top); pDC->LineTo(m_ObjectRect.right,m_ObjectRect.bottom); pDC->LineTo(m_ObjectRect.left,m_ObjectRect.bottom); pDC->LineTo(m_ObjectRect.left,m_ObjectRect.top); int iPrevMode = pDC->SetBkMode(TRANSPARENT); CRect rectBitmap(m_ObjectRect.left+2,m_ObjectRect.top+5, m_ObjectRect.left+2+m_BitmapSize.cx, m_ObjectRect.top+6+m_BitmapSize.cy); int SeriesCount = m_pParent->GetSeriesCount(); for (int i=0;i<SeriesCount;i++) { CChartSerie* pSerie = m_pParent->GetSerie(i); if ( (pSerie->GetName() == _T("")) || !pSerie->IsVisible() ) continue; int MaxHeight = 0; CSize TextSize = pDC->GetTextExtent(pSerie->GetName().c_str()); if (TextSize.cy > m_BitmapSize.cy) { pDC->ExtTextOut(rectBitmap.right+4,rectBitmap.top,ETO_CLIPPED,NULL,pSerie->GetName().c_str(),NULL); CRect rectTemp(rectBitmap); int YOffset = TextSize.cy/2 - rectBitmap.Height()/2; rectTemp.OffsetRect(0,YOffset); pSerie->DrawLegend(pDC,rectTemp); MaxHeight = TextSize.cy; } else { int YOffset = rectBitmap.CenterPoint().y - TextSize.cy/2; pDC->ExtTextOut(rectBitmap.right+4,YOffset,ETO_CLIPPED,NULL,pSerie->GetName().c_str(),NULL); MaxHeight = m_BitmapSize.cy; pSerie->DrawLegend(pDC,rectBitmap); } if (!m_bIsHorizontal) rectBitmap.OffsetRect(0,MaxHeight+2); else rectBitmap.OffsetRect(m_BitmapSize.cx+4+TextSize.cx+10,0); } pDC->SetBkMode(iPrevMode); pDC->SelectObject(pOldFont); DeleteObject(NewFont); pDC->SelectObject(pOldPen); DeleteObject(SolidPen); }
void CChartLegend::Draw(CDC *pDC) { if (!pDC->GetSafeHdc()) return; if (!m_bIsVisible) return; int YPos = 2; int MaxBitmapWidth = 0; bool bDraw = false; size_t SeriesCount = m_pParent->GetSeriesCount(); size_t i=0; for (i=0;i<SeriesCount;i++) { CChartSerie* pSerie = m_pParent->GetSerie(i); if ( (pSerie->GetName() == _T("")) || !pSerie->IsVisible() ) continue; bDraw = true; CSize BitmapSize = pSerie->GetLegendSize(); if (BitmapSize.cx > MaxBitmapWidth) MaxBitmapWidth = BitmapSize.cx; } if (!bDraw) return; CPen SolidPen(PS_SOLID,0,RGB(0,0,0)); CPen* pOldPen; CFont* pOldFont; CFont NewFont; NewFont.CreatePointFont(m_iFontSize,(LPCTSTR)m_strFontName.c_str(),pDC); //Fill back color CBrush m_BrushBack; m_BrushBack.CreateSolidBrush(m_ObjectColor) ; pDC->FillRect(m_ObjectRect,&m_BrushBack); COLORREF OldColor = pDC->SetBkColor(m_ObjectColor); pOldFont = pDC->SelectObject(&NewFont); pOldPen = pDC->SelectObject(&SolidPen); //Draw rectangle: pDC->MoveTo(m_ObjectRect.left,m_ObjectRect.top); pDC->LineTo(m_ObjectRect.right,m_ObjectRect.top); pDC->LineTo(m_ObjectRect.right,m_ObjectRect.bottom); pDC->LineTo(m_ObjectRect.left,m_ObjectRect.bottom); pDC->LineTo(m_ObjectRect.left,m_ObjectRect.top); CPoint UpperLeft; UpperLeft.x = m_ObjectRect.left; UpperLeft.y = m_ObjectRect.top + 4; for (i=0;i<SeriesCount;i++) { CChartSerie* pSerie = m_pParent->GetSerie(i); int NewHeight = pSerie->DrawLegend(pDC,UpperLeft,MaxBitmapWidth); UpperLeft.y += NewHeight+2; } pDC->SelectObject(pOldFont); DeleteObject(NewFont); pDC->SelectObject(pOldPen); DeleteObject(SolidPen); pDC->SetBkColor(OldColor); }
void CChartCtrl::RefreshCtrl() { CClientDC dc(this) ; CRect ClientRect; GetClientRect(ClientRect); m_PlottingRect = ClientRect; CBrush m_BrushBack; m_BrushBack.CreateSolidBrush(BackColor) ; if (!m_BackgroundDC.GetSafeHdc() ) { CBitmap memBitmap; m_BackgroundDC.CreateCompatibleDC(&dc) ; memBitmap.CreateCompatibleBitmap(&dc, ClientRect.Width(),ClientRect.Height()) ; m_BackgroundDC.SelectObject(&memBitmap) ; } m_BackgroundDC.SetBkColor(BackColor); m_BackgroundDC.FillRect(ClientRect,&m_BrushBack); m_BackgroundDC.DrawEdge(ClientRect,EdgeType,BF_RECT); ClientRect.DeflateRect(3,3); CSize TitleSize = m_pTitles->GetSize(&m_BackgroundDC); CRect rcTitle; rcTitle = ClientRect; rcTitle.bottom = TitleSize.cy; ClientRect.top += TitleSize.cy; m_pTitles->SetRect(rcTitle); m_pTitles->Draw(&m_BackgroundDC); CSize LegendSize = m_pLegend->GetSize(&m_BackgroundDC); if ( (LegendSize.cx >= (ClientRect.right-ClientRect.left) - 6) || (LegendSize.cy >= (ClientRect.bottom-ClientRect.top)) ) {} else { ClientRect.right -= LegendSize.cx + 6; int XPos = ClientRect.right + 1; int YPos = ((ClientRect.bottom-ClientRect.top)/2) - (LegendSize.cy/2); m_pLegend->SetPosition(XPos,YPos,&m_BackgroundDC); m_pLegend->Draw(&m_BackgroundDC); } /* size_t AxisCount = m_pAxisList.size(); for (size_t j=0;j<AxisCount;j++) m_pAxisList[j]->PrepareAutoAxis(); size_t SeriesCount = m_pSeriesList.size(); for (size_t i=0;i<SeriesCount;i++) { CChartSerie* m_NewLine = (CChartSerie*)m_pSeriesList[i]; m_NewLine->RefreshAutoAxes(); }*/ //Clip all the margins (axis) of the client rect size_t AxisCount = m_pAxisList.size(); size_t SeriesCount = m_pSeriesList.size(); for (size_t m=0;m<AxisCount;m++) { m_pAxisList[m]->CalculateTicksIncrement(); m_pAxisList[m]->ClipMargin(ClientRect,m_PlottingRect,&m_BackgroundDC); } for (size_t n=0;n<AxisCount;n++) { m_pAxisList[n]->SetAxisSize(ClientRect,m_PlottingRect); m_pAxisList[n]->Draw(&m_BackgroundDC); } CPen SolidPen(PS_SOLID,0,m_BorderColor); CPen* pOldPen = m_BackgroundDC.SelectObject(&SolidPen); m_BackgroundDC.MoveTo(m_PlottingRect.left,m_PlottingRect.top); m_BackgroundDC.LineTo(m_PlottingRect.right,m_PlottingRect.top); m_BackgroundDC.LineTo(m_PlottingRect.right,m_PlottingRect.bottom); m_BackgroundDC.LineTo(m_PlottingRect.left,m_PlottingRect.bottom); m_BackgroundDC.LineTo(m_PlottingRect.left,m_PlottingRect.top); m_BackgroundDC.SelectObject(pOldPen); DeleteObject(SolidPen); for (size_t z=0;z<SeriesCount;z++) { CChartSerie* m_NewLine = (CChartSerie*)m_pSeriesList[z]; m_NewLine->SetRect(m_PlottingRect); m_NewLine->DrawAll(&m_BackgroundDC); } Invalidate(); }