views:

1625

answers:

3

Hi,

I'm new to Silverlight development and am currently venturing into the Charting territory. I've been following several tutorials that show how easy it is to bind a ColumnSeries to a datasource using ItemsSource (http://silverlight.net/forums/t/44166.aspx).

I'm programatically adding a chart to a canvas.

Chart BudgetChart = new Chart { Title = "budget", MaxHeight= 200, MaxWidth=500};
ColumnSeries cs = new ColumnSeries();
BudgetChart.Series.Add(cs);
cs.Title = "blarg";
cs.ItemsSource = o.Budget; //returns List<Budget>
cs.IndependentValueBinding = new System.Windows.Data.Binding("Budget");
cs.DependentValueBinding = new System.Windows.Data.Binding("Year");

This code compiles fine. However, when I debug it, this error is throw However, I've been experiencing a lot of difficulty with ItemsSource. Each time I assign the ItemsSource, I get a error stating that it's Sys.InvalidOperationException: ManagedRuntimeError error #4004 in control 'Xaml1': System.NullReferenceException System.NullReferenceException: Object not set to an instance of an object. at System.Windows.Control.DataVisualization.Charting.ColumnSeries.<>c__DisplayClass8.b__4()

Please help, this error is driving me crazy!!!

A: 

Try moving the ItemsSource assignment to after the bindings. Also Add the series to the chart after having configured its bindings.

Chart BudgetChart = new Chart { Title = "budget", MaxHeight= 200, MaxWidth=500};
ColumnSeries cs = new ColumnSeries();
cs.Title = "blarg";
cs.IndependentValueBinding = new System.Windows.Data.Binding("Budget");
cs.DependentValueBinding = new System.Windows.Data.Binding("Year");

BudgetChart.Series.Add(cs);

cs.ItemsSource = o.Budget; //returns List<Budget>
AnthonyWJones
Hi AnthonyWJones,Thanks for your reply. I tried doing as you've suggest, but the problem still persist. Any other suggestions that I may try?
hantu7
If it's any help, I'm using the March release of the Toolkit.
hantu7
A: 

I've finally learned how to read. I was binding wrongly. I got confused between the Dependent and Independent bindings. Thank you for your help AnthonyWJones.

hantu7
A: 

Make sure your first data point does not contain a null/Nothing in the dependent value. The error occurs when the chart tries to "sniff" the type of dependent axes required, and fails.

   at System.Windows.Controls.DataVisualization.Charting.DataPointSeriesWithAxes.GetAxes(DataPoint firstDataPoint, Func`2 independentAxisPredicate, Func`1 independentAxisFactory, Func`2 dependentAxisPredicate, Func`1 dependentAxisFactory)

at System.Windows.Controls.DataVisualization.Charting.ColumnSeries.GetAxes(DataPoint firstDataPoint)

There seems to be no workaround. One option is to subclass a LinearAxis which overrides CanPlot(value). Currently NumericAxis.CanPlot fails for value==null

 /// <summary>
    /// Returns a value indicating whether a value can plot.
    /// </summary>
    /// <param name="value">The value to plot.</param>
    /// <returns>A value indicating whether a value can plot.</returns>
    public override bool CanPlot(object value)
    {
        double val;
        return ValueHelper.TryConvert(value, out val);
    }
Chui Tey