Graph Animation | CodeGuru

Graph Animation

Environment: VC6 WIN 9x/2k For some time I’ve been wanting to imitate the ‘CPU Usage’ Graph in win 2k, so I finally did. CGraph is a very simple class for drawing animated graph. The interface includes a few simply functions: CGraph(int x, int y, int width, int height); Constructor which defines the size of the […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 27, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC6 WIN 9x/2k

For some time I’ve been wanting to imitate the ‘CPU Usage’ Graph in win 2k, so I finally did.


CGraph is a very simple class for drawing animated graph.

The interface includes a few simply functions:



CGraph(int x, int y, int width, int height);

Constructor which defines the size of the graph.

void SetLineColor(COLORREF cl);


Sets the color of graph line}

void SetLinesColor(COLORREF cl);


Sets the color of the axes

void SetRange(int start, int end);


Sets the range values of the graph}

void SetBK(COLORREF cl);


Sets the background color of the graph}

void DrawGraph(CDC *pDC);


Draws the graph

BOOL AddPoint(CDC *pDC, int y);


Adds a point with value ‘y’ to the graph and draws it

Returns ‘TRUE’ if the point is llegal, ‘FALSE’ if it’s not.

int GetRange()


Get the range of values in the graph

//The tested app is a non doc/view MFC App
CGraph *m_Graph;
// m_Graph is a member variable of m_wndView
// which is a member of CMainFrame
void CMainFrame::OnViewGraph()
{
  CClientDC dc(m_wndView.FromHandle(m_wndView.GetSafeHwnd()));
  m_wndView.m_Graph = new CGraph(3, 3, 400, 400);
  m_wndView.m_Graph->SetBK(RGB(0,0,0));
  m_wndView.m_Graph->SetRange(0, 100);
  m_wndView.m_Graph->SetLinesColor(RGB(0, 255, 0));
  m_wndView.m_Graph->SetLineColor(RGB(255,0,0));
  m_wndView.m_Graph->DrawGraph(&dc);
  m_wndView.m_Graph->AddPoint(&dc, 40);
  m_wndView.m_Graph->AddPoint(&dc, 80);
  m_wndView.m_Graph->AddPoint(&dc, 23);
  m_wndView.m_Graph->AddPoint(&dc, 99);
  m_wndView.m_Graph->AddPoint(&dc, 40);
  m_wndView.m_Graph->AddPoint(&dc, 80);
  m_wndView.m_Graph->AddPoint(&dc, 23);
  m_wndView.m_Graph->AddPoint(&dc, 99);
  m_wndView.m_Graph->AddPoint(&dc, 40);
  m_wndView.m_Graph->AddPoint(&dc, 80);
  m_wndView.m_Graph->AddPoint(&dc, 23);
  m_wndView.m_Graph->AddPoint(&dc, 99);
  m_wndView.m_Graph->AddPoint(&dc, 40);
  m_wndView.m_Graph->AddPoint(&dc, 80);
  m_wndView.m_Graph->AddPoint(&dc, 23);
  m_wndView.m_Graph->AddPoint(&dc, 99);
  //Added the exact amount of points to fill the graph
  SetTimer(1, 500, 0);
  //Starts a timer which adds a random point every 500 ms.
}
void CMainFrame::OnTimer(UINT nIDEvent)
{
  if (nIDEvent == 1)
  {
    CClientDC dc(m_wndView.FromHandle(m_wndView.GetSafeHwnd()));
    int rnd = rand() % m_wndView.m_Graph->GetRange();
    m_wndView.m_Graph->AddPoint(&dc, rnd);
  }
  CFrameWnd::OnTimer(nIDEvent);
}

Downloads

Download demo project – 37.6 Kb

Download source – 1.94 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.