📄 viewgraph.cpp
字号:
{
pDC->TextOut(nX, m_rectAxis.bottom+1, m_aLabelText[i]);
} else
{
pDC->DrawText(m_aLabelText[i], &rect, DT_CENTER | DT_NOPREFIX | DT_WORDBREAK | DT_TOP);
}
}
// Tidy up
pDC->SelectObject(pFontOld);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::DrawYAxis(CDC* pDC)
{
CRect rect;
CFont font;
pDC->SetBkMode(TRANSPARENT);
// Determine font size
int nSize = 11;
ScaleFont(pDC, nSize, "123", m_rectAxis.left/3, m_rectAxis.Height());
// Create font
CreateFont(pDC, font, nSize);
CFont* pFontOld = pDC->SelectObject(&font);
CSize sz = pDC->GetTextExtent("123");
// Label axis
double d = m_prop.m_dYMin;
for (int i = 0; i <= m_prop.m_nYPoints; i++)
{
// Determine text
strstream sValue;
sValue.precision(10);
sValue << d << ends;
// Determine position of label
int nY = m_rectAxis.bottom + ((m_rectAxis.top - m_rectAxis.bottom) *
i) / m_prop.m_nYPoints;
rect.left = 0;
rect.right = m_rectAxis.left - sz.cx/3;
rect.top = nY - sz.cy;
rect.bottom = nY + sz.cy;
pDC->DrawText(sValue.str(),&rect, DT_RIGHT|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX);
sValue.rdbuf()->freeze(0);
// Draw label tick
pDC->MoveTo(m_rectAxis.left, nY);
pDC->LineTo(m_rectAxis.left-sz.cx/6, nY);
// Determine next value
d += (m_prop.m_dYMax - m_prop.m_dYMin) / m_prop.m_nYPoints;
}
// Tidy up
pDC->SelectObject(pFontOld);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::DrawGraph(CDC* pDC)
{
double dY, dY0;
// Draw the data on the graph
for (int i = 0; i < m_aGraphData.GetSize(); i++)
{
// Create pen
int nWidth = GetWidth(pDC, m_prop.m_aLineStyle[i], m_prop.m_aLineWidth[i]);
CPen pen(m_prop.m_aLineStyle[i], nWidth, m_prop.m_aColour[i]);
CPen* pPenOld = pDC->SelectObject(&pen);
// Draw lines
for (int j = 0; j < m_aGraphData[i].GetSize(); j++)
{
int nX = GetXPos(nWidth, i, j);
int nX1 = (nX + GetXPos(nWidth, i, j+1))/2;
int nX0 = nX - (nX1-nX) + nWidth;
nX1 -= nWidth;
dY0 = m_rectAxis.bottom - (0 - m_prop.m_dYMin) *
(m_rectAxis.Height()) / (m_prop.m_dYMax - m_prop.m_dYMin);
dY0 = max(dY0, m_rectAxis.top);
dY0 = min(dY0, m_rectAxis.bottom);
dY = m_rectAxis.bottom - (m_aGraphData[i][j] - m_prop.m_dYMin) *
(m_rectAxis.Height()) / (m_prop.m_dYMax - m_prop.m_dYMin);
// Vertical lines
if (m_prop.m_aGraphStyle[i] == CGraphProperties::lines)
{
pDC->MoveTo(nX, (int)dY0);
pDC->LineTo(nX, (int)dY);
}
// Vertical columns
else if (m_prop.m_aGraphStyle[i] == CGraphProperties::columns)
{
CRect rect;
rect.left = nX0;
rect.right = nX1;
rect.top = (long)dY;
rect.bottom = (long)dY0;
pDC->FillSolidRect(&rect, m_prop.m_aColour[i]);
}
// Symbols
else if (m_prop.m_aGraphStyle[i] == CGraphProperties::symbols)
{
int nY = (int)dY;
pDC->MoveTo(nX-nWidth, nY-nWidth);
pDC->LineTo(nX+nWidth, nY+nWidth);
pDC->MoveTo(nX-nWidth, nY+nWidth);
pDC->LineTo(nX+nWidth, nY-nWidth);
}
}
pDC->SelectObject(pPenOld);
};
}
/////////////////////////////////////////////////////////////////////////////
int CViewGraph::GetXPos(int nWidth, int i, int j)
{
return m_rectAxis.left + ((j+1)*m_rectAxis.Width())/(m_aGraphData[i].GetSize()+1);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::DrawTitle(CDC* pDC, int nAdjustV)
{
int nSize = 16;
CSize sz;
// Determine rectangle
CRect rect;
rect.left = m_rectAxis.left;
rect.right = m_rectAxis.right;
rect.top = m_rect.top;
rect.bottom = (m_rectAxis.top*nAdjustV)/100;
// Determine the size of font that will fit
ScaleFont(pDC, nSize, m_prop.m_sTitle, rect.Width());
// Create the font
CFont font;
CreateFont(pDC, font, nSize);
CFont* pFontOld = pDC->SelectObject(&font);
// Output the text
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(m_prop.m_sTitle, &rect, DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX);
pDC->SelectObject(pFontOld);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::DrawLegend(CDC* pDC, int nAdjustV, int nLegendW)
{
CFont font;
CRect rect;
// Size font according to the number of labels
int nFont = max(6,min(12,((12 * 8) / (m_prop.m_aLegendText.GetSize()+2))));
// Create font
ScaleFont(pDC, nFont, "123", (m_rect.right-m_rectAxis.right)/nLegendW);
CreateFont(pDC, font, nFont);
CFont* pFontOld = pDC->SelectObject(&font);
CSize sz = pDC->GetTextExtent("123");
int nY = (m_rectAxis.top * nAdjustV) / 100;
for (int i = 0; i < m_prop.m_aLegendText.GetSize(); i++)
{
// Determine height
int nWidth = GetWidth(pDC, m_prop.m_aLineStyle[i], m_prop.m_aLineWidth[i]);
CPen pen(m_prop.m_aLineStyle[i], nWidth, m_prop.m_aColour[i]);
CPen* pPenOld = pDC->SelectObject(&pen);
// Draw line
pDC->MoveTo(m_rectAxis.right + sz.cx/3, nY + sz.cy/2);
pDC->LineTo(m_rectAxis.right + sz.cx/3*2, nY);
// Draw the text
rect.left = m_rectAxis.right + sz.cx/3*3;
rect.right = m_rect.right - sz.cx/3;
rect.top = nY;
rect.bottom = m_rect.bottom;
nY += pDC->DrawText(m_prop.m_aLegendText[i], &rect, DT_LEFT|DT_WORDBREAK|DT_TOP|DT_NOPREFIX);
// Tidy up
pDC->SelectObject(pPenOld);
}
pDC->SelectObject(pFontOld);
}
///////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnSize(UINT nType, int cx, int cy)
{
cy = max(50, cy);
CBDView::OnSize(nType, cx, cy);
}
/////////////////////////////////////////////////////////////////////////////
BOOL CViewGraph::OnPreparePrinting(CPrintInfo* pInfo)
{
return DoPreparePrinting(pInfo);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
{
CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
pMainFrame->GetMessageBar()->SetWindowText("");
CBDView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnFileSaveAs()
{
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnUpdateFileSaveAs(CCmdUI* pCmdUI)
{
pCmdUI->Enable(FALSE);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnUpdateFileOpen(CCmdUI* pCmdUI)
{
pCmdUI->Enable(FALSE);
}
/////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
m_rect = pInfo->m_rectDraw;
CBDView::OnPrint(pDC, pInfo);
}
/////////////////////////////////////////////////////////////////////////////
COLORREF CViewGraph::GetColour(int i)
{
const COLORREF aCR[] = {RGB(0,0,128),RGB(0,128,0),RGB(128,0,0),
RGB(0,128,128),RGB(128,128,0),RGB(128,0,128),
RGB(0,0,255),RGB(0,255,0),RGB(255,0,0),
RGB(0,255,255),RGB(255,255,0),RGB(255,0,255),
RGB(0,0,192),RGB(0,192,0),RGB(192,0,0),
RGB(0,192,192),RGB(192,192,0),RGB(192,0,192),
RGB(0,0,96),RGB(0,96,0),RGB(96,0,0),
RGB(0,96,96),RGB(96,96,0),RGB(96,0,96)};
return aCR[i % (sizeof(aCR)/sizeof(COLORREF))];
}
/////////////////////////////////////////////////////////////////////////////
int CViewGraph::GetWidth(CDC* pDC, int nStyle, int nWidth)
{
if (nStyle == PS_SOLID)
return (int)(pDC->GetDeviceCaps(LOGPIXELSX) / MMPERINCH /3)*nWidth;
else
return 1;
}
//////////////////////////////////////////////////////////////////////////////
void CViewGraph::ScaleFont(CDC* pDC, int& nFont, LPCSTR s, int nWidth, int nHeight)
{
CSize sz;
do
{
CFont font;
CreateFont(pDC, font, nFont);
CFont* pFontOld = pDC->SelectObject(&font);
sz = pDC->GetTextExtent(s);
pDC->SelectObject(pFontOld);
nFont--;
} while ((sz.cx > nWidth || (sz.cy > nHeight && nHeight != 0)) && nFont > 2);
}
//////////////////////////////////////////////////////////////////////////////
void CViewGraph::CreateFont(CDC* pDC, CFont& font, int nSize, BOOL bVert)
{
// Adjust size of font according to size of screen
int n = (int)sqrt(square(m_rect.Width()) + square(m_rect.Height()));
nSize = max(1,min((nSize*3)/2,(nSize * n) / 800));
// Create font
LOGFONT lf = m_prop.m_logfont;
lf.lfHeight = -(nSize * m_prop.m_logfont.lfHeight) / 12;
if (bVert)
{
lf.lfOrientation = -450;
lf.lfEscapement = -450;
}
CBDView::ScaleFont(pDC, &lf);
font.CreateFontIndirect(&lf);
}
///////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnEditCopy()
{
CBDView::OnEditCopy();
}
///////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnGraphProp()
{
CDlgGraphProp dlg(&m_prop);
if (dlg.DoModal() == IDOK)
{
Invalidate();
RedrawWindow();
}
}
///////////////////////////////////////////////////////////////////////////////
void CViewGraph::OnLButtonDblClk(UINT nFlags, CPoint point)
{
OnGraphProp();
CBDView::OnLButtonDblClk(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -