• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

Chart axis automatically adding dates


I'm generating a line chart in vb.net.  The series I'm adding is for dates (excluding weekends ) in a month. However the chart seems to be automatically including the weekends so  I am getting axis points for this month at 01/09/13, 08/09/13, 15/09/13 etc.  although these dates are not in the datarows that I am using to create the series.

Can anyone explain this?

1 Solution
Robert SchuttSoftware EngineerCommented:
The intelligence behind the chart uses linear axes by default when possible. So numbers and dates missing from the data points are still shown because normally you'd want that consistency in the way the data is presented. I'm not sure if there's an easy way to disable that mechanism in general but a little trick you can use is to convert the x value to a string, then the numeric interpretation is not done and the values shown on the x axis are only the ones you provide, nothing else.

Just a little example:
Imports System.Windows.Forms.DataVisualization.Charting

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim r As New Random()
        With Chart1
            With .Series(.Series.Count - 1)
                .ChartType = SeriesChartType.Line
                .BorderWidth = 3
                .MarkerStyle = MarkerStyle.Diamond
                .MarkerSize = 7
                .MarkerColor = Color.Red
                For i As Integer = -2 To 2
                    Dim d As Date = DateTime.Now.Date.AddDays(i * 7 - (DateTime.Now.DayOfWeek - 1))
                    For j = 0 To 4
                        .Points.AddXY(d.ToShortDateString, r.NextDouble() * 10)
                        d = d.AddDays(1)
            End With
            With .ChartAreas(0)
                .AxisX.Interval = 1
                .AxisY.Interval = 1
            End With
        End With
    End Sub

End Class

Open in new window

Petermcg001Author Commented:

Firstly, please accept my apologies for delaying.  I posted the question just as I was going on leave.

Your solution worked perfectly! As you say passing the value as a string rather than the raw data value disabled the automatic inclusion of missing dates.

Thanks so much.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now