Pre loader

Text Annotations and the Zoom Extents Modifier

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

1
0

I have a real time charting program that is getting data from accel sensors. When the sampling is complete, I allow them to add annotations to the graph. I also have the double-click ZoomExtentsModifier active, which normally works great. I added a text annotation but when I double-click it doesn’t include the text annotation in the extents and as a result the text is not showing on the screen. Is there something I am doing wrong? I don’t want the user to get confused when the text isn’t visible. I allow them to save and re-load so on the re-load the text doesn’t show unless the user zooms out.

Version
V4.2.4
  • You must to post comments
0
1

Hi Richard,

The ZoomExtentsModifier zooms to fit the chart to the available series. ZoomExtents does not take into account annotations in the viewport, only series.

The best workaround is to override the ViewportManager to include the Annotations in the viewport on Zoom Extents.

To do this I’ve used the ViewportManager feature and code as follows:

public class ViewportManagerWithAnnotations : DefaultViewportManager
{
    private ISciChartSurface _parentSurface;

    public override void AttachSciChartSurface(ISciChartSurface scs)
    {
        _parentSurface = scs;

        base.AttachSciChartSurface(scs);
    }

    protected override IRange OnCalculateNewXRange(IAxis xAxis)
    {
        var xAxisRange = base.OnCalculateNewXRange(xAxis);
        return MergeRanges(xAxis, xAxisRange);
    }        

    protected override IRange OnCalculateNewYRange(IAxis yAxis, RenderPassInfo renderPassInfo)
    {
        var yAxisRange = base.OnCalculateNewYRange(yAxis, renderPassInfo);
        return MergeRanges(yAxis, yAxisRange);
    }

    private IRange MergeRanges(IAxis axis, IRange axisRange)
    {
        // Get the autorange for the annotations
        var annotationsRange = CalculateAutoRangeForAnnotations(axis);

        // merge with the axis range 
        if (annotationsRange.IsDefined)
        {
            axisRange = axisRange.Union(annotationsRange);
            axisRange = axisRange.GrowBy(axis.GrowBy.Min.ToDouble(), axis.GrowBy.Max.ToDouble());
        }

        return axisRange;
    }

    private IRange CalculateAutoRangeForAnnotations(IAxis axis)
    {
        double min = double.MaxValue;
        double max = double.MinValue;

        // loop through annoations
        foreach (var annotation in _parentSurface.Annotations)
        {
            // is this annoation registered on this axis 
            bool axisMatch = (axis.IsXAxis && annotation.XAxisId == axis.Id) ||
                             (!axis.IsXAxis && annotation.YAxisId == axis.Id);

            if (axisMatch)
            {
                // Accumulate the min, max X1/Y1
                min = Math.Min((axis.IsXAxis ? annotation.X1 : annotation.Y1).ToDouble(), min);
                max = Math.Max((axis.IsXAxis ? annotation.X1 : annotation.Y1).ToDouble(), max);

                // For annotations that have two points
                if (!(annotation is AnchorPointAnnotation))
                {
                    // Accumulate the min, max X2/Y2
                    min = Math.Min((axis.IsXAxis ? annotation.X2 : annotation.Y2).ToDouble(), min);
                    max = Math.Max((axis.IsXAxis ? annotation.X2 : annotation.Y2).ToDouble(), max);
                }
            }
        }

        return new DoubleRange(min, max);
    }
}

and in the View

    <s:SciChartSurface>
        <s:SciChartSurface.XAxis>
            <s:NumericAxis GrowBy="0.1, 0.1"/>
        </s:SciChartSurface.XAxis>
        <s:SciChartSurface.YAxis>
            <s:NumericAxis GrowBy="0.1, 0.1"/>
        </s:SciChartSurface.YAxis>

        <s:SciChartSurface.Annotations>
            <s:TextAnnotation Text="Hi there!" X1="-5" Y1="11" HorizontalAnchorPoint="Center" VerticalAlignment="Center" />
            <s:TextAnnotation Text="Here are some annotations" X1="5" Y1="5" HorizontalAnchorPoint="Center" VerticalAlignment="Center" />
            <s:TextAnnotation Text="Included in AutoRange!" X1="15" Y1="-2" HorizontalAnchorPoint="Center" VerticalAlignment="Center" />
        </s:SciChartSurface.Annotations>

        <s:SciChartSurface.ViewportManager>
            <local:ViewportManagerWithAnnotations/>
        </s:SciChartSurface.ViewportManager>
    </s:SciChartSurface>

This results in AutoRange considering the annotation positions.

Let me know if this helps,

Best regards,
Andrew

  • You must to post comments
0
0

In OnCalculateNewXRange or OnCalculateNewYRange, is there a way to differentiate whether its triggered by zoom extents or manual user action like rubberband zoom or mouse wheel scroll or pan?

We want to merge annotation range only when during zoom extent, but when user selects small portion of chart and do rubber band zoom, plot should be visible only for rubber band area. Currently with this code, we cannot zoom out beyond annotations area.

  • You must to post comments
Showing 2 results
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