Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import Data from Multiple Text Files in Excel 12 86
Help with opening combobox using VB.NET. 3 38
Help with error in Query 2 39
SSRS 2016 Rendering HTML tables 3 61
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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