📄 graphicview.cpp
字号:
// GraphicView.cpp : implementation file
//
#include "stdafx.h"
#include "Student.h"
#include "StudentDoc.h"
#include "GraphicView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGraphicView
IMPLEMENT_DYNCREATE(CGraphicView, CView)
CGraphicView::CGraphicView()
{
}
CGraphicView::~CGraphicView()
{
}
BEGIN_MESSAGE_MAP(CGraphicView, CView)
//{{AFX_MSG_MAP(CGraphicView)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGraphicView drawing
void CGraphicView::OnDraw(CDC* pDC)
{
CStudentDoc* pDoc = (CStudentDoc*)GetDocument();
// TODO: add draw code here
if (pDoc->m_studentlist.GetnStudent() && (pDoc->m_Period >= 0 && pDoc->m_Period <= 4))
{
float average = pDoc->m_studentlist.Average(pDoc->m_Period);
float stddev = pDoc->m_studentlist.StdDev(pDoc->m_Period);
PlotStatistics(pDC, average, stddev);
XY* pXY = pDoc->m_studentlist.Distribution(pDoc->m_Period);
PlotHistogram(pDC, pXY);
delete pXY;
}
}
/////////////////////////////////////////////////////////////////////////////
// CGraphicView diagnostics
#ifdef _DEBUG
void CGraphicView::AssertValid() const
{
CView::AssertValid();
}
void CGraphicView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGraphicView message handlers
// 绘制统计参数之函数
void CGraphicView::PlotStatistics(CDC* pDC, float average, float stddev)
{
CStudentDoc* pDoc = (CStudentDoc*)GetDocument();
ASSERT_VALID(pDoc);
// 根据阶段号显示不同的阶段名
CString period;
switch (pDoc->m_Period)
{
case EXERCISE:
period = "平时作业";
break;
case REPORT:
period = "实验报告";
break;
case MIDTERM:
period = "期中考试";
break;
case TERMINAL:
period = "期末考试";
break;
case MARK:
period = "总评成绩";
break;
}
// 绘制统计参数
CRect rc;
GetClientRect(rc);
rc.DeflateRect(10, 10, 5, 10);
int height = (rc.bottom - rc.top) / 15;
rc.bottom = rc.top + height;
CString str = "统计结果(" + period + ")";
pDC->DrawText(str, rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
rc.top = rc.bottom;
rc.bottom = rc.top + height;
str.Format("平均分数:%6.2f 标准偏差:%6.2f", average, stddev);
pDC->DrawText(str, rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
// 绘制成绩分布直方图之函数
void CGraphicView::PlotHistogram(CDC* pDC, XY* xy)
{
int ymax = 0, i, j = 0, k = 10;
for (i = 0; i < 10; i ++)
{
if (xy[i].y > ymax)
ymax = xy[i].y;
}
for (i = 0; i < 10; i ++)
{
if (xy[i].y == 0)
j ++;
else
break;
}
for (i = 9; i >= 0; i --)
{
if (xy[i].y == 0)
k --;
else
break;
}
CRect rc;
GetClientRect(rc);
rc.DeflateRect(10, 100, 5, 40);
int nSegWidth = rc.Width() / (k - j);
int nSegHeight = rc.Height() / ymax;
COLORREF crSeg = RGB(0, 0, 192);
CBrush brush1(HS_FDIAGONAL, crSeg);
CBrush brush2(HS_FDIAGONAL, crSeg);
CPen pen(PS_INSIDEFRAME, 2, crSeg);
CBrush* oldBrush = pDC->SelectObject(&brush1);
CPen* oldPen = pDC->SelectObject(&pen);
CRect rcSeg(rc);
rcSeg.right = rcSeg.left + nSegWidth;
CString strSeg[] = {"0~10", "10~20", "20~30", "30~40", "40~50", "50~60", "60~70", "70~80", "80~90", "90~100" };
CRect rcStr;
for (i = j; i < k; i ++)
{
if (i % 2)
pDC->SelectObject(&brush2);
else
pDC->SelectObject(&brush1);
rcSeg.top = rcSeg.bottom - xy[i].y * nSegHeight - 2;
pDC->Rectangle(rcSeg);
if (xy[i].y > 0)
{
CString str;
str.Format("%d人", xy[i].y);
pDC->DrawText(str, rcSeg, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
}
rcStr = rcSeg;
rcStr.top = rcStr.bottom + 2;
rcStr.bottom += 20;
pDC->DrawText(strSeg[i], rcStr, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
rcSeg.OffsetRect(nSegWidth, 0);
}
pDC->SelectObject(oldBrush);
pDC->SelectObject(oldPen);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -