Click here to Skip to main content
15,885,141 members
Articles / Desktop Programming / MFC
Article

Passing an Array of Values to the Visual C++ MSChart OCX

Rate me:
Please Sign up or sign in to vote.
4.54/5 (10 votes)
27 Nov 2001 194K   39   27
A step by step guide to setting the values in the MSChart control.

Introduction

This is a step by step guide to setting the values in the MSChart control using an array of values.

Step 1 : Creating the Project

Start Visual C++ en create a simple dialog based application labeled "Graph"

Step 2 : Add the MSChart OCX to Your Project

Select "project menu" option and select "Components and contols" and then choose the MSChart component en click "add"

Image 1

Step 3 : Add the MSChart OCX to Your Dialog

Select resources view tab en open the main dialog (It’s a simple dialog based application). Drop the OCX on your dialog.

Image 2

Now, label your Chart "IDC_MSCAHRT1"

Image 3

Now, choose menu option "Classwizard" to create a member variable of your chart labeled "m_Chart"

Image 4

Step 4: Add the Code

Now add a button labeled "Go" to your dialog. Double click it to edit the code and add the following code in the On_Go function:
COleSafeArray saRet;

DWORD numElements[] = {10, 10}; // 10x10

// Create the safe-array...

saRet.Create(VT_R8, 2, numElements);

// Initialize it with values...

long index[2];

for(index[0]=0; index[0]<10; index[0]++) {
 for(index[1]=0; index[1]<10; index[1]++) {
  double val = index[0] + index[1]*10;
  saRet.PutElement(index, &val);
 }
}

// Return the safe-array encapsulated in a VARIANT...

m_Chart.SetChartData(saRet.Detach());

m_Chart.Refresh;          

Step 5: Building and Running the Application

Build and execute your app, then click the "Go" button. Here is the result:

Image 5

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
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to speed up display of MSchart graph using detach Pin
LRatna7-Jun-07 21:55
LRatna7-Jun-07 21:55 
QuestionIs Detach() really needed ? Pin
Yoavip18-Apr-07 2:23
Yoavip18-Apr-07 2:23 
GeneralMSChart control display problem!! Pin
shinyhui13-Jul-06 18:51
shinyhui13-Jul-06 18:51 
GeneralRe: MSChart control display problem!! Pin
Ananth.tm3-Nov-06 8:47
Ananth.tm3-Nov-06 8:47 
Do not use RowCount or LabelCount, let MSChart do that automatically based on data array passed and you wont have the problem
GeneralDialog wont open anymore Pin
Astrona20-Apr-05 15:15
Astrona20-Apr-05 15:15 
GeneralRe: Dialog wont open anymore Pin
Anonymous22-Sep-05 23:13
Anonymous22-Sep-05 23:13 
GeneralRe: Dialog wont open anymore Pin
ptitanic0713-Sep-06 5:48
ptitanic0713-Sep-06 5:48 
GeneralSimple plotting of x-y values Pin
Krishna Seetharaman6-Mar-05 17:39
Krishna Seetharaman6-Mar-05 17:39 
Question.dll or .ocx needed??? Pin
DELCOURT5-Feb-04 22:46
DELCOURT5-Feb-04 22:46 
AnswerRe: .dll or .ocx needed??? Pin
vishuvp2-Mar-04 9:18
vishuvp2-Mar-04 9:18 
GeneralRe: .dll or .ocx needed??? Pin
JabraJabra9-Aug-04 7:20
JabraJabra9-Aug-04 7:20 
GeneralRe: .dll or .ocx needed??? Pin
Member 5281554-Mar-05 22:51
Member 5281554-Mar-05 22:51 
QuestionHow to Draw 3D chart in VB.net Pin
xprtguro8-Dec-03 0:12
xprtguro8-Dec-03 0:12 
Generalonline drawing on MSCHART Pin
annona8-Apr-03 23:50
annona8-Apr-03 23:50 
GeneralHi there a prob in compilation Pin
cemlouis16-Mar-03 5:40
cemlouis16-Mar-03 5:40 
GeneralRe: Hi there a prob in compilation Pin
mussashi30-Jun-04 1:10
mussashi30-Jun-04 1:10 
GeneralRe: Hi there a prob in compilation Pin
User 15933135-Feb-05 5:45
User 15933135-Feb-05 5:45 
QuestionHow can I print this Mschart Pin
Anonymous3-Dec-02 1:41
Anonymous3-Dec-02 1:41 
AnswerRe: How can I print this Mschart Pin
Anonymous3-Dec-02 20:55
Anonymous3-Dec-02 20:55 
GeneralRe: How can I print this Mschart Pin
Qadddd4-Dec-02 11:32
Qadddd4-Dec-02 11:32 
GeneralRe: How can I print this Mschart Pin
Qadddd16-Dec-02 2:36
Qadddd16-Dec-02 2:36 
QuestionTo display the value in tooltips by placing mouse at a position? Pin
vcLearner19-May-02 20:31
vcLearner19-May-02 20:31 
GeneralXY plot Pin
12-Dec-01 2:35
suss12-Dec-01 2:35 
GeneralRe: XY plot Pin
13-Feb-02 8:52
suss13-Feb-02 8:52 
GeneralRe: XY plot Pin
Krishna Seetharaman6-Mar-05 17:29
Krishna Seetharaman6-Mar-05 17:29 

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.