Linear Chart. ItemsSource: Object reference not set to an instance of an object.

Mar 19, 2010 at 7:56 AM

 

Hi All,

 

I've got this error trying to change the itemssource for a chart.

 

Once I've added the data to the chart and after that I want to change this data by a new point collection. How can I do that?

 

Here is my code snippet:

 

DataSet dc = VelWb.Analytics_SalesProductGroupbyMonthAndcompany("235000405", "235000405", point.FieldName, point.Label);
            LineSeries line = chart1.Series[0] as LineSeries;
            points = new VelChartPoints();
            foreach (DataRow dr in dc.Tables[0].Rows)
            {
                points.Add(new VelChartPoint
                {
                    FieldName = point.FieldName,
                    Label = dr.Field<string>(1),
                    Value = (double)(dr.Field<int>(2))
                });
            }
            line.ItemsSource = new VelChartPoints();
            line.ItemsSource = points;

 

   LineSeries line = chart.Series[0] as LineSeries;
                points = new VelChartPoints();
                foreach (DataRow dr in dc.Tables[0].Rows)
                {

                    points.Add(new VelChartPoint
                    {
                        FieldName = "Chart1",
                        Label = dr.Field<string>(0),
                        Value = (double)(dr.Field<int>(1))
                    });


                    maxvalue = Math.Max(maxvalue, (double)(dr.Field<int>(1)));

                }

                LinearAxis axis = chart.Axes[1] as LinearAxis;

                axis.SetValue(LinearAxis.MaximumProperty, (maxvalue + (maxvalue / 4)));
                axis.SetValue(LinearAxis.IntervalProperty, Math.Round((maxvalue / 10), 0));
                line.ItemsSource = points;

 

First time this code works fine (I can see the point on the chart), but runnig this code again for the same chart throw the extention.

Please help me to solve the problem...

 

 

Mar 20, 2010 at 1:11 PM

if you call Chart.Series.Clear

and then

Chart.Series.Add(points) it should work...

Mar 21, 2010 at 7:28 PM
_kenneth_ wrote:

if you call Chart.Series.Clear

and then

Chart.Series.Add(points) it should work...

Thnx for reply. 

You're right, now I am able to edit it. But my points are the common objects. So the question now is how to access these objects in LinearSeries? I can see there only Points collection (nothing about stored Items... Found out only the SelectedItem).

Mar 21, 2010 at 7:49 PM
Edited Mar 22, 2010 at 6:21 AM

I pratially solved my issue, but still getting the error:

The enumerator is not valid because the collection changed.

The error occures at the line

pointsnew.Clear();

Here is a part of my code:

  ObservableCollection<VelChartPoint> pointsnew = line.ItemsSource as ObservableCollection<VelChartPoint>;
            pointsnew.Clear();
           
            DataSet dc = statistics.SalesQuantityProdcutGroupsMonthly;


            foreach (DataRow dr in dc.Tables[0].Rows)
            {
  
                    pointsnew.Add(new VelChartPoint
                    {
                        Label = dr.Field<string>(1),
                        Value = (double)(dr.Field<Int32?>(2).Value)
                    });
}

 

 

Mar 22, 2010 at 1:37 PM

Could you provide a compilable example and also your xaml code.

 

My version of it works just fine. Here my snippet:

 

            LineSeries line = testChart.Series[0] as LineSeries;
            ObservableCollection<KeyValuePair<string, double>> points = null;
            if (line.ItemsSource is ObservableCollection<KeyValuePair<string, double>>)
            {
                points = line.ItemsSource as ObservableCollection<KeyValuePair<string, double>>;
                points.Clear();
            }
            else
                points = new ObservableCollection<KeyValuePair<string, double>>();
            Random rnd = new Random();
            for (int i = 0; i < 50; i++)
            {
                KeyValuePair<string, double> point = new KeyValuePair<string, double>("point " + i, rnd.Next(100));
                points.Add(point);
            }
           
            line.ItemsSource = points;