?
Solved

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

Posted on 2016-11-19
4
Medium Priority
?
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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