📄 graph.cpp
字号:
dummyFont.CreateFont(12, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
pOldFont = (CFont*)pDC->SelectObject(&dummyFont);
switch(graphType)
{
case BAR_GRAPH :
case LINE_GRAPH :
case SCATTER_GRAPH :
case BOX_WHISKER_GRAPH :
// case RADAR_GRAPH :
case STACKED_BAR_GRAPH :
case XY_LINE_GRAPH :
CPen* pOldAxisPen;
pOldAxisPen = pDC->SelectObject(&axisPen);
switch(graphQuadType)
{
case 1 :
//draw y axis
pDC->MoveTo(xApexPoint, yApexPoint);
pDC->LineTo(xApexPoint, yApexPoint - yAxisHeight);
//draw x axis
pDC->MoveTo(xApexPoint, yApexPoint);
pDC->LineTo(xApexPoint + xAxisWidth, yApexPoint);
break;
case 2 :
//draw y axis
pDC->MoveTo(xApexPoint, yApexPoint);
pDC->LineTo(xApexPoint, yApexPoint - yAxisHeight);
//draw x axis
pDC->MoveTo(xApexPoint - (xAxisWidth / 2), yApexPoint);
pDC->LineTo(xApexPoint + (xAxisWidth / 2), yApexPoint);
break;
case 3 :
//draw y axis
pDC->MoveTo(xApexPoint, yApexPoint + (yAxisHeight / 2));
pDC->LineTo(xApexPoint, yApexPoint - (yAxisHeight / 2));
//draw x axis
pDC->MoveTo(xApexPoint, yApexPoint);
pDC->LineTo(xApexPoint + xAxisWidth, yApexPoint);
break;
case 4 :
//draw y axis
pDC->MoveTo(xApexPoint, yApexPoint + (yAxisHeight / 2));
pDC->LineTo(xApexPoint, yApexPoint - (yAxisHeight / 2));
//draw x axis
pDC->MoveTo(xApexPoint - (xAxisWidth / 2), yApexPoint);
pDC->LineTo(xApexPoint + (xAxisWidth / 2), yApexPoint);
break;
}
pDC->SelectObject(pOldAxisPen);
int tFontSize;
//draw labels
tFontSize = 16;
if(maxWidth > maxHeight)
{
while((axisYLabel.GetLength() * (tFontSize / 2)) > maxHeight)
{
tFontSize -= 2;
}
}
else
{
while((axisXLabel.GetLength() * (tFontSize / 2)) > maxWidth)
{
tFontSize -= 2;
}
}
axisFont.CreateFont(tFontSize, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
sideFont.CreateFont(tFontSize, 0, 900, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
pDC->SelectObject(&sideFont);
pDC->GetTextMetrics(&tm);
charWidth = tm.tmAveCharWidth;
if(graphAlignment)
{
switch(graphQuadType)
{
case 1 :
case 2 :
pDC->TextOut(10, (yApexPoint - (yAxisHeight / 2)) + ((axisYLabel.GetLength() * charWidth) / 2), axisYLabel);
break;
case 3 :
case 4 :
pDC->TextOut(10, yApexPoint + ((axisYLabel.GetLength() * charWidth) / 2), axisYLabel);
break;
}
pDC->SelectObject(&axisFont);
pDC->GetTextMetrics(&tm);
charWidth = tm.tmAveCharWidth;
switch(graphQuadType)
{
case 1 :
pDC->TextOut(xApexPoint + (xAxisWidth / 2) - ((axisXLabel.GetLength() / 2) * charWidth), maxHeight - 5 - 6, axisXLabel);
break;
case 2 :
pDC->TextOut(xApexPoint - ((axisXLabel.GetLength() / 2) * charWidth), maxHeight - 5 - 6, axisXLabel);
break;
case 3 :
pDC->TextOut(xApexPoint + (xAxisWidth / 2) - ((axisXLabel.GetLength() / 2) * charWidth), maxHeight - 5 - 6, axisXLabel);
break;
case 4 :
pDC->TextOut(xApexPoint - ((axisXLabel.GetLength() / 2) * charWidth), maxHeight - 5 - 6, axisXLabel);
break;
}
pDC->SelectObject(pOldFont);
tickScale = 0.00;
tickScale = (yAxisHeight - 10) / numTicks;
//draw y axis ticks
if(!inRedraw)
{
switch(graphQuadType)
{
case 1 :
for(y = 1; y <= numTicks; y++)
{
tickYLocation = (int)(yApexPoint - (y * tickScale));
//draw tick mark
pDC->MoveTo(xApexPoint - 3, tickYLocation);
pDC->LineTo(xApexPoint + 3, tickYLocation);
if(graphHasGridLines)
{
//draw grid lines
COLORREF gridLineColor;
gridLineColor = DARK_GREY;
CPen gridPen (PS_SOLID, 1, gridLineColor);
CPen* pOldPen;
pOldPen = pDC->SelectObject(&gridPen);
pDC->MoveTo(xApexPoint, tickYLocation);
pDC->LineTo(xApexPoint + xAxisWidth, tickYLocation);
pDC->SelectObject(pOldPen);
}
//draw tick label
CString tickLabel;
tickLabel.Format("%d", minTick + (y * tickSpace));
CFont yFont;
yFont.CreateFont(yTickFontSize, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
pDC->SelectObject(&yFont);
pDC->TextOut(xApexPoint - 10 - (tickLabel.GetLength() * (yTickFontSize / 2)), tickYLocation - 6, tickLabel);
pDC->SelectObject(pOldFont);
topYTick = tickYLocation;
}
break;
pDC->SelectObject(pOldFont);
case 2 :
if(graphType == SCATTER_GRAPH)
{
double oldTickScale;
int oldNumTicks;
oldNumTicks = numTicks;
oldTickScale = tickScale;
numTicks /= 2;
tickScale *= 2;
for(y = 1; y <= numTicks; y++)
{
tickYLocation = (int)(yApexPoint - (y * tickScale));
//draw tick mark
pDC->MoveTo(xApexPoint - 3, tickYLocation);
pDC->LineTo(xApexPoint + 3, tickYLocation);
if(graphHasGridLines)
{
//draw grid lines
COLORREF gridLineColor;
gridLineColor = LIGHT_GREY;
CPen gridPen (PS_SOLID, 1, gridLineColor);
CPen* pOldPen;
pOldPen = pDC->SelectObject(&gridPen);
pDC->MoveTo(xApexPoint - (xAxisWidth / 2), tickYLocation);
pDC->LineTo(xApexPoint + (xAxisWidth / 2), tickYLocation);
pDC->SelectObject(pOldPen);
}
//draw tick label
CString tickLabel;
tickLabel.Format("%d", minTick + (y * tickSpace));
CFont yFont;
yFont.CreateFont(yTickFontSize, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
pDC->SelectObject(&yFont);
pDC->TextOut(xApexPoint - (xAxisWidth / 2) - 10 - (tickLabel.GetLength() * (yTickFontSize / 2)), tickYLocation - 6, tickLabel);
pDC->SelectObject(pOldFont);
topYTick = tickYLocation;
}
//draw the 0
pDC->TextOut(xApexPoint - (xAxisWidth / 2) - 10 - (yTickFontSize / 2), yApexPoint - 6, "0");
//reset numTicks and tickScale back to normal
tickScale = oldTickScale;
numTicks = oldNumTicks;
}
else
{
for(y = 1; y <= numTicks; y++)
{
tickYLocation = (int)(yApexPoint - (y * tickScale));
//draw tick mark
pDC->MoveTo(xApexPoint - 3, tickYLocation);
pDC->LineTo(xApexPoint + 3, tickYLocation);
if(graphHasGridLines)
{
//draw grid lines
COLORREF gridLineColor;
gridLineColor = DARK_GREY;
CPen gridPen (PS_SOLID, 1, gridLineColor);
CPen* pOldPen;
pOldPen = pDC->SelectObject(&gridPen);
pDC->MoveTo(xApexPoint - (xAxisWidth / 2), tickYLocation);
pDC->LineTo(xApexPoint + (xAxisWidth / 2), tickYLocation);
pDC->SelectObject(pOldPen);
}
//draw tick label
CString tickLabel;
tickLabel.Format("%d", minTick + (y * tickSpace));
CFont yFont;
yFont.CreateFont(yTickFontSize, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
pDC->SelectObject(&yFont);
pDC->TextOut(xApexPoint - (xAxisWidth / 2) - 10 - (tickLabel.GetLength() * (yTickFontSize / 2)), tickYLocation - 6, tickLabel);
topYTick = tickYLocation;
}
//draw the 0
pDC->TextOut(xApexPoint - (xAxisWidth / 2) - 10 - (yTickFontSize / 2), yApexPoint - 6, "0");
}
pDC->SelectObject(pOldFont);
break;
case 3 :
for(y = 0; y <= numTicks / 2; y++)
{
tickYLocation = (int)(yApexPoint + 5 - (yAxisHeight / 2) + (y * tickScale));
//draw tick mark
pDC->MoveTo(xApexPoint - 3, tickYLocation);
pDC->LineTo(xApexPoint + 3, tickYLocation);
if(graphHasGridLines)
{
//draw grid lines
COLORREF gridLineColor;
gridLineColor = DARK_GREY;
CPen gridPen (PS_SOLID, 1, gridLineColor);
CPen* pOldPen;
pOldPen = pDC->SelectObject(&gridPen);
pDC->MoveTo(xApexPoint, tickYLocation);
pDC->LineTo(xApexPoint, tickYLocation);
pDC->SelectObject(pOldPen);
}
//draw tick label
CString tickLabel;
tickLabel.Format("%d", maxTick - (y * tickSpace));
CFont yFont;
yFont.CreateFont(yTickFontSize, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
pDC->SelectObject(&yFont);
pDC->TextOut(xApexPoint - 10 - (tickLabel.GetLength() * (yTickFontSize / 2)), tickYLocation - 6, tickLabel);
topYTick = tickYLocation;
}
//draw the 0
pDC->TextOut(xApexPoint - (xAxisWidth / 2) - 10 - (yTickFontSize / 2), yApexPoint - 6, "0");
for(; y <= numTicks; y++)
{
tickYLocation = (int)(yApexPoint + 5 - (yAxisHeight / 2) + (y * tickScale));
//draw tick mark
pDC->MoveTo(xApexPoint - 3, tickYLocation);
pDC->LineTo(xApexPoint + 3, tickYLocation);
if(graphHasGridLines)
{
//draw grid lines
COLORREF gridLineColor;
gridLineColor = DARK_GREY;
CPen gridPen (PS_SOLID, 1, gridLineColor);
CPen* pOldPen;
pOldPen = pDC->SelectObject(&gridPen);
pDC->MoveTo(xApexPoint, tickYLocation);
pDC->LineTo(xApexPoint, tickYLocation);
pDC->SelectObject(pOldPen);
}
//draw tick label
CString tickLabel;
tickLabel.Format("%d", minTick + ((numTicks - y) * tickSpace));
pDC->TextOut(xApexPoint - 10 - (tickLabel.GetLength() * (yTickFontSize / 2)), tickYLocation - 6, tickLabel);
topYTick = tickYLocation;
}
pDC->SelectObject(pOldFont);
break;
pDC->SelectObject(pOldFont);
case 4 :
for(y = 1; y <= numTicks / 2; y++)
{
tickYLocation = (int)(yApexPoint - (y * tickScale));
//draw tick mark
pDC->MoveTo(xApexPoint - 3, tickYLocation);
pDC->LineTo(xApexPoint + 3, tickYLocation);
if(graphHasGridLines)
{
//draw grid lines
COLORREF gridLineColor;
gridLineColor = DARK_GREY;
CPen gridPen (PS_SOLID, 1, gridLineColor);
CPen* pOldPen;
pOldPen = pDC->SelectObject(&gridPen);
pDC->MoveTo(xApexPoint - (xAxisWidth / 2), tickYLocation);
pDC->LineTo(xApexPoint + (xAxisWidth / 2), tickYLocation);
pDC->SelectObject(pOldPen);
}
//draw tick label
CString tickLabel;
tickLabel.Format("%d", minTick + (y * tickSpace));
CFont yFont;
yFont.CreateFont(yTickFontSize, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
pDC->SelectObject(&yFont);
pDC->TextOut(xApexPoint - (xAxisWidth / 2) - 10 - (tickLabel.GetLength() * (yTickFontSize / 2)), tickYLocation - 6, tickLabel);
topYTick = tickYLocation;
}
//draw the 0
pDC->TextOut(xApexPoint - (xAxisWidth / 2) - 10 - (yTickFontSize / 2), yApexPoint - 6, "0");
for(; y <= numTicks; y++)
{
tickYLocation = (int)(yApexPoint - (yAxisHeight / 2) + (y * tickScale));
//draw tick mark
pDC->MoveTo(xApexPoint - 3, tickYLocation);
pDC->LineTo(xApexPoint + 3, tickYLocation);
if(graphHasGridLines)
{
//draw grid lines
COLORREF gridLineColor;
gridLineColor = DARK_GREY;
CPen gridPen (PS_SOLID, 1, gridLineColor);
CPen* pOldPen;
pOldPen = pDC->SelectObject(&gridPen);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -