CSize CChartLegend::GetSize(CDC* pDC) const { CSize LegendSize; CSize TextSize; if (!m_bIsVisible) { LegendSize.cx = LegendSize.cy = 0; return LegendSize; } CFont* pOldFont; CFont NewFont; NewFont.CreatePointFont(m_iFontSize, (LPCTSTR)m_strFontName.c_str(),pDC); pOldFont = pDC->SelectObject(&NewFont); int Height = 4; //Upper space int Width = 0; int MaxTextWidth = 0; size_t SeriesCount = m_pParent->GetSeriesCount(); int Drawn = 0; for (size_t i=0;i<SeriesCount;i++) { CChartSerie* pSerie = m_pParent->GetSerie(i); if ( (pSerie->GetName() == _T("")) || !pSerie->IsVisible() ) continue; Drawn++; CSize BitmapSize =pSerie->GetLegendSize(); TextSize = pDC->GetTextExtent(pSerie->GetName().c_str()); if (TextSize.cy>BitmapSize.cy) Height += TextSize.cy + 2; else Height += BitmapSize.cy + 2; if ( (TextSize.cx + BitmapSize.cx + 6)> MaxTextWidth) MaxTextWidth = TextSize.cx + BitmapSize.cx + 6; } if (!Drawn) { LegendSize.cx = LegendSize.cy = 0; return LegendSize; } Width += MaxTextWidth + 6; LegendSize.cx = Width; LegendSize.cy = Height+2; pDC->SelectObject(pOldFont); DeleteObject(NewFont); return LegendSize; }
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 CChartLegend::UpdatePosition(CDC* pDC, const CRect& rcControl) { CRect NewPosition; NewPosition.SetRectEmpty(); if (!m_bIsVisible) { SetRect(NewPosition); return; } CFont* pOldFont; CFont NewFont; NewFont.CreatePointFont(m_iFontSize,m_strFontName.c_str(),pDC); pOldFont = pDC->SelectObject(&NewFont); int Height = 0; int Width = 0; int MaxText = 0; CSize TextSize; size_t SeriesCount = m_pParent->GetSeriesCount(); int Drawn = 0; for (size_t i=0;i<SeriesCount;i++) { CChartSerie* pSerie = m_pParent->GetSerie(i); if ( (pSerie->GetName() == _T("")) || !pSerie->IsVisible() ) continue; Drawn++; TextSize = pDC->GetTextExtent(pSerie->GetName().c_str()); if (!m_bIsHorizontal) { if (TextSize.cy>m_BitmapSize.cy) Height += TextSize.cy + 2; else Height += m_BitmapSize.cy + 2; if (TextSize.cx > MaxText) MaxText = TextSize.cx; } else { Width += TextSize.cx + 4 + m_BitmapSize.cx + 10; if (TextSize.cy > MaxText) MaxText = TextSize.cy; } } pDC->SelectObject(pOldFont); DeleteObject(NewFont); if (!Drawn) { SetRect(NewPosition); return; } if (!m_bIsHorizontal) { Width += MaxText + m_BitmapSize.cx + 12; Height += 4 + 4 - 2; // Top and bottom margins. -2 because space counted once too much } else { Width += 2 + 2 - 10; Height = 4 + max(m_BitmapSize.cy,MaxText) + 4; } if (!m_bDocked) { NewPosition.top = m_iTopPos; NewPosition.left = m_iLeftPos; NewPosition.bottom = m_iTopPos + Height + 2; NewPosition.right = m_iLeftPos + Width; } else { switch (m_DockSide) { case dsDockRight: NewPosition.top = ((rcControl.bottom-rcControl.top)/2) - ((Height + 2)/2); NewPosition.left = rcControl.right - (Width + 6); NewPosition.bottom = NewPosition.top + Height; NewPosition.right = NewPosition.left + Width; break; case dsDockLeft: NewPosition.top = ((rcControl.bottom-rcControl.top)/2) - ((Height + 2)/2); NewPosition.left = rcControl.left + 3; NewPosition.bottom = NewPosition.top + Height; NewPosition.right = NewPosition.left + Width; break; case dsDockTop: NewPosition.top = rcControl.top + 3; //((rcControl.bottom-rcControl.top)/2) - ((Height + 2)/2); NewPosition.left = ((rcControl.right-rcControl.left)/2) - (Width/2); // rcControl.left + 3; NewPosition.bottom = NewPosition.top + Height; NewPosition.right = NewPosition.left + Width; break; case dsDockBottom: NewPosition.top = rcControl.bottom - (Height + 2); //((rcControl.bottom-rcControl.top)/2) - ((Height + 2)/2); NewPosition.left = ((rcControl.right-rcControl.left)/2) - (Width/2); // rcControl.left + 3; NewPosition.bottom = NewPosition.top + Height; NewPosition.right = NewPosition.left + Width; break; } } SetRect(NewPosition); }
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 CBase_SampleChart::OnExportData() { CString file = _T("C:/Points.csv"); CFileDialog mFileDlg(FALSE,_T("csv"),_T("Points"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("csv文件|*.csv||"),this); DWORD dwVersion = GetVersion(); DWORD dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion))); DWORD dwWindowsMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion))); if (dwVersion < 0x80000000) mFileDlg.m_ofn.lStructSize=88;//显示新的文件对话框 else mFileDlg.m_ofn.lStructSize=76;//显示老的文件对话框 mFileDlg.m_ofn.lpstrTitle= _T("保存到文件"); if(mFileDlg.DoModal() !=IDOK ) return; file = mFileDlg.GetPathName(); CString strSep = _T(","); CFile f; BOOL ret = f.Open(file,CFile::modeCreate | CFile::modeReadWrite); if(!ret) { MessageBox(_T("打开文件失败,可能是文件为只读属性或被占用")); return; } CString line(_T("时间")); // CArray<CChartSerie*, CChartSerie*> series; series.Add(m_pcCurveMain); unsigned maxCount = 0; CChartSerie* pSerieOfMaxCount = NULL; for(int i=0; i<=series.GetUpperBound(); ++i) { CChartSerie* pS = series.GetAt(i); unsigned c = pS->GetPointsCount(); if(c > maxCount) { maxCount = c; pSerieOfMaxCount = pS; } line = line + strSep + pS->GetName().c_str(); } //标题列 { TWen_USES_CONVERSION; LPCSTR s=TWen_CT2A(line + _T("\r\n")); f.Write(s,strlen(s)); } for(size_t i=0; i<maxCount; i++) { line.Format(_T("%.15f"), pSerieOfMaxCount->m_vPoints.m_pPoints[i].x); for(int j=0; j<=series.GetUpperBound(); ++j) { CChartSerie* pS = GetSerieByIndex(j); unsigned c = pS->GetPointsCount(); CString v; if(c > i) v.Format(_T("%.15f"), pS->m_vPoints.m_pPoints[i].y); line = line + strSep + v; } { TWen_USES_CONVERSION; LPCSTR s=TWen_CT2A(line + _T("\r\n")); f.Write(s,strlen(s)); } } f.Close(); MessageBox(_T("数据已保存到文件[")+file+_T("]")); }