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.
- Ray Hung asked 7 years ago
- You must login to post comments
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 answered 7 years ago
- last edited 7 years ago
-
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.
-
I have solved this by setting the correct Axis ID, thanks
- You must login to post comments
I want a functionality where user can drag mouse on surface and create a custom Line hope this func is now available.
- Abhishek vyas answered 6 years ago
-
Unfortunately this functionality isn’t available our of the box in Android. However I think it’s possible to implement it by creating custom modifier and annotation. We can implement desired functionality for you as part of a short term consultancy project on the hourly rate basis. If you’re interested please contact with our sales department for more details.
- You must login to post comments
Please login first to submit.