Monday, January 05, 2009

Manually inserting empty points into a series in the Microsoft Chart Control - especially looking for gaps of specified lengths.

Been struggling with the manual insertion of empty points into a chart control.

Basically the built in DataManipulation.InsertEmptyPoints method do not search for gaps of a certain length - instead they look for datapoints at positions incremented from the first xValue seen - i.e. if there are data points on 1st Jan, 1st Feb, 1st March, 25th March, 10th April, 1st May. .... then the InsertEmptyPoints method when checking on months would show 1st April as empty - what a fool - sic ;)

Finally worked out how to do what I wanted - but had to use reflector to work it out - the key thing was manually setting the inserted data point to IsEmpty - hope this makes sense.


            List<int> emptyPointsPositionsToInsert = new List<int>();


            DataPoint previousPoint = series.Points.FirstOrDefault();

            for (int i=1; i<series.Points.Count; i++)


                double numDaysDifference = series.Points[i].XValue - series.Points[i-1].XValue;

                if (numDaysDifference > CurrentOptions.DurationForEmptyPoints)






            if (emptyPointsPositionsToInsert.Count > 0)


                series["EmptyPointValue"] = "Average";

                series.EmptyPointStyle.BorderDashStyle = ChartDashStyle.Dash;

                series.EmptyPointStyle.BorderWidth = 1;

                series.EmptyPointStyle.BorderColor = Color.FromArgb(200, Color.LightGray);

                if (series.ChartType != SeriesChartType.Area)

                    series.EmptyPointStyle.Color = series.EmptyPointStyle.BorderColor;

                series.EmptyPointStyle.MarkerStyle = MarkerStyle.None;


                // reverse the list first (otherwise positions will be all wrong!


                foreach (int position in emptyPointsPositionsToInsert)


                    double xNewPoint = series.Points[position].XValue - 1;

                    DataPoint newPoint = new DataPoint(xNewPoint, double.NaN);

                    newPoint.IsEmpty = true;

                    series.Points.Insert(position, newPoint);




1 comment:

  1. Hi
    I Your article is nicely expressive. But I cannot understand with these two points. Like what are numDaysDifference & CurrentOptions.DurationForEmptyPoints. Please reply me soon. I will be thankful for your support.
    for (int i=1; i CurrentOptions.DurationForEmptyPoints)
    Many Thanks In Advance...F. Shah