Click here to Skip to main content
12,293,917 members (61,757 online)
Click here to Skip to main content
Add your own
alternative version


73 bookmarked

Using the MS Chart control in VC++

, 14 Jun 2003
Rate this:
Please Sign up or sign in to vote.
Setting data and labels on the MS Chart control using VC++
<!-- Add the rest of your HTML here -->


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

First 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 a 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 on code project.

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,
                            // ie 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 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;

    // make sure this cannot be converted to a valid number like "54" and
    // is a valid string like "John"
    bstr = filedetailsstruct.login.AllocSysString(); // Row label
    saRet.PutElement(index, bstr);

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

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

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

Okay now to label the columns:


Thats it folks.



Developed On Windows 2000 Server using VC++ ver 6


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alex C. Punnen
India India
Hi everyone!! Sometimes I get real stumped with some concepts and that gets me excited.So I say - hey let me explore - and there I go!! .Currently I am working as software engineer for Nokia Siemens Networks, Bangalore -India.My dream subject - Physics!!

You may also be interested in...

Comments and Discussions

Generaltext over bars Pin
marcdev2-Aug-05 4:50
membermarcdev2-Aug-05 4:50 
AnswerRe: text over bars Pin
chris17531-Mar-06 2:43
memberchris17531-Mar-06 2:43 
Generalbar colors Pin
marcdev19-Jul-05 22:18
membermarcdev19-Jul-05 22:18 
GeneralRe: bar colors Pin
marcdev20-Jul-05 5:32
membermarcdev20-Jul-05 5:32 
GeneralRe: bar colors Pin
chris17531-Mar-06 2:52
memberchris17531-Mar-06 2:52 
Generalflickering problem Pin
sayup30-Jun-05 20:47
membersayup30-Jun-05 20:47 
Generaladding resource file of thew control Pin
sayup30-Jun-05 20:43
membersayup30-Jun-05 20:43 
Generalsimple plotting of x,y values Pin
Krishna Seetharaman6-Mar-05 19:14
memberKrishna Seetharaman6-Mar-05 19:14 
QuestionHow import Ms Chart to my project? Pin
mw81120-Feb-05 21:08
membermw81120-Feb-05 21:08 
AnswerRe: How import Ms Chart to my project? Pin
Krishna Seetharaman6-Mar-05 19:18
memberKrishna Seetharaman6-Mar-05 19:18 
GeneralRe: How import Ms Chart to my project? Pin
mw8117-Mar-05 19:44
membermw8117-Mar-05 19:44 
QuestionHow to hide grid line in the plot Pin
vinubhai26-Apr-04 2:19
membervinubhai26-Apr-04 2:19 
AnswerRe: How to hide grid line in the plot Pin
brngupta2-Nov-04 20:40
memberbrngupta2-Nov-04 20:40 
GeneralRe: How to hide grid line in the plot Pin
Ananth.tm13-Oct-06 9:01
memberAnanth.tm13-Oct-06 9:01 
QuestionUse MS Chart Control for free? Pin
marathom23-Apr-04 3:32
membermarathom23-Apr-04 3:32 
AnswerRe: Use MS Chart Control for free? Pin
brngupta2-Nov-04 20:33
memberbrngupta2-Nov-04 20:33 
Generalit's a hag Pin
timepalette19-Feb-04 22:51
membertimepalette19-Feb-04 22:51 
GeneralRe: it's a hag Pin
brngupta2-Nov-04 20:36
memberbrngupta2-Nov-04 20:36 
Generalcolors of the Graphs Pin
Vasudevacharyulu27-Aug-03 1:56
memberVasudevacharyulu27-Aug-03 1:56 
GeneralRe: colors of the Graphs Pin
brngupta2-Nov-04 20:44
memberbrngupta2-Nov-04 20:44 
GeneralRe: colors of the Graphs Pin
chris17531-Mar-06 2:50
memberchris17531-Mar-06 2:50 
GeneralX and Y labels; Zoom Pin
djteambse6-Aug-03 0:20
memberdjteambse6-Aug-03 0:20 
GeneralRe: X and Y labels; Zoom Pin
Krishna Seetharaman6-Mar-05 19:12
memberKrishna Seetharaman6-Mar-05 19:12 
GeneralRe: X and Y labels; Zoom Pin
Ananth.tm13-Oct-06 9:05
memberAnanth.tm13-Oct-06 9:05 
GeneralOffice version Pin
jbradync23-Jun-03 6:17
memberjbradync23-Jun-03 6:17 
GeneralRe: Office version Pin
Astrona20-Apr-05 11:41
memberAstrona20-Apr-05 11:41 
Questioncontrol is missing - where can i get it from ? Pin
csc15-Jun-03 20:21
membercsc15-Jun-03 20:21 
AnswerRe: control is missing - where can i get it from ? Pin
jbradync16-Jun-03 1:24
memberjbradync16-Jun-03 1:24 
GeneralRe: control is missing - where can i get it from ? Pin
csc16-Jun-03 3:19
membercsc16-Jun-03 3:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 15 Jun 2003
Article Copyright 2003 by Alex C. Punnen
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid