Solved

How Do I set the Visual Minor Grid Interval Value Programatically?

Posted on 2016-11-19
4
35 Views
Last Modified: 2016-11-21
I'm currently working with chart control in Visual Studio 2015 using the Visual Basic Language.  Here is what I have so far.

chart-1.JPG
I would like to add .25, .50, & .75 on the x-axis between the 0 and 1, I don't need it between the 1 & 2, 2 & 3 and 3 &4. 1. I have been playing with the code a bit and haven't been able to accomplish this. I'm not a big chart coder so I'm a little boggled. I have the chart data loading on form load event, as it's a pop-up form from the main form.  Here is what I have for code that produces the current graph.

Public Class frmBushingCharts
    Private Sub frmBushingCharts_Load(sender As Object, e As EventArgs) Handles Me.Load



            '#### 1/32 Wall Chart Data ####
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.125, 93)
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.25, 94)
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.5, 96)
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.75, 97)
            chtClose_In.Series("1/32 Wall").Points.AddXY(1, 98)
            chtClose_In.Series("1/32 Wall").Points.AddXY(1.375, 99.5)

            '#### 1/16 Wall Chart Data ####
            chtClose_In.Series("1/16 Wall").Points.AddXY(0.313, 88)
            chtClose_In.Series("1/16 Wall").Points.AddXY(0.5, 89)
            chtClose_In.Series("1/16 Wall").Points.AddXY(0.75, 90)
            chtClose_In.Series("1/16 Wall").Points.AddXY(1, 91)
            chtClose_In.Series("1/16 Wall").Points.AddXY(1.75, 91)

            '#### 1/8 Wall Chart Data ####
            chtClose_In.Series("1/8 Wall").Points.AddXY(0.4375, 73)
            chtClose_In.Series("1/8 Wall").Points.AddXY(0.5, 74)
            chtClose_In.Series("1/8 Wall").Points.AddXY(0.75, 77)
            chtClose_In.Series("1/8 Wall").Points.AddXY(1, 79)
            chtClose_In.Series("1/8 Wall").Points.AddXY(2, 83)
            chtClose_In.Series("1/8 Wall").Points.AddXY(2.4375, 83.5)

            '#### 3/16 Wall Chart Data ####
            chtClose_In.Series("3/16 Wall").Points.AddXY(0.688, 50)
            chtClose_In.Series("3/16 Wall").Points.AddXY(0.75, 54)
            chtClose_In.Series("3/16 Wall").Points.AddXY(1, 60)
            chtClose_In.Series("3/16 Wall").Points.AddXY(2, 77)
            chtClose_In.Series("3/16 Wall").Points.AddXY(3, 82)
            chtClose_In.Series("3/16 Wall").Points.AddXY(3.063, 82)

            '#### 1/4 Wall Chart Data ####
            chtClose_In.Series("1/4 Wall").Points.AddXY(1.188, 37)
            chtClose_In.Series("1/4 Wall").Points.AddXY(2, 60)
            chtClose_In.Series("1/4 Wall").Points.AddXY(3, 72)
            chtClose_In.Series("1/4 Wall").Points.AddXY(4, 73)

    End Sub
End Class

Open in new window


I was thinking of adding a for each loop to read each line and change interval for the label. But I keep getting errors. Maybe I'm going about this all wrong. Any insight would be greatly appreciated.


        Dim XValue = CDbl(chtClose_In.ChartAreas(0).Axes(0).ToString)
        Dim xInterval = chtClose_In.ChartAreas(0).AxisX.Interval
        For Each xVal In XValue.ToString
            If XValue < 1 Then xInterval = 1

                    'chart code goes here

         Next

Open in new window


Is there maybe a setting in the properties that allows for a minor grid interval between a specified Range?
0
Comment
Question by:Steve Williams
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 41895144
I think what you want is CustomLabel. I made some code to test and it seems to work. Note that info here states that once you add CustomLabels on the first row of labels the default labels are no longer shown so I added these back in. Maybe there are easier ways to get to mini/maxi but I found this worked where other things I tried didn't work so I'm going with this for the moment...
        Dim mini As Double = 0, maxi As Double = 0, intv As Double = chtClose_In.ChartAreas(0).AxisX.MajorGrid.Interval
        For s As Integer = 0 To chtClose_In.Series.Count - 1
            For p As Integer = 0 To chtClose_In.Series(s).Points.Count - 1
                Dim x As Double = chtClose_In.Series(s).Points(p).XValue
                If x < mini Then mini = x
                If x > maxi Then maxi = x
            Next
        Next
        For i As Double = mini To maxi Step intv
            If (i > 0 And i < 1) OrElse Math.Round(i) = i Then
                chtClose_In.ChartAreas(0).AxisX.CustomLabels.Add(New CustomLabel(i - intv / 2, i + intv / 2, i.ToString, 0, LabelMarkStyle.None))
            End If
        Next

