Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

14 Experts available now in Live!

Get 1:1 Help Now