Solved

MSChart not displaying 0 in Y-axis

Posted on 2002-03-07
7
960 Views
Last Modified: 2008-02-07
I have an MSChart (mschrt20.ocx), which happens to be a Line but the problem also seems to occur for Bar/Pictograph, Area, Step and Combination. If the Y-axis, or second Y-axis, scale is -1 to 1, with 10 divisions, then the middle value shown on the Y-axis is 5.55112e-017, rather than 0. In fact, this will occur for similar ranges up to -5 to 5, at which point 0 is shown as the middle value. In one project this anomaly wouldn't show up the first time the chart was shown, but would on subsequent showings. In a project just consisting of the chart on a single form, the anomaly showed up right away.

My question is, how can I use this control to have a Y scale of -1 to 1, with 10 divisions and still have a 0 as the middle value? (I tried to use mschart.ocx, but VB said I didn't have a license to use it, but that's another problem.)
0
Comment
Question by:BDTurner
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:adg
ID: 6891937
Still interested? if so, can you post your code?
0
 

Author Comment

by:BDTurner
ID: 6892639
I am still interested in an answer to this, but it may be a bit less simple than I first thought. The test project I had previously created to confirm this no longer shows the problem, and a new project I created today initially showed the problem but then it went away without me changing anything. I don't know if giving you the code will help, because there's nothing to it, but I'll give you what I can. Try it and if it gives you no problems then I may just have to live with it.

There is a main form, with one button to call up the second form, and another button to quit;
Private Sub btnPlot_Click()
    Me.Visible = False
    frmPlot.Show vbModal, Me
    Me.Visible = True
End Sub
Private Sub btnQuit_Click()
    Unload Me
End Sub

The second form contains an MSChart (mschrt20.ocx) and a quit button;
Private Sub btnQuit_Click()
    Unload Me
End Sub

Change the chart type to line, and set the Y-axis to range from -1 to 1, with 10 divisions. That's all there is to it. Sometimes the middle value shows up as 0, and other times it is 5.55112e-017. I hope you can help.

0
 
LVL 3

Expert Comment

by:adg
ID: 6893092
I tried it with the following code as as you expected the problem did not appear.

    MSChart1.chartType = VtChChartType2dLine
    With MSChart1.Plot.Axis(VtChAxisIdY).ValueScale
        .Auto = False
        .Minimum = -1
        .Maximum = 1
        .MajorDivision = 10
    End With

A few questions:  

1. Is the use of multiple forms with buttons necessary to cause the problem to happen?

2. How are you setting the chart parameters; are you using code like above or something else?  

3. Are you using the same data each time or different data? Do you think it could be that the intermittent nature of the problem is due to different data being used?

4. Can you post an actual example of a program (with data) that had the problem?  

5. Did the size of the form and/or chart control change between the times it worked and the times it didn't.  
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:BDTurner
ID: 6893328
Thanks for looking into this for me. Here are some answers to your questions.

 1. Is the use of multiple forms with buttons necessary to cause the problem to happen?
No, one test project I made just had the chart on a sheet, although that one now seems to be working.

2. How are you setting the chart parameters; are you using code like above or something else?
In my real project, the plot values are set with an array;
   ReDim plotvalues(1 To 6, 1 To 2)
   'set some values
   MSChart1.ChartData = plotvalues
I don't think this would cause the problem though, since it happened with the test project mentioned earlier, which just used the default values in the chart control, and didn't have this code.

3. Are you using the same data each time or different data? Do you think it could be that the intermittent nature of the problem is due to different data being used?
I have not changed the data in the test projects, and they have gone from having the problem to not having it. The real project can use different data each time and always has the problem.

4. Can you post an actual example of a program (with data) that had the problem?  
See below.

5. Did the size of the form and/or chart control change between the times it worked and the times it didn't.
No, changing the size of the form and/or chart did not change the values on the axis.

I have been playing around with this for most of the day, and I seem to have a way to make this occur. On the form with the chart add a command button, with the following code;
Private Sub Command1_Click()
    With MSChart1
        For r = 1 To 5
            .Row = r
            If Rnd() < 0.5 Then
                .Data = -Rnd()
            Else
                .Data = Rnd()
            End If
        Next r
    End With
End Sub

This gives random values between -1 and 1 and on my system, the central value on the Y-axis is 5.55112e-017, after the first push. However, if I use different code to produce the same values;

Private Sub Command1_Click()
    With MSChart1
        For r = 1 To 5
            .Row = r
            cat = Rnd()
            dog = 0.5
            If cat < dog Then
                .Data = -Rnd()
            Else
                .Data = Rnd()
            End If
        Next r
    End With
End Sub

then the central value stays as 0! It seems that this problem can be caused by the if statement, although I didn't have any if statements in my test projects, and don't have that particular if statement in my real project. Can you reproduce this on your system, and if so, do you have any ideas on what is causing it?
0
 
LVL 3

Accepted Solution

by:
adg earned 300 total points
ID: 6899554
To my surprise I was able to recreate it. Just by experimentation it seems to be triggered by a reference to a function returning a single in an if condition.  Not just RND, but even a user-written function returning a single. Furthermore, the if statement doesn't actually have to contain any logic, just the reference is enough to trigger the bug.  It doesn't seem to be related to using  "With" or .Row or .Data. It doesn't seem to be affected by optimization.

 I changed your example as follows and it works on my system - does it work for you?

Private Sub Form_Load()
    Dim r As Long
    Randomize Timer
    With MSChart1.Plot.Axis(VtChAxisIdY).ValueScale
        .Auto = False
        .Minimum = -1
        .Maximum = 1
        .MajorDivision = 10
    End With
    With MSChart1
        .chartType = VtChChartType2dLine
        For r = 1 To 5
            .Row = r
            If CDbl(Rnd()) < 0.5 Then ' bug hidden
            'If Rnd() < 0.5 Then ' bug revealed
                .Data = -Rnd()
            Else
                .Data = Rnd()
            End If
        Next r
    End With
End Sub
0
 

Author Comment

by:BDTurner
ID: 6901474
YES!! That's it. With your comments I was able to find the offending statement. On another form I have an MSFlexgrid, with a statement to adjust the column width;

If TextWidth(.TextMatrix(r, c)) > CellWidth(c) Then

TextWidth was of type single, and once I replaced this statement with;

If CDbl(TextWidth(.TextMatrix(r, c))) > CellWidth(c) Then

the problem seems to have gone away.  Many thanks, and I've increased the points to 300 since it was more involved.
0
 
LVL 3

Expert Comment

by:adg
ID: 6901684
Thats great! You're very welcome and I'm glad I was able to help.  
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question