Click here to Skip to main content
15,887,267 members
Articles / Desktop Programming / MFC
Article

Using the MS Chart control in VC++

Rate me:
Please Sign up or sign in to vote.
4.45/5 (17 votes)
14 Jun 2003 257.2K   6.5K   74   40
Setting data and labels on the MS Chart control using VC++

Introduction

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.

Image 1

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.

C++
//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,
                            // 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.
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 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
    ::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:

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

License

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


Written By
Architect
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!!

Comments and Discussions

 
Questionwhy i can not find Microsoft Chart Control to add as refrence Pin
Member 135073176-Nov-17 21:11
Member 135073176-Nov-17 21:11 
GeneralMore sample Pin
Ankata10-Jun-09 14:04
Ankata10-Jun-09 14:04 
GeneralMay i Have this in C# Pin
Irfan Kothari29-Dec-06 20:12
Irfan Kothari29-Dec-06 20:12 
GeneralRe: May i Have this in C# Pin
emadns26-Aug-08 6:43
emadns26-Aug-08 6:43 
QuestionHow to add only one vertical grid line in the plot? [modified] Pin
Youn-seong Cho28-Nov-06 19:40
Youn-seong Cho28-Nov-06 19:40 
Questiondependencies? Pin
dkvp10-Oct-06 23:23
dkvp10-Oct-06 23:23 
AnswerRe: dependencies? Pin
Ananth.tm2-Nov-06 17:07
Ananth.tm2-Nov-06 17:07 
GeneralRe: dependencies? Pin
Ananth.tm3-Nov-06 8:55
Ananth.tm3-Nov-06 8:55 
GeneralMSChart control display problem!! Pin
shinyhui13-Jul-06 20:59
shinyhui13-Jul-06 20:59 
GeneralRe: MSChart control display problem!! Pin
Ananth.tm2-Nov-06 17:38
Ananth.tm2-Nov-06 17:38 
QuestionCustom Text Over Bars?? Pin
Christopher Stratmann31-Mar-06 2:47
Christopher Stratmann31-Mar-06 2:47 
have been working with the Active X MSCharting class for about a week or so and I have come up with the issue of putting custom labels within the chart. I am using Microsoft Visual Studio 6.0 on Windows 2000 SP4. So far this is what I have found....

CVcSeriesCollection sc = m_chart.GetPlot().GetSeriesCollection();
// 2 will select the second series.
CVcSeries s = sc.GetItem(2);
CVcDataPoints dps = s.GetDataPoints();
// -1 will select all the datapoints in the series
CVcDataPoint dp = dps.GetItem(-1);
// 1 = VtChLabelLocationTypeAbovePoint which places the values ontop the bars.
dp.GetDataPointLabel().SetLocationType(1);
This works perfectly for displaying the values of all the datapoints above the bars in a selected series.


Now the Problem
I would like to put a custom label on top a datapoint like "8 hours". I have tried the following but get an error when calling dps.GetItem(1);

CVcSeriesCollection sc = m_chart.GetPlot().GetSeriesCollection();
CVcSeries s = sc.GetItem(2);
CVcDataPoints dps = s.GetDataPoints();
long l_Item_Count = dps.GetCount();
CVcDataPoint dp = dps.GetItem(1);
dp.GetDataPointLabel().SetLocationType(1);
dp.GetDataPointLabel().SetText("8 Hours");


Other Information
- There is a dp.GetDataPointLabel().SetCustom(bool) function but I have no idea how to use it. I can set this function to true after calling dps.GetItem(-1) but it seems to do nothing.
- The error that I get says "Index given is out of bounds" but the value that gets stored into l_Item_count is 5.
- This is how I am adding data to the chart. m_chart.GetDataGrid().SetData(ROW_NUM,COL_NUM,d_va lue,0); which I am pretty sure is correct.
- This is what is in my Visual Studios Output box after the exception is thrown..."First-chance exception in Testd.exe (KERNEL32.DLL): 0xE06D7363: Microsoft C++ Exception. Warning: Uncaught exception in WindowProc (returning 1)."
- I am not adding or modifing the columns or rows in the bar chart at all. I am just using the 5 generic rows and 4 generic columns built by the chart class.
- I can change the labels with the following function: m_chart.GetDataGrid().SetRowLabel(ROW_NUM,1,"RowA" );. The 1 is an index which does something but I dont know what. I have tried to set this value to 2 and 3 but I get errors. This may play a part in this problem I dont really know.



Thanks for taking the time to read my problem. Please responed with any information.
Thanks again.


Chris
Generaltext over bars Pin
Marc Soleda2-Aug-05 4:50
Marc Soleda2-Aug-05 4:50 
AnswerRe: text over bars Pin
Christopher Stratmann31-Mar-06 2:43
Christopher Stratmann31-Mar-06 2:43 
Generalbar colors Pin
Marc Soleda19-Jul-05 22:18
Marc Soleda19-Jul-05 22:18 
GeneralRe: bar colors Pin
Marc Soleda20-Jul-05 5:32
Marc Soleda20-Jul-05 5:32 
GeneralRe: bar colors Pin
Christopher Stratmann31-Mar-06 2:52
Christopher Stratmann31-Mar-06 2:52 
Generalflickering problem Pin
sayup30-Jun-05 20:47
sayup30-Jun-05 20:47 
Generaladding resource file of thew control Pin
sayup30-Jun-05 20:43
sayup30-Jun-05 20:43 
Generalsimple plotting of x,y values Pin
Krishna Seetharaman6-Mar-05 19:14
Krishna Seetharaman6-Mar-05 19:14 
QuestionHow import Ms Chart to my project? Pin
Member 149649120-Feb-05 21:08
Member 149649120-Feb-05 21:08 
AnswerRe: How import Ms Chart to my project? Pin
Krishna Seetharaman6-Mar-05 19:18
Krishna Seetharaman6-Mar-05 19:18 
GeneralRe: How import Ms Chart to my project? Pin
Member 14964917-Mar-05 19:44
Member 14964917-Mar-05 19:44 
QuestionHow to hide grid line in the plot Pin
joyvinod26-Apr-04 2:19
joyvinod26-Apr-04 2:19 
AnswerRe: How to hide grid line in the plot Pin
brngupta2-Nov-04 20:40
brngupta2-Nov-04 20:40 
GeneralRe: How to hide grid line in the plot Pin
Ananth.tm13-Oct-06 9:01
Ananth.tm13-Oct-06 9:01 

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.