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).
- Uwe Hafner asked 9 years ago
- You must login to post comments
Hi Uwe,
Simple way to do this
- Handle AxisBase.VisibleRangeChanged event
- Check AxisBase.VisibleRange.AsDoubleRange().Min and Max
- 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
- Andrew Burnett-Thompson answered 9 years ago
-
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.
-
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.
-
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.
-
I know. I am evaluating :).
-
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 login to post comments
Please login first to submit.