Open in new window

capture
1
 
LVL 1

Author Comment

by:Steve Williams
ID: 41895750
@Robert Shutt:

Am I right to assume that the following code uses "Imports System.Windows.Forms.DataVisualization.Charting" before the public form class?

chtClose_In.ChartAreas(0).AxisX.CustomLabels.Add(New CustomLabel(i - intv / 2, i + intv / 2, i.ToString, 0, LabelMarkStyle.None))

Open in new window


I'm referring to "Custom Label" & "LabelMarkStyle"

I also tried this code and it removes the labels on the X-Axis. I must be implementing it wrong. Here is the code:

    Private Sub frmBushingCharts_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim mini As Double = 0
        Dim maxi As Double = 0
        Dim intv As Double = chtClose_In.ChartAreas(0).AxisX.MajorGrid.Interval

        For s As Integer = 0 To chtClose_In.Series.Count - 1
            For p As Integer = 0 To chtClose_In.Series(s).Points.Count - 1
                Dim x As Double = chtClose_In.Series(s).Points(p).XValue
                If x < mini Then mini = x
                If x > maxi Then maxi = x
            Next
        Next
        For i As Double = mini To maxi Step intv
            If (i > 0 And i < 1) OrElse Math.Round(i) = i Then
                chtClose_In.ChartAreas(0).AxisX.CustomLabels.Add(New CustomLabel(i - intv / 2, i + intv / 2, i.ToString, 0, LabelMarkStyle.None))
            End If
            '#### 1/32 Wall Chart Data ####
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.125, 93)
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.25, 94)
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.5, 96)
            chtClose_In.Series("1/32 Wall").Points.AddXY(0.75, 97)
            chtClose_In.Series("1/32 Wall").Points.AddXY(1, 98)
            chtClose_In.Series("1/32 Wall").Points.AddXY(1.375, 99.5)

            '#### 1/16 Wall Chart Data ####
            chtClose_In.Series("1/16 Wall").Points.AddXY(0.313, 88)
            chtClose_In.Series("1/16 Wall").Points.AddXY(0.5, 89)
            chtClose_In.Series("1/16 Wall").Points.AddXY(0.75, 90)
            chtClose_In.Series("1/16 Wall").Points.AddXY(1, 91)
            chtClose_In.Series("1/16 Wall").Points.AddXY(1.75, 91)

            '#### 1/8 Wall Chart Data ####
            chtClose_In.Series("1/8 Wall").Points.AddXY(0.4375, 73)
            chtClose_In.Series("1/8 Wall").Points.AddXY(0.5, 74)
            chtClose_In.Series("1/8 Wall").Points.AddXY(0.75, 77)
            chtClose_In.Series("1/8 Wall").Points.AddXY(1, 79)
            chtClose_In.Series("1/8 Wall").Points.AddXY(2, 83)
            chtClose_In.Series("1/8 Wall").Points.AddXY(2.4375, 83.5)

            '#### 3/16 Wall Chart Data ####
            chtClose_In.Series("3/16 Wall").Points.AddXY(0.688, 50)
            chtClose_In.Series("3/16 Wall").Points.AddXY(0.75, 54)
            chtClose_In.Series("3/16 Wall").Points.AddXY(1, 60)
            chtClose_In.Series("3/16 Wall").Points.AddXY(2, 77)
            chtClose_In.Series("3/16 Wall").Points.AddXY(3, 82)
            chtClose_In.Series("3/16 Wall").Points.AddXY(3.063, 82)

            '#### 1/4 Wall Chart Data ####
            chtClose_In.Series("1/4 Wall").Points.AddXY(1.188, 37)
            chtClose_In.Series("1/4 Wall").Points.AddXY(2, 60)
            chtClose_In.Series("1/4 Wall").Points.AddXY(3, 72)
            chtClose_In.Series("1/4 Wall").Points.AddXY(4, 73)
        Next

Open in new window


and here is the result:

