Search code examples
exceptionchartsrangewindows-forms-designerupdating

C# out or range exception Chart


Hey guys I am trying to create an updating Line Chart in C# but I keep getting an out of range exception on chart1.Series[0].Points[0].XValue. My Code goes like this :

        {    
    _valueList.Add(mspk);
    chart1.ResetAutoValues();

    if (chart1.Series[0].Points.Count > 0)
        {

            while (chart1.Series[0].Points[0].XValue < chart1.ChartAreas[0].AxisX.Minimum+5000)
            {

                    chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue - 5000;
                    chart1.ChartAreas[0].AxisX.Maximum = chart1.Series[0].Points[0].XValue + 5000;
                    chart1.Series[0].Points.RemoveAt(0);
            }

        }
    chart1.Series[0].Points.AddXY(inst, _valueList[_valueList.Count - 1]);
    chart1.Invalidate();
    }

I am not very experienced in C# so I tried to adapt this code that uses random numbers over time. I had no problem switching from the random numbers to my input but I cant seem to figure out how can I replace time with my data

        private void AddData()
    {
        DateTime now = DateTime.Now;
        //Insert a number into the list.
        _valueList.Add(_ran.Next(0, 100));


        chart1.ResetAutoValues();

        //Remove old datas from the chart.
        if (chart1.Series[0].Points.Count > 0)
        {
            while (chart1.Series[0].Points[0].XValue < now.AddSeconds(-5).ToOADate())
            {
                chart1.Series[0].Points.RemoveAt(0);

                chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
                chart1.ChartAreas[0].AxisX.Maximum = now.AddSeconds(5).ToOADate();
            }
        }

        //Insert a data into the chart.

        chart1.Series[0].Points.AddXY(now.ToOADate(), _valueList[_valueList.Count - 1]);

        chart1.Invalidate();
    }

Thanks In advance


Solution

  • Make sure that chart1.Series[0] exists before trying to access its properties.

        private void AddData()
    {
        DateTime now = DateTime.Now;
        //Insert a number into the list.
        _valueList.Add(_ran.Next(0, 100));
    
    
        chart1.ResetAutoValues();
    
        if(chart1.Series.Count==0) // Check to make sure Series 0 exists
        {
            chart1.Series.Add("Series 1"); // If it doesn't exist then create it.
        }
    
        //Remove old datas from the chart.
        if (chart1.Series[0].Points.Count > 0)
        {
            while (chart1.Series[0].Points[0].XValue < now.AddSeconds(-5).ToOADate())
            {
                chart1.Series[0].Points.RemoveAt(0);
    
                chart1.ChartAreas[0].AxisX.Minimum = chart1.Series[0].Points[0].XValue;
                chart1.ChartAreas[0].AxisX.Maximum = now.AddSeconds(5).ToOADate();
            }
        }
    
        //Insert a data into the chart.
    
        chart1.Series[0].Points.AddXY(now.ToOADate(), _valueList[_valueList.Count - 1]);
    
        chart1.Invalidate();
    }