问题
I have an app in which I'm using the included ASP.NET Chart controls. In one of my charts, I need to show a Bar chart with durations. The durations are stored as TimeSpan objects as shown here:
List<TimeSpan> durations = GetDurations(); // Will be between 15 minutes and 6 hours
List<string> labels = GetLabels();
...
chart.Series["Default"].Points.DataBindXY(xValues, durations);
When I execute this, an Exception is thrown that says:
Series data points do not support values of type System.TimeSpan only values of these types can be used: Double, Decimal, Single, int, long, uint, ulong, String, DateTime, short, ushort.
The exception is very clear what the problem is. However, how do I show my durations in a bar chart then?
Thank you!
回答1:
So then instead of having your durations be of type TimeSpan, which the exception is telling you Series data points do not support, why not use the Minutes property of the timespan? http://msdn.microsoft.com/en-us/library/system.timespan.minutes.aspx
List<int> durations = GetDurations(); // Change GetDurations to return a List<int> of the *minutes* of the timespan
List<string> labels = GetLabels();
...
chart.Series["Default"].Points.DataBindXY(xValues, durations);
来源:https://stackoverflow.com/questions/15364652/show-durations-in-an-asp-net-bar-chart