SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components
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.
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.
I want a functionality where user can drag mouse on surface and create a custom Line hope this func is now available.
Please login first to submit.