📄 graph.cpp
字号:
//Graph.cpp - Version 3.0 (Brian Convery, May, 2001)
#include "stdafx.h"
#include "afxtempl.h"
//#include "GraphLegend.h"
#include "math.h"
#include "Graph.h"
//#include "GraphDataColor.h"
//#include "GraphPieLabel.h" //for pie labels
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGraph
CGraph::CGraph()
{
graphDataSet = new CObList();
seriesSize = 0;
graphAlignment = VERTICAL_ALIGN;
graphType = 0;
graphQuadType = 1;
quadSetManually = FALSE;
m_gMinDataSet.SetPosition(1000, 1000);
m_gMaxDataSet.SetPosition(0,0);
}
CGraph::CGraph(int type)
{
graphDataSet = new CObList();
seriesSize = 0;
graphAlignment = VERTICAL_ALIGN;
graphType = type;
graphQuadType = 1;
if(type != BAR_GRAPH)
quadSetManually = TRUE;
else
quadSetManually = FALSE;
}
CGraph::~CGraph()
{
POSITION pos;
CGraphDataSet * pSeries;
for( pos = graphDataSet->GetHeadPosition(); pos != NULL; )
{
pSeries = (CGraphDataSet*) graphDataSet->GetNext( pos );
graphDataSet->RemoveAt(pos);
delete pSeries;
}
graphDataSet->RemoveAll();
delete graphDataSet;
}
/////////////////////////////////////////////////////////////////////////////
// CGraph message handlers
void CGraph::SetGraphType(int gType)
{
graphType = gType;
}
void CGraph::DrawGraph(CDC* pDC)
{
CWnd* graphWnd = pDC->GetWindow();
CRect graphRect;
graphWnd->GetClientRect(&graphRect);
TEXTMETRIC tm;
//reset graph to be clear background
COLORREF backColor;
backColor = RGB(255,255,255); //replace with desired background color
CBrush backBrush (backColor);
CBrush* pOldBackBrush;
pOldBackBrush = pDC->SelectObject(&backBrush);
pDC->Rectangle(0, 0, graphRect.Width(), graphRect.Height());
pDC->SelectObject(pOldBackBrush);
maxHeight = graphRect.Height() - 20; //for frame window and status bar
maxWidth = graphRect.Width() - 5; //for frame window
//draw graph title
CFont titleFont;
int tFontSize = 24;
while((graphTitle.GetLength() * (tFontSize / 2)) > maxWidth)
{
tFontSize -= 2;
}
titleFont.CreateFont(tFontSize, 0, 0, 0, 700, FALSE, FALSE, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN,"Arial");
CFont* pOldFont = (CFont*) pDC->SelectObject(&titleFont);
pDC->GetTextMetrics(&tm);
int charWidth = tm.tmAveCharWidth;
//next line is centered....trust me !!
pDC->TextOut((graphRect.Width() / 2) - ((graphTitle.GetLength() / 2) * charWidth),
10, graphTitle);
pDC->SelectObject(pOldFont);
if((!quadSetManually) && (graphType == BAR_GRAPH))
{
//computer number of quadrants needed based on data
POSITION pos;
pos = graphDataSet->GetHeadPosition();
CGraphDataSet* tmpSeries;
int minXValue = 0;
int minYValue = 0;
for(int x = 1; x <= graphDataSet->GetCount(); x++)
{
tmpSeries = (CGraphDataSet*)graphDataSet->GetNext(pos);
for(int s = 0; s < seriesSize; s++)
{
//to allow scalability (height may be less than tickRange)
int curXValue = tmpSeries->GetXData();
int curYValue = tmpSeries->GetYData();
if(curXValue < minXValue)
minXValue = curXValue;
if(curYValue < minYValue)
minYValue = curYValue;
}
}
graphQuadType = 1;
if((minXValue < 0) && (minYValue < 0))
graphQuadType = 4;
if((minXValue < 0) && (minYValue >= 0))
graphQuadType = 2;
if((minXValue >= 0) && (minYValue < 0))
graphQuadType = 3;
}
if((graphType != 2) && (graphType != 32)) //pie & 3d pie
{
inRedraw = FALSE;
}
//draw series data and labels
DrawDataSet(pDC);
//lines below are commented for doing redraw. If you
//want the axis lines on top of the other graph elements
//uncomment this functionality...I may add it back later
//but there were some bugs doing it this way.
//redraw axis lines in case graph elements overwrote the axis lines
// if((graphType != 2) && (graphType != 32) && (graphQuadType > 1)) //pie & 3d pie
// {
// inRedraw = TRUE;
//draw axis lines
// DrawAxis(pDC);
// }
}
void CGraph::AddDataSet(CGraphDataSet* dataSet)
{
if ( dataSet->GetXData() > m_gMaxDataSet.GetXData()) {
m_gMaxDataSet.SetXPosition(dataSet->GetXData());
}
if ( dataSet->GetYData() > m_gMaxDataSet.GetYData()) {
m_gMaxDataSet.SetYPosition(dataSet->GetYData());
}
if ( dataSet->GetXData() < m_gMinDataSet.GetXData()) {
m_gMinDataSet.SetXPosition(dataSet->GetXData());
}
if ( dataSet->GetYData() < m_gMinDataSet.GetYData()) {
m_gMinDataSet.SetYPosition(dataSet->GetYData());
}
graphDataSet->AddTail((CObList *)dataSet);
}
void CGraph::SetGraphTitle(CString title)
{
graphTitle = title;
}
void CGraph::DrawDataSet(CDC* pDC)
{
//points will now plot as ((value * scale) + apex point)
COLORREF barColor;
barColor = BLACK;
CBrush brush (barColor);
CBrush* pOldBrush;
pOldBrush = pDC->SelectObject(&brush);
POSITION pos;
CGraphDataSet* tmpSeries;
for(pos = graphDataSet->GetHeadPosition(); pos != NULL; graphDataSet->GetNext(pos))
{
// tmpSeries = (CGraphDataSet*)graphDataSet->GetAt(pos);
//multiply each value by 1.00 to force result into a double value, and therefore
//make it more accurate in it's plot location.
// tickXLocation = (int)(tmpSeries->GetXDataValue());
// tickYLocation = (int)(tmpSeries->GetYDataValue());
//draw ellipse...
// pDC->Ellipse(tickXLocation - 3, tickYLocation - 3,
// tickXLocation + 3, tickYLocation + 3);
}
pDC->SelectObject(pOldBrush);
}
void CGraph::RemoveDataSet()
{
POSITION pos;
CGraphDataSet* pSeries;
pos = graphDataSet->GetHeadPosition();
while(pos != NULL)
{
pSeries = (CGraphDataSet*) graphDataSet->GetAt( pos );
graphDataSet->RemoveAt(pos);
delete pSeries;
}
graphDataSet->RemoveAll();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -