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 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


Comments

  • It's horrible horrible thing.

    Posted by Legacy on 09/02/2003 12:00am

    Originally posted by: neanderthal

    I have no idea why this demo, if it is, could be published in this site.

    Reply
  • very nice, but whats about two graphs in one parent wnd ...

    Posted by Legacy on 04/09/2003 12:00am

    Originally posted by: rebu0815

    i need to have two of those wonderful graph-objects in on parent window (i.e. CFormView). if i do so, only one graph is drawn, the other one does only show this nice grid but no added points !

    Reply
  • Excellent work!

    Posted by Legacy on 10/25/2002 12:00am

    Originally posted by: WM

    Just what I was looking for. Well done!

    WM

    Reply
  • Memory Leak...

    Posted by Legacy on 02/13/2002 12:00am

    Originally posted by: Newton

    Great example app!
    
    

    I did notice one of those little memory leaks do to the construction of the CGraph object.

    Simple fix would be to call delete in the destructor of the MainFrame and set the pointer to [0], as well as to check for allocated memory before new allocation occurs, thus eliminating any leakage. *Make certain to set the m_Graph pointer to [0] in the MainFrame's constructor as well.

    ****It may be somewhat of a kludge, but it works.

    void CMainFrame::OnViewGraph()
    {
    if(m_wndView.m_Graph)
    {
    delete m_wndView.m_Graph;
    m_wndView.m_Graph = 0;
    }

    m_wndView.m_Graph = new CGraph(3, 3, 400, 400);
    }

    • its gr8 but...

      Posted by LiteUponLite on 08/18/2004 04:23pm

      i waz looking for a graphing utility too and this is great, but you can only control the data of y value and its not displayed on the axis either..am i wrong, or is there a way

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds