Solved

MSChart not displaying 0 in Y-axis

Posted on 2002-03-07
7
944 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
Comment Utility
Still interested? if so, can you post your code?
0
 

Author Comment

by:BDTurner
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:BDTurner
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thats great! You're very welcome and I'm glad I was able to help.  
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now