Skip to main content

Sharing objects between other objects

LightningChart object model is tree-based. Every class has its parent object and a list of child objects. This tree-model allows child object to notify the parent object of its changes, allowing the parent to respond to it. Respectively, the parent notifies its parent and so on until the root node, LightningChart itself, is reached, which then knows how to refresh accordingly.

Chart takes ownership of all the objects given to it and will dispose the objects when it no longer needs them. This includes situations where a new object replacing an old one is given to chart, and the parent of the object is disposed. User must be aware of this, as otherwise it is possible to end up using disposed objects.

If an object is shared between another .NET component and LightningChart, and LightningChart disposes the object, the .NET component is left with a disposed object. LightningChart cannot detect parent sharing between LightningChart and other components.

Important

Sharing objects between other objects in the same chart, or other chart instances, is not allowed.



// Example #1 of wrong usage:
AnnotationXY annotation1 = new AnnotationXY();
chart.ViewXY.Annotations.Add(annotation1);
AnnotationXY annotation2 = new AnnotationXY();
annotation2.Fill = annotation1.Fill;
chart.ViewXY.Annotations.Add(annotation2);
tip

Issue: The same Fill object cannot be shared between multiple objects. Correct way: Only copy properties if they are of ValueType (e.g. Integer, Double, Color)


// Example #2 of wrong usage:
SeriesEventMarker marker = new SeriesEventMarker();
chart.ViewXY.PointLineSeries[0].SeriesEventMarkers.Add(marker);
chart.ViewXY.PointLineSeries[1].SeriesEventMarkers.Add(marker);
tip

Issue: The same object shouldn't be added to a collection of multiple collections. Correct way: Create several markers, one for each series.


Remember to subscribe to ChartMessage event handler. In most cases it reports errors of invalid object sharing cases.