10-21-2014 09:03 PM
I'm creating a WPF application that displays a graph of data (several plots) and programmatically adds several annotations. The user is able to drag the annotation labels around (InteractionMode = AnnotationInteractionModes.DragLabel). I need to be able to save the new label position and load it back later.
I'm using the NationalInstruments.Controls.PointAnnotation object an a NationalInstruments.Controls.Graph.
I had thought it would be reflected in the LabelAlignment, but that doesn't appear to change from the value I set when I initially create the annotation.
Here is how I'm adding the annotations to the graph:
/// <summary> /// Add the annotations to the graph /// </summary> /// <param name="dataHandler"></param> /// <param name="StartDate"></param> /// <param name="EndDate"></param> private void DisplayAnnotations(DataHandler dataHandler, DateTime StartDate, DateTime EndDate) { PointAnnotation tmpAnnotation = new PointAnnotation(); //Iterate through all the annotations in the source data set foreach (Post_Job_Charting.Annotation dataPointAnnotation in dataHandler.Annotations) { TimeSpan DateDiff = EndDate - StartDate; TimeSpan HalfWay = new TimeSpan(StartDate.AddSeconds(DateDiff.TotalSeconds / 2.0).Ticks); //Only add annotations that are in the visible data range if (dataPointAnnotation.Date >= StartDate && dataPointAnnotation.Date <= EndDate) { tmpAnnotation = new PointAnnotation(); tmpAnnotation.HorizontalPosition = dataPointAnnotation.Date; tmpAnnotation.VerticalPosition = dataPointAnnotation.Value; tmpAnnotation.Label = dataPointAnnotation.Text; if (dataPointAnnotation.Date.Ticks < HalfWay.Ticks) { tmpAnnotation.LabelAlignment = new AnnotationLabelAlignment(BoundsAlignment.None, 0, -25); } else { tmpAnnotation.LabelAlignment = new AnnotationLabelAlignment(BoundsAlignment.None, -75, -25); } tmpAnnotation.InteractionMode = AnnotationInteractionModes.DragLabel; //Show/Hide the annotation if (dataPointAnnotation.Display) { tmpAnnotation.Visibility = System.Windows.Visibility.Visible; } else { tmpAnnotation.Visibility = System.Windows.Visibility.Hidden; } //Tag the annotation so I can find it later tmpAnnotation.Tag = dataPointAnnotation.ID; //Display annotation on the graph niGraph.Children.Add(tmpAnnotation); } } }
Once I have the position of the label, I need to be able to reset the label to that position, but first things first, I need to get that position.
Any help you can offer would be greatly appreciated.
Best regards,
-Marcel
Solved! Go to Solution.
10-22-2014 10:28 AM
There is no property on the point annotation that exposes the position of the label. I have created a task to look into adding this.
However, you can monitor the changes by using a custom BoundsAlignment
implementation that records the new offset as it is updated:
public sealed class RecordingBoundsAlignment : BoundsAlignment {
public Vector LastOffset { get; private set; }
public override Point AdjustPosition( AlignmentArgs args ) {
LastOffset = new Vector( args.XOffset, args.YOffset );
return None.AdjustPosition( args );
}
}
When you create the annotation, you would use an instance of the recording alignment, and when you wanted to save the position you would read the value from the recorder:
tmpAnnotation.LabelAlignment = new AnnotationLabelAlignment(
new RecordingBoundsAlignment( ),
initialXOffset,
initialYOffset );
// ...
var recorder = (RecordingBoundsAlignment)tmpAnnotation.LabelAlignment.Alignment;
Save( recorder.LastOffset );
10-23-2014 07:30 PM
Fantastic solution! Thank you very much, I really appreciate your help.