I am using a spline chart with 2 DataSeries on. When the chart is drawn first time, I setup the two DataSeries objects with their DataPoints and it renders absolutely fine.
Then, at certain intervals, I check whether the source data has changed and if it has then do one of three things to the original DataSeries:
- If the Y value of an original datapoint has changed in the source data - adjust the value of that datapoint in the original dataseries
- If a value has dissappeared from the source data then the datapoint is removed
- If there is a new value in the source data then we either add or insert a new datapoint into the dataseries
Actions 1 and 2 are working fine. With action 3, a call to DataSeries.Add to add to the end works fine but a call to DataSeries.Insert (to insert at a particular index) is failing with the following error:
---------------------
Microsoft JScript runtime error: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Telerik.Windows.Controls.Charting.DataPoint.GetNextSignificantIndex()
at Telerik.Windows.Controls.Charting.SplineHelper.EvaluateIndexDependencies(Int32 currentIndex, DataSeries dataSeries)
at Telerik.Windows.Controls.Charting.SelfDrawingSeries.CalculateSplinePoints(Func`2 extractXRangeValue, Func`3 extractYRangeValue)
at Telerik.Windows.Controls.Charting.SplineSeries.CreateFigures()
at Telerik.Windows.Controls.Charting.SelfDrawingSeries.InitializeTopBorderLineGeometry()
at Telerik.Windows.Controls.Charting.SelfDrawingSeries.InitializeSeriesGeometry()
at Telerik.Windows.Controls.Charting.SelfDrawingSeries.OnApplyTemplate()
at System.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)
--- End of inner exception stack trace ---
at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name)
at MS.Internal.XcpImports.FrameworkElement_ApplyTemplate(FrameworkElement frameworkElement)
at System.Windows.Controls.ItemContainerGenerator.LayoutStatesManager.GetElementRoot(Boolean templatesAreGenerated)
at System.Windows.Controls.ItemContainerGenerator.LayoutStatesManager.Load()
at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
at System.Windows.Controls.ItemsControl.AddVisualChild(Int32 containerIndex, DependencyObject container, Boolean needPrepareContainer)
at System.Windows.Controls.ItemsControl.AddContainers()
at System.Windows.Controls.ItemsControl.RecreateVisualChildren(IntPtr unmanagedObj)
-----------------------
I noticed that when I look at the DataSeries object after my insert, for the DataPoint I inserted - the DataPoint.DataSeries member is null for that one and the next DataPoint (one of the original ones). All the others have a valid member of the parent DataSeries.
Am I misunstanding how the Insert method operates? I'm expecting if I have 6 datapoints (indexes 0 to 5) and I insert at index 0 then the original 6 datapoints move to indexes 1 to 6 and my new datapoint goes in at index 0. I would read the help page on this but it won't load - Click the Insert method from this page http://www.telerik.com/help/silverlight/telerik.windows.controls.charting-telerik.windows.controls.charting.dataseries_members.html
I read the post that has a similar error - http://www.telerik.com/community/forums/silverlight/chart/error-after-upgrade-to-2011-version.aspx - but I'm not reusing DataPoints or DataSeries across instances - I'm using a brand new DataPoint.
Thanks
Chris
17 Answers, 1 is accepted
Although I'm guessing some links need to be updated.
Still, after looking at the help, I can't see what I'm doing wrong.
A further update on this. I've found that it's not the DataSeries.Insert on it's own thats causing the issue. It's actually a combination of a DataSeries.Insert and a DataSeries.Remove in that order. If I change it to do the two operations the other way around then the code works fine.
Heading home for the weekend now but look forward to any feedback on this if possible.
Thanks for raising up the wrong links. It will be taken care of them.
Regarding your issue, I wasn't able to reproduce it. I've created a sample project following a similar scenario to yours. The project is attached to this thread so you could refer to it.
I tried adding, inserting and removing data points and they all worked fine.
In order to be able to help you further we will need more information. A runnable copy of your project would be fine.
Hope that helps.
Regards,
Peshito
the Telerik team
I slightly altered the sample code you sent over to better represent our code and have been able to reproduce the exact problem. It seems to be always with the DataSeries.Remove line (but as outlined below in two examples, can produce slightly different errors).
In this example - just click the "First Change" button.
The new MainPage.xaml:
<
UserControl
x:Class
=
"DataSeriesAddInsert.MainPage"
mc:Ignorable
=
"d"
d:DesignHeight
=
"300"
d:DesignWidth
=
"400"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
>
<
Grid
x:Name
=
"LayoutRoot"
Background
=
"White"
Width
=
"750"
Height
=
"650"
>
<
Button
Name
=
"btnFirstChange"
HorizontalAlignment
=
"Left"
VerticalAlignment
=
"Top"
Click
=
"btnFirstChange_Click"
>First Change</
Button
>
<
Button
Name
=
"btnSecondChange"
HorizontalAlignment
=
"Left"
Margin
=
"0,25,0,0"
VerticalAlignment
=
"Top"
Click
=
"btnSecondChange_Click"
>Second Change</
Button
>
<
telerik:RadChart
Name
=
"radChart"
Width
=
"650"
Height
=
"450"
/>
</
Grid
>
</
UserControl
>
The new MainPage.xaml.cs:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Telerik.Windows.Controls.Charting;
namespace
DataSeriesAddInsert
{
public
partial
class
MainPage : UserControl
{
DataSeries series =
new
DataSeries();
DataPoint newPoint =
new
DataPoint(
new
DateTime(2009, 5, 7).ToOADate(), 110);
public
MainPage()
{
InitializeComponent();
series.Definition =
new
SplineSeriesDefinition();
series.LegendLabel =
"Rate Prices"
;
series.Add(
new
DataPoint(
new
DateTime(2011, 7, 15).ToOADate(), 100));
series.Add(
new
DataPoint(
new
DateTime(2011, 7, 27).ToOADate(), 110));
series.Add(
new
DataPoint(
new
DateTime(2011, 8, 29).ToOADate(), 120));
radChart.DefaultView.ChartArea.AxisX.IsDateTime =
true
;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Inside;
radChart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
radChart.DefaultView.ChartArea.AxisX.DefaultLabelFormat =
"dd-MMM"
;
radChart.DefaultView.ChartArea.EnableAnimations =
false
;
radChart.DefaultView.ChartTitle.Content =
"Year 2011"
;
radChart.DefaultView.ChartLegend.Header =
"Legend"
;
radChart.DefaultView.ChartLegend.UseAutoGeneratedItems =
true
;
//Axis X
radChart.DefaultView.ChartArea.AxisX.Title =
"Date"
;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Between;
//Axis Y
radChart.DefaultView.ChartArea.AxisY.Title =
"Price"
;
radChart.DefaultView.ChartArea.DataSeries.Add(series);
}
private
void
btnFirstChange_Click(
object
sender, RoutedEventArgs e)
{
var button = sender
as
Button;
if
((
bool
)button.IsPressed)
{
// remove A and B
List<DataPoint> dpsToRemove =
new
List<DataPoint>();
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][0]);
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][1]);
// HERE
DataPoint point1 =
new
DataPoint(
new
DateTime(2011, 7, 18).ToOADate(), 107);
DataPoint point2 =
new
DataPoint(
new
DateTime(2011, 7, 28).ToOADate(), 109);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(0, point1);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(1, point2);
// TO TELERIK - THIS LINE WILL FAIL IN THIS EXAMPLE
// BUT WILL WORK FINE IF MOVED TO WHERE IT SAYS "HERE" ABOVE
dpsToRemove.ForEach(dp => radChart.DefaultView.ChartArea.DataSeries[0].Remove(dp));
}
}
private
void
btnSecondChange_Click(
object
sender, RoutedEventArgs e)
{
var button = sender
as
Button;
if
((
bool
)button.IsPressed)
{
// remove A and B
List<DataPoint> dpsToRemove =
new
List<DataPoint>();
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][0]);
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][1]);
dpsToRemove.ForEach(dp => radChart.DefaultView.ChartArea.DataSeries[0].Remove(dp));
DataPoint point1 =
new
DataPoint(
new
DateTime(2011, 7, 15).ToOADate(), 100);
DataPoint point2 =
new
DataPoint(
new
DateTime(2011, 7, 27).ToOADate(), 110);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(0, point1);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(1, point2);
}
}
}
}
I also then changed this sample code again very slightly and repeated the issue in this thread - http://www.telerik.com/community/forums/silverlight/chart/need-help---nullreferenceexception-working-directly-with-the-datapoints-in-a-dataseries.aspx
In this second, example, I've moved the remove line in the codebehind for the first button so it now gets past that. So click the "First Change" button, then the "Second Change" button and it will then fail - again on a remove line.
This involved just a quick change in the MainPage.xaml.cs:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
Telerik.Windows.Controls.Charting;
namespace
DataSeriesAddInsert
{
public
partial
class
MainPage : UserControl
{
DataSeries series =
new
DataSeries();
DataPoint newPoint =
new
DataPoint(
new
DateTime(2009, 5, 7).ToOADate(), 110);
public
MainPage()
{
InitializeComponent();
series.Definition =
new
SplineSeriesDefinition();
series.LegendLabel =
"Rate Prices"
;
series.Add(
new
DataPoint(
new
DateTime(2011, 7, 15).ToOADate(), 100));
series.Add(
new
DataPoint(
new
DateTime(2011, 7, 27).ToOADate(), 110));
series.Add(
new
DataPoint(
new
DateTime(2011, 8, 29).ToOADate(), 120));
radChart.DefaultView.ChartArea.AxisX.IsDateTime =
true
;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Inside;
radChart.DefaultView.ChartArea.AxisX.LabelRotationAngle = 45;
radChart.DefaultView.ChartArea.AxisX.DefaultLabelFormat =
"dd-MMM"
;
radChart.DefaultView.ChartArea.EnableAnimations =
false
;
radChart.DefaultView.ChartTitle.Content =
"Year 2011"
;
radChart.DefaultView.ChartLegend.Header =
"Legend"
;
radChart.DefaultView.ChartLegend.UseAutoGeneratedItems =
true
;
//Axis X
radChart.DefaultView.ChartArea.AxisX.Title =
"Date"
;
radChart.DefaultView.ChartArea.AxisX.LayoutMode = AxisLayoutMode.Between;
//Axis Y
radChart.DefaultView.ChartArea.AxisY.Title =
"Price"
;
radChart.DefaultView.ChartArea.DataSeries.Add(series);
}
private
void
btnFirstChange_Click(
object
sender, RoutedEventArgs e)
{
var button = sender
as
Button;
if
((
bool
)button.IsPressed)
{
// remove A and B
List<DataPoint> dpsToRemove =
new
List<DataPoint>();
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][0]);
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][1]);
dpsToRemove.ForEach(dp => radChart.DefaultView.ChartArea.DataSeries[0].Remove(dp));
DataPoint point1 =
new
DataPoint(
new
DateTime(2011, 7, 18).ToOADate(), 107);
DataPoint point2 =
new
DataPoint(
new
DateTime(2011, 7, 28).ToOADate(), 109);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(0, point1);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(1, point2);
}
}
private
void
btnSecondChange_Click(
object
sender, RoutedEventArgs e)
{
var button = sender
as
Button;
if
((
bool
)button.IsPressed)
{
// remove A and B
List<DataPoint> dpsToRemove =
new
List<DataPoint>();
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][0]);
dpsToRemove.Add(radChart.DefaultView.ChartArea.DataSeries[0][1]);
// TO TELERIK - THIS LINE WILL FAIL IN THIS EXAMPLE
dpsToRemove.ForEach(dp => radChart.DefaultView.ChartArea.DataSeries[0].Remove(dp));
DataPoint point1 =
new
DataPoint(
new
DateTime(2011, 7, 15).ToOADate(), 100);
DataPoint point2 =
new
DataPoint(
new
DateTime(2011, 7, 27).ToOADate(), 110);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(0, point1);
radChart.DefaultView.ChartArea.DataSeries[0].Insert(1, point2);
}
}
}
}
I believe that these two threads are now very closely related. I will post a message in the other thread as such to point back to this one.
The code is working against the 2011 Q2 version (2011.2.712.1040).
I look forward to hearing back from you with your findings.
Kind regards
Chris
We managed to reproduce your issue and seems like you have hit a bug.
Thanks for bringing this to our attention. As a result of that and the broken links you've mentioned earlier, we have updated your Telerik points.
I have forwarded the bug to our developers and ti will be fixed soon.
Meanwhile as you've already mentioned that DataSeries.Add works fine, you could use this approach as a workaround.
Kind regards,
Peshito
the Telerik team
I face exactly the same problem. Could you please post an ID in PITS so I can track when it's fixed?
Regards,
Georgi
Peshito - thanks for confirming that it is definately a bug and not something we're doing wrong - I couldn't use the Add method all the time as we have to insert at specific positions sometimes and delete some existing datapoints.
Therefore, the only way I could resolve this was to rewrite our approach and have a DataTable instead that was then bound to the Chart using the ItemsSource member. I then changed all the code that did the Add/Insert/Remove on the DataPoints to do Add/Insert/Remove on rows of the DataTable instead. This obviously removed the reliance on dealing directly with DataPoints. This has resolved our own issue but obviously Georgi and others may not be able to do the same thing.
Thanks
Chris
I am glad you managed to find appropriate solution for your issue.
Here is a link to the PITS where the bug ststus can be tracked.
Greetings,
Peshito
the Telerik team
Cheers,
-Tim
You can keep tracking this issue in the PITS here.
Whenever there is a change in the bug status it will be marked there.
Regards,
Peshito
the Telerik team
Thanks
The fix will be included in the weekly internal build next Monday.
Sorry for the temporary inconvenience.
Best wishes,
Giuseppe
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
Thanks very much for the update. We look forward to trying the update.
Best regards,
Terence
I am missing something - I don't see a new internal build. The latest. appears to be from the 15th. Please advise.
Thanks,
Terence
Thanks,
Terence