when i use this Multiple Y axes feature, i found this odd exception hard to address,
i create a chart with multiple Y axes using this code:
Chart1.DefaultView.ChartArea.AdditionalYAxes.Add(new AxisY());
Chart1.DefaultView.ChartArea.AdditionalYAxes.Add(new AxisY());
Chart1.DefaultView.ChartArea.AdditionalYAxes[0].AxisName = "Secondary";
Chart1.DefaultView.ChartArea.AdditionalYAxes[1].AxisName = "Tertiary";
then i bind data on it and it's running correctly first time.
and i use a event(say button click) to trigger this code:
Chart1.SeriesMappings.Clear();
Chart1.DefaultView.ChartArea.AdditionalYAxes.Clear();
after this i run the same code above to add AdditionalYAxes and bind data again, and in throw a null reference exception everytime.
complete codes are like this:
public MultipleAxesTest()
{
InitializeComponent();
this.FillSampleChartData();
}
private void FillSampleChartData()
{
Chart1.DefaultView.ChartArea.AdditionalYAxes.Add(new AxisY());
Chart1.DefaultView.ChartArea.AdditionalYAxes.Add(new AxisY());
Chart1.DefaultView.ChartArea.AdditionalYAxes[0].AxisName = "Secondary";
Chart1.DefaultView.ChartArea.AdditionalYAxes[1].AxisName = "Tertiary";
Chart1.DefaultView.ChartArea.DataSeries.Add(GenerateSeries(0, 10, "Legend1", string.Empty, new BarSeriesDefinition()));
Chart1.DefaultView.ChartArea.DataSeries.Add(GenerateSeries(15, 50, "Legend2", "Secondary", new SplineSeriesDefinition()));
}
private static DataSeries GenerateSeries(int minRange, int maxRange, string legendLabel, string axisName, ISeriesDefinition definition)
{
DataSeries series = new DataSeries();
Random r = new Random();
series.Definition = definition;
series.LegendLabel = legendLabel;
series.Definition.AxisName = axisName;
for (int i = 0; i < 6; i++)
series.Add(new DataPoint(r.Next(minRange, maxRange)));
return series;
}
private void previewButton_Click(object sender, RoutedEventArgs e)
{
Chart1.SeriesMappings.Clear();
Chart1.DefaultView.ChartArea.AdditionalYAxes.Clear();
this.FillSampleChartData();
}
every time i trigger this event there's this exception.
and if i skip those two lines:
Chart1.SeriesMappings.Clear();
Chart1.DefaultView.ChartArea.AdditionalYAxes.Clear();
and it's ok, but obviously this is not the case as intend :(
This sounds a bug to me.
I would appreciate if anyone can give me more details about this behavior.
Edit: this only occur when i use 2010 Q1 version. i just try 2009 Q3 and it's running correctly.