Search code examples
vb.netasp.net-charts

asp.net Charts : Extra point(Tick Mark) added on X Axis


I am new to Asp.net charts and is developing a Line Chart which looks like:

enter image description here

The part of X-Axis code is : (The comments are added in the code)

    Chart1.ChartAreas(0).AxisX.Title = "Age"
    Chart1.ChartAreas(0).AxisX.LineWidth = 2
    Chart1.ChartAreas(0).AxisX.Interval = 1
    'Turn off X-axis lines
    Chart1.ChartAreas(0).AxisX.MajorGrid.Enabled = False
    ' Enable X axis margin
    Chart1.ChartAreas(0).AxisX.IsMarginVisible = True
    Chart1.ChartAreas(0).AxisX.IsStartedFromZero = True
    Chart1.ChartAreas(0).AxisX.IsStartedFromZero = False


    Chart1.ChartAreas(0).AxisX.LabelStyle.Angle = -60
    Chart1.ChartAreas(0).AxisX.LabelStyle.Font = New Drawing.Font("Arial", 8, Drawing.FontStyle.Bold)
    Chart1.ChartAreas(0).AxisX.MajorTickMark.Enabled = False
    Chart1.ChartAreas(0).AxisX.IsStartedFromZero = True


    'age() is array of Ages 
    'Estimated is array of estimated values in Dollars
    'Real is array of real values in Dollars
    s.ChartType = SeriesChartType.Line
    g.ChartType = SeriesChartType.Line
    s.BorderWidth = 2
    g.BorderWidth = 2

    Dim count As Integer = 0
    'Finding the interval for X Axis
    'Value of maxAge is 95 and myAge 55
    Dim interval = (maxAge - myAge) / 10 '
    Chart1.ChartAreas(0).AxisX.Minimum = myAge
    'interval value is 4
    Chart1.ChartAreas(0).AxisX.Interval = interval

    'To start the graph from origin

    g.Points.AddXY(Convert.ToInt32(myAge), 0)
    s.Points.AddXY(Convert.ToInt32(myAge), 0)
    'Adding the label for origin
    Chart1.ChartAreas(0).AxisX.CustomLabels.Add(myAge - 1, myAge + 1, "Age " & myAge & " ")
    count = 0
    'This loop runs from 55 to 95 of age() Array
    For i = 1 To 10
        count += interval
        s.Points.AddXY(age(count), Estimated(count))
        g.Points.AddXY(age(count), Real(count))
        Chart1.ChartAreas(0).AxisX.CustomLabels.Add(age(count) - 0.3, age(count), "Age " & age(count) & " ")
    Next i
    Chart1.Series.Add(s)
    Chart1.Series.Add(g)

And the output to this code is

enter image description here

The issue is, though my loop runs for exactly 10 times, an extra Tick mark gets added on the X-Axis.

So my question is

Is there way to restrict the graph to the last tick mark, instead of taking an extra leap?

Thanks in advance


Solution

  • I am newbie to asp.net charts therefore, was making this silly mistake.

    All I had to set the

     Chart1.ChartAreas(0).AxisX.Maximum = maxAge 
    

    And bingo it worked! The graph got limited to Age 95 tick mark.