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

Answered
0
0

Or say, how can I prevent movement of HorizontalLineAnnotation when dragging the grip of X1(typically the left grip)?

Version
2.2.2.2424
  • You must to post comments
Great Answer
0
0

Hi Zhengyang,

You can do this by customizing HozizontalLineAnnotation. There you can override several methods which return IAnnotationAdornerAction depending on where you touched annotation:

class CustomHorizontalLineAnnotation extends HorizontalLineAnnotation {

    public CustomHorizontalLineAnnotation(Context context) {
        super(context);
    }

    @Override
    protected IAnnotationPlacementStrategy initPlacementStrategy(CoordinateSystem newCoordinateSystem) {
        return new CustomPlacementStrategy(this, true);
    }

    private static class CustomPlacementStrategy extends HorizontalLineAnnotation.CartesianAnnotationPlacementStrategy {

        protected CustomPlacementStrategy(HorizontalLineAnnotation annotation, boolean clipAdornerToAnnotationSurface) {
            super(annotation, clipAdornerToAnnotationSurface);
        }

        @Override
        protected IAnnotationAdornerAction createAdornerActionForResizingGripWithIndex(int resizingGripIndex, int resizingGripXOffset, int resizingGripYOffset) {
            // this is action which is used when you drag resizing grip
            // by default it resizes line annotation

            // to disable action return null
            // return super.createAdornerActionForResizingGripWithIndex(resizingGripIndex, resizingGripXOffset, resizingGripYOffset);
            return null;
        }

        @Override
        protected IAnnotationAdornerAction createAdornerActionForAnnotationHit() {
            // this is action which is used when you drag line itself
            // by default it drags line without resizing
            return super.createAdornerActionForAnnotationHit();
        }

        @Override
        protected IAnnotationAdornerAction createAdornerActionForAnnotationLabel(AnnotationLabel label) {
            // this is action which is used when you drag annotation label
            // by default it drags line
            return super.createAdornerActionForAnnotationLabel(label);
        }
    }
}

You can disable those actions by returning null or empty IAnnotationAdornerAction implementation when you need it.

Is this suitable for your needs?

Best regards,
Yura

  • zhengyang qu
    Thanks, I will have a try. Currently I wrote a stateful OnAnnotationDragListener to reset Y coordinate of the horizontal line.
  • You must to post comments
0
0

Well, I think I need to re-implement one more interface to satisfy my need. Code below prevents movement when dragging left grip of a Horizontal Line (will not affect resizing).

class CustomHorizontalLineAnnotation extends HorizontalLineAnnotation {

    class CustomAnnotationAdornerAction implements IAnnotationAdornerAction {
        private final AnnotationBase annotation;
        private final int index;
        private float x;
        private float y;

        public CustomAnnotationAdornerAction(AnnotationBase var1, int var2, float var3) {
            annotation = var1;
            index = var2;
            x = var3;
        }

        public void onAdornerDragStarted(float xStartPoint, float yStartPoint) {
            x += xStartPoint;
            y = getYAxis().getCoordinate(annotation.getY1());
        }

        public void onAdornerDragDelta(float horizontalOffset, float verticalOffset) {
            x += horizontalOffset;
            annotation.moveBasePointTo(x, y, index);
        }

        public void onAdornedDragEnded() {
        }
    }

    public CustomHorizontalLineAnnotation(Context context) {
        super(context);
    }

    @Override
    protected IAnnotationPlacementStrategy initPlacementStrategy(CoordinateSystem newCoordinateSystem) {
        return new CustomPlacementStrategy(this, true);
    }

    private class CustomPlacementStrategy extends HorizontalLineAnnotation.CartesianAnnotationPlacementStrategy {

        protected CustomPlacementStrategy(HorizontalLineAnnotation annotation, boolean clipAdornerToAnnotationSurface) {
            super(annotation, clipAdornerToAnnotationSurface);
        }

        @Override
        protected IAnnotationAdornerAction createAdornerActionForResizingGripWithIndex(int resizingGripIndex, int resizingGripXOffset, int resizingGripYOffset) {
            // this is action which is used when you drag resizing grip
            // by default it resizes line annotation

            // to disable action return null
            return new CustomAnnotationAdornerAction(this.annotation, resizingGripIndex, resizingGripXOffset);
        }

        @Override
        protected IAnnotationAdornerAction createAdornerActionForAnnotationHit() {
            // this is action which is used when you drag line itself
            // by default it drags line without resizing
            return super.createAdornerActionForAnnotationHit();
        }

        @Override
        protected IAnnotationAdornerAction createAdornerActionForAnnotationLabel(AnnotationLabel label) {
            // this is action which is used when you drag annotation label
            // by default it drags line
            return super.createAdornerActionForAnnotationLabel(label);
        }
    }
}
  • zhengyang qu
    I think this could be done more easily.
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.