Solved

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

Posted on 2016-11-19
4
42 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 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