The ChartAreas
Collection is still empty (Count == 0
). That's why you cannot access the item at index 0
. Which leads to your ArgumentOutOfRangeException
C# Chart ArguementOutOfRangeExecption
سؤال
I have the following abstract
class:
public abstract class ChartHelper
{
public System.Windows.Forms.DataVisualization.Charting.Chart resultChart { get; set; }
public String TimeType { get; set; }
protected List<IObject> _datalist;
protected TimeType _timeType;
protected DateTime _stopDate;
protected DateTime _startDate;
protected void SetXAsisTime()
{
if (resultChart != null)
{
foreach (var series in resultChart.Series)
{
series.XValueType = _timeType != DateUtils.TimeType.Weeks ? ChartValueType.DateTime : ChartValueType.String;
}
resultChart.ChartAreas[0].AxisX.LabelStyle.Format = GetLabelFormat();
resultChart.ChartAreas[0].AxisX.Interval = 1;
resultChart.ChartAreas[0].AxisX.IntervalType = GetIntervalType();
resultChart.ChartAreas[0].AxisX.IntervalOffset = 1;
}
}
protected String GetLabelFormat()
{
switch (_timeType)
{
case DateUtils.TimeType.Weeks:
return "Uge:";
case DateUtils.TimeType.Months:
return "MM";
case DateUtils.TimeType.Years:
return "YYYY";
default:
return "DD";
}
}
Now when i attempt to set the AxisX label format : resultChart.ChartAreas[0].AxisX.LabelStyle.Format = GetLabelFormat();
I get the following error message:
System.ArgumentOutOfRangeException was unhandled
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Source=mscorlib
ParamName=index
StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)
at Henvendelser.Chart.ChartHelper.SetXAsisTime()
at Henvendelser.Chart.EmailChartGenerator.GetChart()
at Henvendelser.Models.EmailModel.GetEmailChart(DateTime start, DateTime end, TimeType timeType)
at Henvendelser.Main.SetHenvendelserData(DateTime from, DateTime to)
at Henvendelser.Main.btn_getData_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Henvendelser.Program.Main()
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Can someone tell me what i am missing? or what i am doing wrong?
المحلول
لا تنتمي إلى StackOverflow