|
-
Aug 2nd, 2022, 12:49 AM
#1
Thread Starter
Lively Member
How can I input an existing data series into a sub that controls MSCHART
I want to use MSCHART with VB6 to be able to plot two lines on the same chart. The example I think will work for me is in this thread on the ninth post:
https://www.vbforums.com/showthread....ination-needed
But I have hit a snag. The example generates the data matrices in the code that controls the MSCHART. In my case, my data series exists already in a sub in a different module from the form that contains the MSCHART. How can I make my matrix variables replace the locally generated series (in Sub Chartit)?
My data looks like this. There are two series in two variables.
n ChartPointsAc ........ n ChartPointsRe
1 5.62212463652474 1 10.7578753634753
2 6.50250842134825 2 9.87749157865175
3 6.92226725141999 3 9.45773274858001
4 7.19301243651532 4 9.18698756348468
5 7.39461007232716 5 8.98538992767284
6 7.55893544986895 6 8.82106455013104
7 7.70250069151093 7 8.67749930848907
8 7.83646642657158 8 8.54353357342841
9 7.97283240645675 9 8.40716759354325
10 8.19 10
I want to send the matrices ChartPointsAc and ChartPointsRe which are located in: Module=Table4, Sub=TTABK4
The example data is in Module=frmDRchart, Sub=Private Sub ChartIt(CurSeries As Integer) (The integer indicates which series it is.
Sub Chartit is referenced in form_load twice, ie: Chartit 1, and Chartit 2, but that does not pass data.
How can I replace the example data series' created in Sub Chartit with my 2 series'?
Last edited by StanH; Aug 2nd, 2022 at 12:54 AM.
-
Aug 2nd, 2022, 11:18 AM
#2
Re: How can I input an existing data series into a sub that controls MSCHART
You can either use the 2 dimensional Variant array property ChartData to provide data to MSChart, or you can use the more laborious but fine-grained control offered by the DataGrid object property.
Code:
Option Explicit
Private Sub Form_Load()
'First dimension is Row, second is Column (data series):
Dim Data(1 To 5, 1 To 2) As Variant
Dim Series As MSChart20Lib.Series
'We *could* add a 3rd column and assign String values for each Row as the first
'column. Those would be used as Row Label values instead of the default R1, R2,
'etc. that we get here:
Data(1, 1) = 1.2: Data(1, 2) = 2.1
Data(2, 1) = 2.3: Data(2, 2) = 2.005
Data(3, 1) = 2.1: Data(3, 2) = 1.8
Data(4, 1) = 3.95: Data(4, 2) = 0.8
Data(5, 1) = 0.5: Data(5, 2) = 2.3
BackColor = vbWhite
With MSChart1
.chartType = VtChChartType2dLine
.RandomFill = False
'Assign the data:
.ChartData = Data
'That established our series count as well as the data, so now do some formatting:
With .Plot
.Axis(VtChAxisIdX).CategoryScale.LabelTick = True
.Axis(VtChAxisIdY2).AxisScale.Hide = True
For Each Series In .SeriesCollection
With Series
.Pen.Width = 30 'Twips, not Points as the docs say.
With .SeriesMarker
.Show = True
.Auto = False
End With
With .DataPoints.Item(-1).Marker
.Style = VtMarkerStyleFilledCircle
.Size = 150 'Twips, not Points.
End With
End With
Next
End With
End With
End Sub
Private Sub Form_Resize()
If WindowState <> vbMinimized Then
MSChart1.Move 0, 0, ScaleWidth, ScaleHeight
End If
End Sub
MSChart has a complex object model, but getting data into it is probably the simplest thing about it.
-
Aug 2nd, 2022, 03:34 PM
#3
Thread Starter
Lively Member
Re: How can I input an existing data series into a sub that controls MSCHART
In that example the data is created in the Form_load on the form with the MSCHART. How can I make my variant data array that is in another sub&module accessible from there?
My problem is to be able to declare the right scope to get an array of data created in a different module as either a double or a variant matrix into the chart form that the chart is in -- either the form_load or the sub that runs the chart.
Last edited by StanH; Aug 2nd, 2022 at 04:50 PM.
-
Aug 2nd, 2022, 06:43 PM
#4
Thread Starter
Lively Member
Re: How can I input an existing data series into a sub that controls MSCHART
I tried to access the data series double matrix by putting it in the module level of where it was made. Then from the chart form I tried to reference it. I could see it but not access it.
-
Aug 2nd, 2022, 08:07 PM
#5
Re: How can I input an existing data series into a sub that controls MSCHART
There is nothing special about MSChart in this regard. Wherever you assign the array to ChartData both things must be in scope.
Code external to the Form might call a public procedure in the Form, passing the array as a parameter. Or less gracefully it might grab the MSChart instance and assign to ChartData:
Code:
Form1.MsChart1.ChartData = TwoDimVariantArray
Form1 is public, as is its MSChart1 control.
It's all very straightforward. I don't see any mystery here.
-
Aug 2nd, 2022, 08:56 PM
#6
Thread Starter
Lively Member
Re: How can I input an existing data series into a sub that controls MSCHART
dilettante, What is the reason you use variant arrays?
Are they necessary?
Why wouldn't double work just as well?
-
Aug 3rd, 2022, 01:02 AM
#7
Thread Starter
Lively Member
Re: How can I input an existing data series into a sub that controls MSCHART
I have succeeded in getting the two arrays into the charting code and verifyied that they contain the correct data.
It still isn't working though.
1 I do not get a chart on the screen.
2 When the program stops - the one that initiates the chart - the IDE indicates it is still running. I stop it with [Run][End] with the run menu
-
Aug 3rd, 2022, 06:03 AM
#8
Re: How can I input an existing data series into a sub that controls MSCHART
I used a 2-D Variant array because that's what MSChart requires.
The array holds both Row and Column data, so two dimensions. The "cells" can be String in the 1st Column as Row labels (optional) while it can be Integer or Long or Single or Double for the remaining data Columns.
This is described in the documentation.
How can the program "stop" but "keep running?"
It sounds like you have some sort of problem with the flow of control. VB is not QBasic, all of your code must be in procedures that do a job like handling an event or doing initial setup (Sub Main). These must quickly return control to the message loop by exiting back to the runtime.
-
Aug 3rd, 2022, 05:28 PM
#9
Thread Starter
Lively Member
Re: How can I input an existing data series into a sub that controls MSCHART
 Originally Posted by dilettante
I used a 2-D Variant array because that's what MSChart requires.
The array holds both Row and Column data, so two dimensions. The "cells" can be String in the 1st Column as Row labels (optional) while it can be Integer or Long or Single or Double for the remaining data Columns.
I have converted my series matrices to Variant. I verified that the data get to chart code correctly.
I also made two functions in the chart module - one for each series: Data1 and Data2:
Function Data1()
Data1 = TABLE4.ChartPointsAc
End Function
Function Data2()
Data2 = TABLE4.ChartPointsRe
End Function
TABLE4 is the module where the sub is that creates the matrices. They are declared in that module at the module level only.
The functions are used in the charting module like: "ChartPoints = Data1".
ChartPoints is declared in Sub Form_load and in Private Sub ChartIt(). It is also declared at module level I'm not sure the module level declaration is used. It wasn't sufficient to use in the Subs.
This is described in the documentation.
Perhaps I should follow the example you posted as second post in this thread.
I could also use a link to help me understand how MSChart works
"How can the program "stop" but "keep running?"
It sounds like you have some sort of problem with the flow of control. VB is not QBasic, all of your code must be in procedures that do a job like handling an event or doing initial setup (Sub Main). These must quickly return control to the message loop by exiting back to the runtime.
I will continue to study that. I expected the chart to show following the execution of "Load frmDRchart" but it doesn't. I traced the execution through the "Private Sub MDIForm_Unload(Cancel As Integer)
There is also a "frmReportHolder2.frm" that holds some test report on the MDIform. I have to figure that out because I don't remember how I set it up 35 to 45 years ago. I think it is a utility I purchased.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|