Missing X-Axis Labels
Your example is dead on what I'm trying to accomplish. Thanks for the effort.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 41896341
Yes, the Import is needed, sorry I forgot to mention that.

My code was supposed to supplement your existing code. The complete code I used for testing is:
        '#### 1/32 Wall Chart Data ####
        chtClose_In.Series("1/32 Wall").Points.AddXY(0.125, 93)
        chtClose_In.Series("1/32 Wall").Points.AddXY(0.25, 94)
        chtClose_In.Series("1/32 Wall").Points.AddXY(0.5, 96)
        chtClose_In.Series("1/32 Wall").Points.AddXY(0.75, 97)
        chtClose_In.Series("1/32 Wall").Points.AddXY(1, 98)
        chtClose_In.Series("1/32 Wall").Points.AddXY(1.375, 99.5)

        '#### 1/16 Wall Chart Data ####
        chtClose_In.Series("1/16 Wall").Points.AddXY(0.313, 88)
        chtClose_In.Series("1/16 Wall").Points.AddXY(0.5, 89)
        chtClose_In.Series("1/16 Wall").Points.AddXY(0.75, 90)
        chtClose_In.Series("1/16 Wall").Points.AddXY(1, 91)
        chtClose_In.Series("1/16 Wall").Points.AddXY(1.75, 91)

        '#### 1/8 Wall Chart Data ####
        chtClose_In.Series("1/8 Wall").Points.AddXY(0.4375, 73)
        chtClose_In.Series("1/8 Wall").Points.AddXY(0.5, 74)
        chtClose_In.Series("1/8 Wall").Points.AddXY(0.75, 77)
        chtClose_In.Series("1/8 Wall").Points.AddXY(1, 79)
        chtClose_In.Series("1/8 Wall").Points.AddXY(2, 83)
        chtClose_In.Series("1/8 Wall").Points.AddXY(2.4375, 83.5)

        '#### 3/16 Wall Chart Data ####
        chtClose_In.Series("3/16 Wall").Points.AddXY(0.688, 50)
        chtClose_In.Series("3/16 Wall").Points.AddXY(0.75, 54)
        chtClose_In.Series("3/16 Wall").Points.AddXY(1, 60)
        chtClose_In.Series("3/16 Wall").Points.AddXY(2, 77)
        chtClose_In.Series("3/16 Wall").Points.AddXY(3, 82)
        chtClose_In.Series("3/16 Wall").Points.AddXY(3.063, 82)

        '#### 1/4 Wall Chart Data ####
        chtClose_In.Series("1/4 Wall").Points.AddXY(1.188, 37)
        chtClose_In.Series("1/4 Wall").Points.AddXY(2, 60)
        chtClose_In.Series("1/4 Wall").Points.AddXY(3, 72)
        chtClose_In.Series("1/4 Wall").Points.AddXY(4, 73)

        chtClose_In.ChartAreas(0).AxisY.MajorGrid.Interval = 10
        chtClose_In.ChartAreas(0).AxisY.LabelStyle.Interval = 10

        chtClose_In.ChartAreas(0).AxisX.Minimum = 0
        chtClose_In.ChartAreas(0).AxisX.MajorGrid.Interval = 0.25
        chtClose_In.ChartAreas(0).AxisX.LabelStyle.Interval = 1

        Dim mini As Double = 0, maxi As Double = 0, intv As Double = chtClose_In.ChartAreas(0).AxisX.MajorGrid.Interval
        For s As Integer = 0 To chtClose_In.Series.Count - 1
            For p As Integer = 0 To chtClose_In.Series(s).Points.Count - 1
                Dim x As Double = chtClose_In.Series(s).Points(p).XValue
                If x < mini Then mini = x
                If x > maxi Then maxi = x
            Next
        Next
        For i As Double = mini To maxi Step intv
            If (i > 0 And i < 1) OrElse Math.Round(i) = i Then
                chtClose_In.ChartAreas(0).AxisX.CustomLabels.Add(New CustomLabel(i - intv / 2, i + intv / 2, i.ToString, 0, LabelMarkStyle.None))
            End If
        Next

Open in new window

1
 
LVL 1

Author Closing Comment

by:Steve Williams
ID: 41896349
Robert that did the trick. Works perfectly. I really appreciate your help and valuable time.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 59
Visual Studio editor? 1 38
Red error squiggly on vb.net 7 27
Make all values in arrayist to titlecase+vb.net 23 27
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

896 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

15 Experts available now in Live!

Get 1:1 Help Now