Editing Annotations via User Drag
All annotations support dragging and repositioning via the mouse when AnnotationBase.IsEditable = true. Similarly, to make an annotation read-only, set IsEditable = false.
Creating Annotations on Mouse-Clicks
SciChart features a ChartModifier to help you create annotations on mouse click/drag. This is the AnnotationCreationModifier.
Adding an AnnotationCreationModifier in XAML
To add an AnnotationCreationModifier to your chart, simply declare it in XAML as follows:
Adding an AnnotationCreationModifier |
Copy Code |
---|---|
<s:SciChartSurface> <!-- XAxis, YAxis, RenderableSeries omitted for brevity --> <s:SciChartSurface.ChartModifier> <s:ModifierGroup> <s:AnnotationCreationModifier x:Name="annotationCreation" AnnotationCreated="OnAnnotationCreated" AnnotationType="{x:Type s:LineAnnotation}" AnnotationStyle="{StaticResource LineAnnotationStyle}/> <s:YAxisDragModifier/> <s:XAxisDragModifier/> </s:ModifierGroup> </s:SciChartSurface.ChartModifier> </s:SciChartSurface> |
The AnnotationCreationModifier will listen to mouse-clicks and create an annotation for you. It creates the annotation specified by the AnnotationCreationModifier.AnnotationType. It applies the style specified by AnnotationCreationModifier.AnnotationStyle to the newly created annotation. If you want to change the annotation type or style dynamically then set these properties in code or via a binding.
Adding an AnnotationCreationModifier in Code
To add an AnnotationCreationModifier to your chart, simply declare the following C# code:
Adding an AnnotationCreationModifier |
Copy Code |
---|---|
// Create a SciChartSurface. XAxis, YAxis omitted for brevity var sciChartSurface = new SciChartSurface(); var annotationCreationModifier = new AnnotationCreationModifier() { AnnotationType = typeof(LineAnnotation), AnnotationStyle = this.TryFindResource("SomeStyleDeclaredInXaml") as Style, }; sciChartSurface.ChartModifier = new ModifierGroup( annotationCreationModifier, new XAxisDragModifier(), new YAxisDragModifier()); |
Rolling Your Own Annotation Creation code
You don’t have to use our AnnotationCreationModifier. Fact is, we simple create annotations on mouse-clicks, so you could do the following.
In pseudocode:
On SciChartSurface MouseDown
Convert MousePoint to X,Y data values.
Create an annotation.
Set Annotation.X1, Y1 to data-values calculated above
Add Annotation to SciChartSurface.Annotations
On SciChartSurface MouseMove
Convert MousePoint to X,Y data-values
Update Annotation.X2,Y2 to the latest X,Y data value
On SciChartSurface MouseUp
Convert MousePoint to X,Y data-values
Set Annotation.X2,Y2 to the latest X,Y data value
Clear any flags to stop dragging/updating annotations