SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and iOS Chart & 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

Dear All,

I would like to ask are there any method to draw a line (or annotation) by user touch event ?

For example user touch the chart to set the starting point of the line and then touch/drag to another point to draw the line.

Thanks.

Version
v1.2.1
  • You must to post comments
Best Answer
1
2

Hi Ray,

Unfortunately we don’t support such functionality out of the box for now but we’re planning to add such functionality in one of our future releases. For now you can create and use custom modifier which implements desired functionality.

I have created a simple prototype which creates LineAnnotation on drag. Here is my code:

public class LineDrawingModifier extends TouchModifierBase {
    private final PenStyle srokeStyle = new PenStyle(Color.RED, true, 10);
    private LineAnnotation lineAnnotation;

    @Override
    protected boolean onTouchDown(ModifierTouchEventArgs args) {
        final float x = args.e.getX();
        final float y = args.e.getY();

        lineAnnotation = new LineAnnotation(getContext());

        lineAnnotation.setStroke(srokeStyle);

        getParentSurface().getAnnotations().add(lineAnnotation);

        setLineStart(lineAnnotation, x, y);
        setLineEnd(lineAnnotation, x, y);

        return true;
    }

    @Override
    protected boolean onTouchMove(ModifierTouchEventArgs args) {
        if (lineAnnotation != null) {
            final float x = args.e.getX();
            final float y = args.e.getY();

            setLineEnd(lineAnnotation, x, y);

            return true;
        } else
            return super.onTouchMove(args);
    }

    @Override
    protected boolean onTouchUp(ModifierTouchEventArgs args) {
        if (lineAnnotation != null) {
            final float x = args.e.getX();
            final float y = args.e.getY();

            setLineEnd(lineAnnotation, x, y);

            return true;
        } else
            return super.onTouchUp(args);
    }

    private static void setLineStart(@NonNull LineAnnotation lineAnnotation, float xStart, float yStart) {
        final Comparable xValue = lineAnnotation.getXAxis().getDataValue(xStart);
        final Comparable yValue = lineAnnotation.getYAxis().getDataValue(yStart);

        lineAnnotation.setX1(xValue);
        lineAnnotation.setY1(yValue);
    }

    private static void setLineEnd(@NonNull LineAnnotation lineAnnotation, float xEnd, float yEnd) {
        final Comparable xValue = lineAnnotation.getXAxis().getDataValue(xEnd);
        final Comparable yValue = lineAnnotation.getYAxis().getDataValue(yEnd);

        lineAnnotation.setX2(xValue);
        lineAnnotation.setY2(yValue);
    }

}

Hope this will help you!

Best regards,
Yura.

  • Yura Khariton
    Hi Ray. The only line which I had to change in code above was the line with definition of strokeStyle – in v2.0 StrokeStyle was refactored into an abstract class and now you need to use SolidPenStyle instead. The rest of the code does not require any changes. I tried to run it and I haven’t seen any exceptions. Did you change axis ids for anotations or axes in your application? I would suggest to check if your annotation or axis have correct axis ids – if you get null when calling getXAxis()/getYAxis() for anotation this means that it can’t locate appropriate axis in XAxes or YAxes collections of the chart.
  • Ray Hung
    I have solved this by setting the correct Axis ID, thanks
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.