-
Real-time graph using Mschart
Hi guys,
I am trying to plot a real time graph using Mschart control. I have an array Tot(). Each time Tot() gets new value the Mschart is plotted. My problem is instead of showing the graph, the Mschart control is just flickering. can anyone pl help me.
here is the code.
Private Sub Timer1_Timer()
ReDim Preserve Tot(cnt)
If (MouseOn = True) Then
Tot(cnt) = 10
Else
Tot(cnt) = 0
End If
With MSChart1
.chartType = VtChChartType2dLine
.ShowLegend = True
.ChartData = Tot
End With
cnt = cnt + 1
End Sub
PS: MouseOn is just a boolean variable which is made true or false by some other even in my application.
-
Hi,
Try using the Refresh or Repaint method at the end of the sub...
Bernie
-
-
-
At what interval do you update the chart (what is the timer set to)?
Bernie
-
did you set the Repaint property (allow repaint when data is changed) to True?
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
no..mstraf it did not work. I included repaint in "with" statement.
The timer control interval is 100ms.
-
100 ms?? Aren't you saddling the computer i bit too much? Try to slow the thing down more than a bit and see what's happening...
Bernie
-
it works with me... I added a mschar and a timer to a new project, leaving all the default.
Marco
Code:
Option Explicit
Private arrData() As Double
Private Sub Form_Load()
Dim k As Long
ReDim arrData(9)
For k = 0 To 9
arrData(k) = k + 1
Next
MSChart1.ChartData = arrData
Timer1.Interval = 100
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Timer1.Enabled = False
ReDim Preserve arrData(UBound(arrData) + 1)
arrData(UBound(arrData)) = 10 * Rnd
MSChart1.ChartData = arrData
Timer1.Enabled = True
End Sub
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
I think 100ms is not too much for the computer. However, I changed it to 1sec also, and the problem still persists. I think there is a problem in the way I am using MSchart.
-
Thanks a lot Mstraf..it works for me too. Could you suggest me a book where i can get more information about Mschart control.
-
all I know about the mchart (and it is not much) I read in the MSDN documentation (that is not much indeed) and on the late VBPJ. Try
http://www.fawcette.com/archives/magazines/vsm/
and do a search for mschart. The article I remember is in the October 2000 issue.
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
A few questions about MSChart
Hello All
I just started my first VB project and run into some similar problems as decribed in that thread. I am not that big of a programmer so I started with a simple task. My little program reads voltage from Keithley model 2000 DMM and plots them on the screen. I did the plotting in PictureBox with the following code inside a for loop (for now I am just reading 2000 data points and plting them)
Code:
Y = (ReadingsArray(i%) - 53000#) / 1000#
Graph.PSet (i%, Y * (Graph.ScaleHeight / 2 ))
and it works but I will have to take care of scaling the Y axis which I beleve is taken care of automatically by MSChart. So I tried the following
Code:
Call ChartGraph.DataGrid.SetData(i,1,i,0)
Call ChartGraph.DataGrid.SetData(i,2,ReadingsArray(i%),0)
The above code takes care of the scaling allright but it shows the graph after all 2000 data points were collected.
So my question is: How do I update the graph after each and every single point?
I tried the code posted by mstraf (as is, not with my data points). It worked but the graph was flickering and I do not want that. Ideally I want the MSChart plot area to be of a fixed width (even when it is empty) and the data beeing plotted as they come. Your help is greatly appriciated.
Marek
-
Instead of redim-ing the array every time, set it to the maximum width, and just set the new points instead of adding them, like
for k = 0 to 9
myarray(lastEntry + k) = newValue(k)
next
lastEntry = lastEntry + 10
chart.chartData = myarray
PS setting the right PictureBox scale, the picture box will do the scaling for you. Say you have to paint 1000 points, with values between -100 and 100:
Private Sub Form_Resize()
Picture1.Scale (0, 100)-(1000, -100)
Picture1.Line (50, -100)-(950, 100)
End Sub
Private Sub Picture1_Paint()
Picture1.Line (50, -100)-(950, 100)
End Sub
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
-
Thank You Marco for your post. I am so new to VB that I had to spend some time figuring out what that Picture1.Scale method was doing. I think that I understand that now. I achived very simmilar effect with Scale.Height and Scale.Width properties of my PictureBox.
Regarding Your first bit of code there. I am not sure what it is doing, i.e. how do You know that I am redim-ing my array? There is a text control on my form in which user specifies how many data point he/she wants to take and that array is reDim-ed once before I start filling it up. Sorry if my not including that bit of code caused that misunderstanding.
I added an extra button to my form for testing purposes and that code inside it:
Code:
Private Sub Test_Click()
Dim newvalue(9) As Double
Dim myarray(109) As Double
lastEntry% = 0
Do While LastEntry < 110
for k = 0 to 9
newvalue#(k) = Rnd * 10
myarray#(LastEntry + k) = newvalue(k)
next
lastEntry = lastEntry + 10
chart.chartData = myarray
Loop
and when I click the button the chart flickers and does not show anything (no points). Could You tell me what am I doing wrong here?
Another question regarding MSChart would be the following: When You place new MSChart on your form it shows some lines. Where are those data point comming from?
Thanks again
Marek
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
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|