SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy. We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.

0
0

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

 http://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

  • You must to post comments
0
0

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

  • wilx
    Thanks for your reply ( I have edited this post as I sorter out placing 2 lines) I also need to show the difference between the 2 values 1st annotation at x = 2.62 2nd annotation at 3.71 ( both which can be dragged) and some way of showing what the difference is between the two (0.09). Basically they are a set of cursors like you would find on an oscilloscope.Can you explain what the X2 and Y2 values are for?Thanks
  • wilx
    I have tried adding a remove to the example referenced above. When I try and remove the annotations from the collection, either by clearing it or removing one by one, it does not remove them properly as you can see.If you float the mouse over them, you can still see the drag arrows. Trying to drag one of the removed annotations( which obviously you should not be able to do) also gives a Object reference not set to an instance of an object.exception
    
                    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
  • You must to post comments
0
0

Hi there,

For VerticalLineAnnotation X2 and Y2 are ignored. We have four types of annotation in SciChart which require different coordinates:

  • Types which require X1,Y1,X2,Y2 include LineAnnotation, LineArrowAnnotation, BoxAnnotation
  • Types which require X1,Y1 only include TextAnnotation, CustomAnnotation
  • Types which require just X1 include VerticalLineAnnotation
  • Types which require just Y1 include HorizontalLineAnnotation, AxisMarkerAnnotation

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.

  • Andrew
  • wilx
    Here is your example modified to remove lines as well as add using MVVM , replace your files posted above with these.Can you tell me how to change the colour of the Annotation Label text too please.
  • wilx
    Did my code help you find the problem?
  • You must to post comments
0
0

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

  • wilx
    Thanks, that did the trick. Is it possible to change the Text colour on the annotation label?
  • You must to post comments
0
0

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

  • You must to post comments
0
0

FYI we fixed the bug of bindings keeping annotations alive in v1.7.0.2424, which was released yesterday.

Best regards,
Andrew

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.