This is a migrated thread and some comments may be shown as answers.

Error changing chart definition

5 Answers 81 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Nuno Ferreira
Top achievements
Rank 1
Nuno Ferreira asked on 04 Nov 2010, 12:05 PM
Hi,

I'm working with RadControls for Silverlight Q2 2010 SP2 and I get a null reference exception (line: ds.Definition = seriesDef; see code below) when I try to change chart definition. I think that the code below worked with an older version of RadControls.

XAML:
<Grid x:Name="LayoutRoot">
    <StackPanel Orientation="Vertical">
        <telerik:RadChart Height="500" HorizontalAlignment="Left" Margin="10,10,0,0" Name="radChart1" VerticalAlignment="Top" Width="500" />
        <Button Content="Bar" Click="BarChart" Width="50" Height="20"/>
        <Button Content="Pie" Click="PieChart" Width="50" Height="20"/>
        <Button Content="Line" Click="LineChart" Width="50" Height="20"/>
    </StackPanel>
</Grid>

CS:
public MainPage()
{
    InitializeComponent();
    DataSeries ds = new DataSeries();
    ds.Add(new DataPoint { XCategory = "Item 1", YValue = 2 });
    ds.Add(new DataPoint { XCategory = "Item 2", YValue = 4 });
    radChart1.DefaultView.ChartArea.DataSeries.Add(ds);
 
    radChart1.DefaultView.ChartArea.Legend.Visibility = System.Windows.Visibility.Collapsed;
    radChart1.DefaultSeriesDefinition = new BarSeriesDefinition();
}
 
private void BarChart(object sender, System.Windows.RoutedEventArgs e)
{
    ChangeDefinition(new BarSeriesDefinition());
}
 
private void LineChart(object sender, System.Windows.RoutedEventArgs e)
{
    ChangeDefinition(new LineSeriesDefinition());
}
 
private void PieChart(object sender, System.Windows.RoutedEventArgs e)
{
    ChangeDefinition(new PieSeriesDefinition());
}
 
private void ChangeDefinition(SeriesDefinition seriesDef)
{
    DataSeries ds = radChart1.DefaultView.ChartArea.DataSeries[0];
    ds.Definition = seriesDef;
    radChart1.DefaultView.ChartArea.DataSeries.Clear();
    radChart1.DefaultView.ChartArea.DataSeries.Add(ds);
}

5 Answers, 1 is accepted

Sort by
0
Evgenia
Telerik team
answered on 09 Nov 2010, 01:14 PM
Hello Nuno,

After reviewing your code snippet I noticed that you are trying to set XCategory to PieSeriesDefinition. This is not possible logically as Pies don't have X axis and therefore they can't have XCategories.
Remove your XCategory initialization from DataPoint constructor and fill DataSeries like this: 
ds.Add(new DataPoint { YValue = 2 });
Now all 3 buttons will work correctly and you will be able to see Bar, Line and Pie charts.
I hope this gets you started properly.

Sincerely yours,
Evgenia
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nuno Ferreira
Top achievements
Rank 1
answered on 09 Nov 2010, 03:29 PM
Sorry, but I tried like you said and got the same error. Even if I don't change to the pie chart but change to other chart (line for example) I get the error. Like I said, my application worked, before I upgrade to the lastest version of RadControls.
0
Evgenia
Telerik team
answered on 12 Nov 2010, 03:13 PM
Hello Nuno,

I investigated the issue you have and I admit that NullReference exception is thrown when trying to change the SeriesDefinition on ButtonClick. I'm wondering in what version of RadChart this code snippet you sent worked as this seems to be appearing because of the order of events RadChart has?
Meanwhile this issue won't appear if you create new DataSeries every time you click the button instead of reusing the same DataSeries. Here is what I mean:
public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DataSeries ds = new DataSeries();
            ds.Add(new DataPoint { YValue = 2 });
            ds.Add(new DataPoint { YValue = 4 });
            radChart1.DefaultView.ChartArea.DataSeries.Add(ds);
  
            radChart1.DefaultView.ChartArea.Legend.Visibility = System.Windows.Visibility.Collapsed;
            radChart1.DefaultSeriesDefinition = new BarSeriesDefinition();
  
        }
        private void BarChart(object sender, System.Windows.RoutedEventArgs e)
        {
            ChangeDefinition(new BarSeriesDefinition());
        }
  
        private void LineChart(object sender, System.Windows.RoutedEventArgs e)
        {
            ChangeDefinition(new LineSeriesDefinition());
        }
  
        private void PieChart(object sender, System.Windows.RoutedEventArgs e)
        {
            ChangeDefinition(new PieSeriesDefinition());
        }
  
        private void ChangeDefinition(SeriesDefinition seriesDef)
        {
            DataSeries diffDataSeries = new DataSeries();
            diffDataSeries.Add(new DataPoint { YValue = 2 });
            diffDataSeries.Add(new DataPoint { YValue = 4 });
            diffDataSeries.Definition = seriesDef;
            radChart1.DefaultView.ChartArea.DataSeries.Clear();
            radChart1.DefaultView.ChartArea.DataSeries.Add(diffDataSeries);
        }
    }

What I can suggest is that you use DataBinding and more specifically SeriesMappings instead of  manually giving values to DataSeries as SeriesMappings are more reliable for your case. You can find more information about this in our help topic - http://www.telerik.com/help/silverlight/radchart-populating-with-data-data-binding-with-manual-series-mapping.html. Then on button click everything will work as expected. 

Best Regards,
Evgenia
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Nuno Ferreira
Top achievements
Rank 1
answered on 16 Nov 2010, 01:08 AM
Hi Evgenia,

ok, I'll try with SeriesMappings.

But meanwhile, I have tested my code snippet with different versions of RadControls. Here are the results:

Works with:
- RadControls 2010.2.924 / SL3
- 2010.1.416 / SL4
- 2010.1.422 / SL4
- 2010.2.714 / SL4

Doesn't work with:
- 2010.2.812 / SL4
- 2010.2.924 / SL4
- 2010.3.1110 / SL4


Also note that I can change the chart, the first time I click on a button. The error occurs only at the second click.

Thanks,
Nuno
0
Accepted
Evgenia
Telerik team
answered on 18 Nov 2010, 10:35 AM
Hi Nuno,

Thank you for sharing this information with us. Indeed the exception is thrown only at the second click of any button. This issue is caused by the order of  raising the Chart events as there were changes made to our control in newest versions. I can assure you that these changes were made for improvement / better performance of RadChart. To have your scenario work as expected try the code I pasted in my previous post or use SeriesMappings.

Kind regards,
Evgenia
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
Tags
Chart
Asked by
Nuno Ferreira
Top achievements
Rank 1
Answers by
Evgenia
Telerik team
Nuno Ferreira
Top achievements
Rank 1
Share this question
or