VB6.0 - Arrays & MSChart Control 6.0 - Using 2 Arrays to input to Chart?

Ono
Ono used Ask the Experts™
on
Hi, I have 2 questions here (since they're related). Basically I will show you the coding sample and briefly explain them.

(1) === Example ===

Public value(1 to 40, 1 to 2) as Double

Public Sub PlotGraph()
    With MSChart1
        .Plot.UniformAxis = False
        .chartType = VtChChartType2dXY
        .ChartData = value
    End With
End Sub
=== === === ===

The code above is use for ploting a single set of data (stored in array called 'value') on the MSChart. However, suppose I have 2 set of data, and I want these 2 set of data to be on the same MSChart, how am I suppose to go about doing it? Do i need 2 arrays? or just 1 array will do?


(2)=== Example(I) ===
Public Const NO_OF_POINTS = 40
Public value(1 to NO_OF_POINTS, 1 to 2) as Double

==================
I have an array, but then I do not want to define the size @ the start, I've try using Integer to replace the Const but it wouldn't work (see below). What could I do?

===EXAMPLE(II)===
Public NO_OF_POINTS As Integer
Public value(1 to NO_OF_POINTS, 1 to 2) as Double
=================

Sorry tht I'm having trouble with my basics, but all comments are definately appreciated. Thanks
Ono.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
To dynamically size the elements in an array, use the redim preserve.  In an n dimensional array, the only element you can redimension is the last element, so you would need to flip the dimensions you currently have like:
Public value(1 to 40, 1 to 2) as Double becomes
Public value(1 to 2, 1 to 40) as Double

Now, how to make the array dynamic:
I would usually define the array without dimensions originally such as
Public value() as Double

Then as I cycle through, building the array, I perform the following

Private value() As Double

Private Sub Command1_Click()
    For n = 1 To 40 'or whatever value you want
       
        ReDim Preserve value(1 To 2, 1 To n)
        value(1, n) = n
        value(2, n) = n + 10
       
    Next n
   
    Debug.Print value(1, 1); value(2, 1)
    Debug.Print value(1, 2); value(2, 2)
   
End Sub

Ono

Author

Commented:
Thanks twalgrave, but whud if I need to use X (in (X,Y)) ? which means is there any other way, after i input the values into

value(1 to 2, 1 to 40) to become value(1 to 40, 1 to 2)

In simplest terms, to flip back the dimensions. =)
You could take a snapshot of the array at the time and process it through to generate another array with the elements reversed like:

Private value() As Double
Private valueFlipped() As Double

Private Sub Command1_Click()
   For n = 1 To 40 'or whatever value you want
       
       ReDim Preserve value(1 To 2, 1 To n)
       value(1, n) = n
       value(2, n) = n + 10
       
   Next n
   
   Debug.Print value(1, 1); value(2, 1)
   Debug.Print value(1, 2); value(2, 2)
   
End Sub

'Now flip the dimensions
Private Sub Command2_Click()
   
    Dim lArrayLoop As Long
    ReDim valueFlipped(1 To UBound(value, 2), 1 To UBound(value, 1))
   
    For lArrayLoop = LBound(value, 2) To UBound(value, 2)
        valueFlipped(lArrayLoop, 1) = value(1, lArrayLoop)
        valueFlipped(lArrayLoop, 2) = value(2, lArrayLoop)
       
    Next
   Debug.Print valueFlipped(1, 1); valueFlipped(1, 2)
   Debug.Print valueFlipped(2, 1); valueFlipped(2, 2)
   Debug.Print valueFlipped(3, 1); valueFlipped(3, 2)
   
End Sub


Ono

Author

Commented:
Hy twalgrave,

thanks alot of your help, I didn't try out your latest comment but then discovered another thing. That is to change the 1st dimension in the array. Here's how:

Public Value() As Double

Private Sub Command1()
Dim Number_Of_Points As Integer
Number_Of_Points = 40

Redim Preserve Value(1 to Number_Of_Points, 1 to 2)

For n = 1 To 40 'or whatever value you want      
   value(n, 1) = n
   value(n, 2) = n + 10
Next n

End Sub

So as long as you declare the value of "Number_Of_Points" 1st, you can change the dimension accordingly. Anyway, I'll accept your 1st comment as the answer. Thanks alot.
Ono

Author

Commented:
Hey twalgrave,

thanks alot of your help, I didn't try out your latest comment but then discovered another thing. That is to change the 1st dimension in the array. Here's how:

Public Value() As Double

Private Sub Command1()
Dim Number_Of_Points As Integer
Number_Of_Points = 40

Redim Preserve Value(1 to Number_Of_Points, 1 to 2)

For n = 1 To 40 'or whatever value you want      
  value(n, 1) = n
  value(n, 2) = n + 10
Next n

End Sub

So as long as you declare the value of "Number_Of_Points" 1st, you can change the dimension accordingly. Anyway, I'll accept your 1st comment as the answer. Thanks alot.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial