Using a MS Chart Control in VC | CodeGuru

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. 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 29, 2003
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: 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.

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;

  SAFEARRAYBOUND sab[2];

  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.
  m_ChartControl.SetShowLegend(TRUE);

  // 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;
  index[0]=1;

  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
  //-----------------------------------------------------
  for(i=m_filedetails.begin();i!=m_filedetails.end();i++)
  {
    filedetailsstruct =(FILEDETAILS)*i;
    index[1]=1;
    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);

      index[1]=2;

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

    index[1]=3;
    bstr = filedetailsstruct.n9002.AllocSysString();
           // Data for column 2
    ::SysFreeString(bstr);
    saRet.PutElement(index, bstr);

    index[1]=4;
    bstr = filedetailsstruct.n9004.AllocSysString();
           // Data for column 3
    ::SysFreeString(bstr);
    saRet.PutElement(index, bstr);

    index[1]=5;
    bstr = filedetailsstruct.nCancel.AllocSysString();
           // Data for column 4
    ::SysFreeString(bstr);
    saRet.PutElement(index, bstr);

    index[0]++;

  }

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

m_ChartControl.SetChartData(saRet.Detach());

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

where

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

Okay, now to label the columns:

  m_ChartControl.SetColumnLabelCount(4);
  m_ChartControl.SetColumn(1);
  m_ChartControl.SetColumnLabel("Monday") ;
  m_ChartControl.SetColumn(2);
  m_ChartControl.SetColumnLabel("Wednesday") ;
  m_ChartControl.SetColumn(3);
  m_ChartControl.SetColumnLabel("Friday") ;
  m_ChartControl.SetColumn(4);
  m_ChartControl.SetColumnLabel("Saturday") ;

Thats it, folks.


Dependencies

MFC42.DLL, MSVCRT.DLL, KERNEL32.DLL, USER32.DLL, ADVAPI32.DLL, OLEAUT32.DLL

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

Downloads


Download source – 113 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.