Using a MS Chart Control in VC

Environment: VC++, charting

The following is the description for using a MS Chart control in VC++. The diagram below will give you an idea of the chart we are going to make.

Click here for a larger image.

Firstly, insert the chart control in your project using Project->Add Reference->Microsoft Chart Control.

Next is populating the chart control. When I was working with Visual Basic, I had used data source to pass data to the chart. But in VC++, I did not get the datasource method in the initial tries. So, I passed the chart data through a COleSafeArray. Thanks to J. L. Colson for illuminating the details in his article.

Here is the code snippet for that:

//First, create a safe array

  COleSafeArray saRet;


  sab[0].cElements = noOfRows;    // give this exactly the number
                                  // of rows you display in your
                                  // chart
  sab[1].cElements = 5;           // number of columns + 1
                                  // (because the first column is
                                  // where we put the row labels -
                                  // in 1.1, 2.1, 3.1, 4,1, etc.

    sab[0].lLbound = sab[1].lLbound = 1;

  // Use the chart as the backdrop of the legend.

  // Create the safe array...

  saRet.Create(VT_BSTR, 2, sab);

  long index[2] = {0,0};         // a 2D graph needs a 2D array as
                                 // an index array
  BSTR bstr;

  FILEDETAILS filedetailsstruct; // this is just a datastructure
                                 // I used, pls see attached code
  CString cstemp;

  //m_filedetails is an STL list of filedetailsstruct

  // in this loop, we populate the safe array
    filedetailsstruct =(FILEDETAILS)*i;
    bstr = filedetailsstruct.login.AllocSysString();  // Row label
    // make sure this cannot be converted to a valid number like
    // "54" and is a valid string like "John"

      saRet.PutElement(index, bstr);


    bstr = filedetailsstruct.n9000.AllocSysString();
           // Data for column 1
    saRet.PutElement(index, bstr);

    bstr = filedetailsstruct.n9002.AllocSysString();
           // Data for column 2
    saRet.PutElement(index, bstr);

    bstr = filedetailsstruct.n9004.AllocSysString();
           // Data for column 3
    saRet.PutElement(index, bstr);

    bstr = filedetailsstruct.nCancel.AllocSysString();
           // Data for column 4
    saRet.PutElement(index, bstr);



// now hand over the safe array to the chart control


Our chart data will look like this:


2,1  2,2  2,3  2,4  2,5

1,1  1,2  1,3  1,4  1,5


1,1 -Row label 1
2,1 -Row Label 2

Okay, now to label the columns:

  m_ChartControl.SetColumnLabel("Monday") ;
  m_ChartControl.SetColumnLabel("Wednesday") ;
  m_ChartControl.SetColumnLabel("Friday") ;
  m_ChartControl.SetColumnLabel("Saturday") ;

Thats it, folks.



Developed on Windows 2000 Server using VC++ ver 6.


Download source - 113 Kb


  • add label over/above column

    Posted by marcdev on 07/26/2005 11:39am

    Is it possible to add labels with the value of each column over (or above) each column? Thanks

    • THANK

      Posted by guoshu on 07/19/2007 04:33am


  • good

    Posted by Legacy on 01/03/2004 12:00am

    Originally posted by: nedtong


  • Thank you!!

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

    Originally posted by: Leegun

    Thank you!!Thank you!!

  • VARIANT Data Fill

    Posted by Legacy on 10/23/2003 12:00am

    Originally posted by: Vicky Vigia

    Can we fill the chart with spreadsheet control's
    VARIANT Range.GetValue()

    Any comments!?

  • Flickering problem

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

    Originally posted by: Arbind Yadav

    Hi ,this MSCHART is nice,but there was one major problems.
    If ,I refresh the MSCHART ,THEN it is flickering.

    I do not know ,how to solve this problems.Plz help me regarding this.

    It is really urgent for me.


  • How are going to use scroll bars in MSChart

    Posted by Legacy on 07/16/2003 12:00am

    Originally posted by: Imran Abdul Baset

    Suppose the data set goes large, how will we use scrolling to make the graph more understandable!!!

  • can not open M2Crypt.h??

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

    Originally posted by: happyisgood2002


    • chart

      Posted by walidz on 05/04/2005 03:32am

      Please java code  chart

  • hey Just what i was looking for! thanks

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: Mark

    hey Just what i was looking for! thanks

  • This is great ..Thanks !!! .. Thanks for additional info.

    Posted by Legacy on 05/30/2003 12:00am

    Originally posted by: Mar

    Thanks... It is really a timely code for me.. I need it..

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

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • The mobile market is white hot. Building a well-crafted product development plan that addresses market research, strategy, design, and development will provide the greatest chance for success. Each phase of an app's lifecycle is critical to the its overall success and feeds into the next step of the process of product development for the app. This white paper examines the five key phases of mobile app creation, one by one, to understand how they work together to help create a successful mobile app.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date