Pre loader

Dynamic Horizontal Annotation using MVVM

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

0
0

Hi,

Was hoping you could point me in the right direction. I’m trying to create horizontal line annotations on demand using MVVM but have become stuck. I’ve have them working in MVVM when the chart loads but not when a button is clicked although I have achieved this using code behind.

I’ve tried using an observable collection of AnnotationCollection type but am struggling to add the horizontal lines annotations to it as they are of a different type.

Any pointers in the right direction would be much appreciated!

Thanks,
Tim

Example of MVVM

TradeAnnotations = CreateHorizontalAnnotations();

private AnnotationCollection _annotations;

    public AnnotationCollection TradeAnnotations
    {
        get { return _annotations; }
        set
        {
            _annotations = value;
            OnPropertyChanged("TradeAnnotations");
        }
    }

    private static AnnotationCollection CreateHorizontalAnnotations()
    {
        //var annotations = new AnnotationCollection();

        var annotations = new AnnotationCollection();

        Color strokeColor = (Color)ColorConverter.ConvertFromString("#008B45");
        Brush strokeBrush = new SolidColorBrush(strokeColor);

        HorizontalLineAnnotation annotation = new HorizontalLineAnnotation();

        annotation.ShowLabel = true;
        annotation.LabelPlacement = LabelPlacement.Axis;
        annotation.HorizontalAlignment = HorizontalAlignment.Stretch;
        annotation.IsEditable = true;
        annotation.SnapsToDevicePixels = true;
        annotation.Stroke = strokeBrush;

        annotation.X1 = DateTime.Now;
        annotation.Y1 = 129;

        annotations.Add(annotation);

        return annotations;          
    }

Example of code behind

        private void CreateHorizontalAnnotations(object sender, System.Windows.RoutedEventArgs e)
    {
        //var annotations = new AnnotationCollection();

        var annotationCollection = new AnnotationCollection();

        Color strokeColor = (Color)ColorConverter.ConvertFromString("#008B45");
        Brush strokeBrush = new SolidColorBrush(strokeColor);

        HorizontalLineAnnotation annotation = new HorizontalLineAnnotation();

        annotation.ShowLabel = true;
        annotation.LabelPlacement = LabelPlacement.Axis;
        annotation.HorizontalAlignment = HorizontalAlignment.Stretch;
        annotation.IsEditable = true;
        annotation.SnapsToDevicePixels = true;
        annotation.Stroke = strokeBrush;

        annotation.X1 = DateTime.Now;
        annotation.Y1 = 129;

        this.priceChart.Annotations.Add(annotation);
    }
  • You must to post comments
0
0

Hi Tim,

There are lots and lots of examples of best practices with SciChart & MVVM Best Practices. Just do a quick search for SciChart MVVM on google. Here are the top 5:

  1. Databinding Annotations with MVVM
  2. Best practices for SciChart ViewModels in MVVM | SciChart
  3. Best Practies for SciChart MVVM (2)
  4. Tag: MVVM on SciChart Forums
  5. Editing annotations and keeping track of them in an mvvm application

This last one (5) is probably the most helpful for you. Let me know if this helps!

Best regards,
Andrew

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies