SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
What I need is 2 or more VerticalLineAnnotation which can be placed dynamically to act as cursors, showing the difference in value between the two using MVVM and am struggling where to start.
I have been looking at the thread
https://www.scichart.com/questions/question/editing-annotations-and-keeping-track-of-them-in-an-mvvm-application/
but am a bit clueless to what is happening as I am a beginner to C# and MVVM, and am struggling to understand.
I have firstly tried to change your AnnotationMvvm example to use an observable collection as suggested , so that values can be changes, but I can’t even get this to work, as LabelsSource is being returned as null, I am not sure if I have the dependency property correct.I am also not quite sure how the attach etc is working and will struggle to convert to using VerticalLineAnnotation too.
public static readonly DependencyProperty LabelsSourceProperty = DependencyProperty.Register("LabelsSource", typeof(ObservableCollection<IChartSeriesViewModel>), typeof(CustomAnnotationChartModifier), new PropertyMetadata(null, OnLabelsSourceChanged));
// Here LabelsSource is IEnumerable, but you could easily make it ObservableCollection<LabelViewModel>
// in order to get changed notifications when items are added to, or removed from the collection
public ObservableCollection<IChartSeriesViewModel> LabelsSource
{
get { return (ObservableCollection<IChartSeriesViewModel>)GetValue(LabelsSourceProperty); }
set { SetValue(LabelsSourceProperty, value); }
}
// Get a notification when new labels are set.
private static void OnLabelsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var modifier = (CustomAnnotationChartModifier) d;
ObservableCollection<IChartSeriesViewModel> newValue = e.NewValue as ObservableCollection<IChartSeriesViewModel>;
if (newValue == null) return;
modifier.RebuildAnnotations();
}
You say at the end of the thread, you are thinking of doing a good demo on how to do all this, I don’t suppose you could knock me one up using dynamic VerticalLine annotations (MVVM) as above with a display of the difference in the 2 values?
Thanks
Hi there,
Ok, I seem to remember a couple of examples like this asked before. I did a quick search on google for SciChart MVVM VerticalLineAnnotation and came up with this thread on Vertical Lines with a downloadable sample.
Does this sample help?
I’d suggest taking it one step at a time with MVVM, WPF and C#. There are a mixture of concepts there and quite a steep learning curve. I have been coding WPF applications since 2006, and it took a fair amount of that time to get my head around XAML binding! That being said you’re in the right place, if we can help you, we will.
Best regards,
Andrew
while (AnnotationCollection.Count !=0) { var annotation = AnnotationCollection[AnnotationCollection.Count -1]; AnnotationCollection.Remove(annotation); } AnnotationCollection.Clear(); VPManager.InvalidateParentSurface(RangeMode.None);Screenshots included If you could help me on this, thanks
Hi there,
For VerticalLineAnnotation X2 and Y2 are ignored. We have four types of annotation in SciChart which require different coordinates:
So, to get a difference between two VerticalLineAnnotations first you need to use MVVM and databind the VerticalLineAnnotation.X1 property to a property in the viewmodel, e.g.
public double X1First { get { return x1First; } set { x1First = value; OnPropertyChanged("X1First"); OnPropertyChanged("X1Diff"); } } public double X1Second { get { return x1Second; } set { x1Second= value; OnPropertyChanged("X1Second"); OnPropertyChanged("X1Diff"); } }
Next you need to expose a property to output the difference, e.g.
public double X1Diff { get { return Math.Abs(x1First - x1Second); } }
Note I raised PropertyChanged for X1Diff whenever the X1First or X1Second are updated by the user dragging the vertical lines.
Finally you must now databind a TextBox to the X1Diff property to output to the user.
This is similar to the principle we use in the Drag Horizontal threshold example, which uses MVVM to bind a horizontal line’s Y1 property to a ViewModel, then depending on this do some changes.
Regarding the remove annotation leaving them active, this is not something we’ve observed ourselves. Can you help us reproduce it?
E.g. if you go to the Create Annotations Dynamically example and add a vertical line, then remove it, this error does not seem to occur.
Thanks for the code to reproduce, that really helps us identifying issues.
Ok so for some reason the Binding to X1 is keeping the annotation alive, not allowing it to be removed. I made a small modification to your code sample like this, nulling the X1 property (removing the binding) before removing the annotation from the collection. This causes the annotation to be completely removed from the canvas.
private void RemoveVerticalLineAnnotation() { while (AnnotationCollection.Count != 0) { var annotation = AnnotationCollection[AnnotationCollection.Count - 1]; annotation.X1 = null; // Binding keeps annotation alive. Remove it AnnotationCollection.Remove(annotation); } AnnotationCollection.Clear(); n = 0; }
Why this occurs I don’t know, we’ll create a bug for investigation, but for now please use the above workaround.
Best regards,
Andrew
Hi there,
It takes default value from Stroke property of the parent annotation(Vertical/HorizontalLineAnnotation) or from axis ticks color if it’s placed on an axis. To change, just set Foreground property on the label.
Hope this helps,
Best regards,
Yuriy
FYI we fixed the bug of bindings keeping annotations alive in v1.7.0.2424, which was released yesterday.
Best regards,
Andrew
Please login first to submit.