2
votes

I'm using the MS Chart control in .Net 4 to display 4 data series, the X axis is set to datetime type.

with the interval set to days the graph includes weekend even though no weekend dates are included in the data.

All the series except one have the same date points, the exception is a projection line that starts on the last date and continues for a number of working days (excludes weekends).

How can remove the display of weekends (or dates that have no value) from the chart for all series?

this is a winforms .Net 4.0 Application.

 foreach (var series in chart2.Series)
   {
       series.ChartType = SeriesChartType.Line;
       series.BorderWidth = 5;
       series.XValueType = ChartValueType.Date;
       // I Tried this to remove weekends but it results in the graph showing a big red X
       series.IsXValueIndexed = true; 
   }

Edit with Code to reproduce my issue below:

        DateTime dt = DateTime.Now;
        chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;

        Series test1 = new Series("Test1");
        test1.XValueType = ChartValueType.Date;
        for (int i = 1; i < 7; i++)
        {
            //skip the weekend
            if (dt.DayOfWeek.ToString() == "Saturday") dt = dt.AddDays(1);
            if (dt.DayOfWeek.ToString() == "Sunday") dt = dt.AddDays(1);

            test1.Points.AddXY(dt.Date, i);
            dt = dt.AddDays(1);
            i++;
        }

        chart1.Series.Add(test1);

        Series test2 = new Series("Test2");
        test1.XValueType = ChartValueType.Date;
        for (int i = 1; i < 7; i++)
        {
            //skip the weekend
            if (dt.DayOfWeek.ToString() == "Saturday") dt = dt.AddDays(1);
            if (dt.DayOfWeek.ToString() == "Sunday") dt = dt.AddDays(1);

            test2.Points.AddXY(dt.Date, i);
            dt = dt.AddDays(1);
            i++;
        }

        chart1.Series.Add(test2);

        foreach (var series in chart1.Series)
        {
            series.ChartType = SeriesChartType.Line;
            series.BorderWidth = 5;
            series.IsXValueIndexed=true;
        }
        chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 45;
        chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
1

1 Answers

3
votes

You are adding the x-values as DateTime, which is what you should do. But by default this means the all x-values are positionend proportionally, as one usually wants them to be.

But your case is an exception, that is even mentioned in the documentation of the Series.IsXValueIndexed property:

All data points in a series use sequential indices, and if this property is true then data points will be plotted sequentially, regardless of their associated X-values. This means that there will be no "missing" data points.

For example, assume there are three (3) data points in a series having X-values of 1, 2 and 4. If this property was false, there would be a data point missing at the X-axis location labeled "3". However, if you set this property to true, the three data points will be plotted at points 1, 2, and 4, sequentially, and there will be no missing data point. The X-axis location labeled "3" will not appear on the chart.

This is useful when you do not want to have missing data points for intervals that you know you will not have data for, such as weekends.

Note however:

If you are displaying multiple series and at least one series uses indexed X-values, then all series must be aligned — that is, have the same number of data points—and the corresponding points must have the same X-values.

So you were correct in using this property, however, most likely, your series were not fully aligned.

To make sure they are one often can make use of one of the Chart.DataManipulator.InsertEmptyPoints overloads. Unfortunately they all work with a fixed Interval, not with a template Series.

So you will need to makes sure to add one Empty DataPoint wherever your data are missing a value in a Series. If the data are databound you will have to do that in the DataSource!