A collection with one item will have incorrect behavior in several places of your code.
Here graphSpan
will equal zero:
TimeSpan graphSpan = res.Values.ToList().Last().Key - res.Values.ToList().First().Key;
And here Maximum
and Minimum
will be the same:
lineSeries.IndependentAxis = new DateTimeAxis
{
Minimum = res.Values.ToList().First().Key,
Maximum = res.Values.ToList().Last().Key,
I suggest that you add another if-block and construct a different axis for the special case when the collection has only 1 item.
var values = res.Values.ToList();
TimeSpan graphSpan = values.Last().Key - values.First().Key;
if (graphSpan == TimeSpan.Zero)
{
lineSeries.IndependentAxis = new DateTimeAxis
{
Orientation = AxisOrientation.X,
Location = AxisLocation.Bottom
};
}
else
{
lineSeries.IndependentAxis = new DateTimeAxis
{
Minimum = values.First().Key,
Maximum = values.Last().Key,
Orientation = AxisOrientation.X,
Location = AxisLocation.Bottom
};
if (graphSpan > monthSpan)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days;
((DateTimeAxis)lineSeries.IndependentAxis).Interval = 5;
}
else if (graphSpan > daySpan && graphSpan < monthSpan)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days;
((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1;
}
else if (graphSpan > hourSpan && graphSpan < daySpan)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Hours;
((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1;
}
else if (graphSpan < hourSpan)
{
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Minutes;
((DateTimeAxis)lineSeries.IndependentAxis).Interval = 15;
}
else
{
//sometimes all comparisons fail, just back up to a safe interval of 1 day.
((DateTimeAxis)lineSeries.IndependentAxis).IntervalType = DateTimeIntervalType.Days;
((DateTimeAxis)lineSeries.IndependentAxis).Interval = 1;
}
}