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.

Answered
0
0

See the picture how my chart looks. The vertical line was created when pressing the stop button. It is a Fifo chart so the annotation will leave the view at some point in the future.

The code to create the annotation was taken from this sample: https://www.scichart.com/questions/question/add-vertical-line-annotation-programmatically

My real code looks like this:

     private void OnStopChartCommand()
    {
        // create vertical line
        if (_chartSeries.Count > 0)
        {
            IXyDataSeries<double, double> dataSeries = _chartSeries[0].DataSeries as IXyDataSeries<double, double>;
            if (dataSeries != null && dataSeries.XValues.Count > 0)
            {
                double lastValue = dataSeries.XValues.Last();
                AnnotationCollection.Add(CreateVerticalLineAnnotation(lastValue));
            }
        }
    }

    public AnnotationCollection AnnotationCollection
    {
        get { return _annotationCollection; }
        set
        {
            _annotationCollection = value;
            OnPropertyChanged();
        }
    }

    private IAnnotation CreateVerticalLineAnnotation(double xPlacement)
    {
        var annotation = new VerticalLineAnnotation
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            StrokeThickness = 2,
            Stroke = new SolidColorBrush(Colors.Black),
            CoordinateMode = AnnotationCoordinateMode.Absolute,
            IsEditable = true,
            X1 = xPlacement,
            XAxisId = ChartConfigVm.XAxis.AxisId,
            YAxisId = ChartConfigVm.YAxes[0].AxisId
        };

        return annotation;
    }

So far, so good. Everything works fine. But of course when pressing the button new annotations are added and the old ones are not removed.
I would like them removed when they are not visible anymore on this FiFo Chart. But not when pressing the button because I don’t know when that will happen.
Does someone have an idea how to do this once the annotation leaves the chart? This is not timecritical to me but it should happen at some point if possible automatically (or automatically triggered – I can write code to remove it).

Images
  • You must to post comments
Best Answer
0
0

Hi Uwe,

Simple way to do this

  1. Handle AxisBase.VisibleRangeChanged event
  2. Check AxisBase.VisibleRange.AsDoubleRange().Min and Max
  3. Iterate over SciChartSurface.Annotations, remove those of type VerticalLineAnnotation where X1 is outside Min and Max

You will need to be a bit clever, e.g. if you are continually adding and removing annotations this will slow down the entire chart. We already virtualize to a certain extend by setting Visibility=Collapsed for annotations outside of the viewport.

Best regards,
Andrew

  • Uwe Hafner
    Thanks. I will try that. Adding will not be done all the time. It is done when the chart is stopped to visualize that there is a jump in the timeline (x-axis) because it shall not be a visible gap. So I think it should not be a performance issue as the chart is stopped anyway. I am just a bit afraid, that handling the VisibleRangeChanged could be a performance issue. As that range will change very fast (I believe) when adding points at a high rate (up to > 10.000 points/second on 8 series in parallel). But I will see when testing.
  • Uwe Hafner
    I am loosing about 8-10% performance in my test scenario (which mocks the worst case scenario). I have to lock the collection and iterate it on every event.
  • Andrew
    I'm sure you can think of a few ways to handle this :) I've just given you one. You can also check once a second for annotations out of viewport. Or, when appending data, Or, when SciChartSurface.Rendered is fired. Or by throttling one or more of the above events. There's a nice explanation of Throttling here: http://drupalmotion.com/article/debounce-and-throttle-visual-explanation which can be achieved in C# by no more than a timer, reset when an event occurs, and doing your cleanup action when it elapses.
  • Uwe Hafner
    I know. I am evaluating :).
  • Andrew
    I have another idea for you. Have a timer that runs once a second. Iterate over the AnnotationCollection looking for VerticalLineAnnotations with X1 less that DataSeries.XValues[0] (This is the earliest DataSeries value). Remember FIFO data series scroll so the earliest data-series value is the earliest data you have.
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.