John Lotacs
John Lotacs

Reputation: 1254

Android real time graph adjustments user touch screen

Is there any existing graphing library for android that will allow me to draw a line with an adjustable amount of segments that can be touched and dragged in real time? I currently have a working application using androidplot that captures an image of what I'm scanning and graphs that data. I need adjustable line segment under the graph so the user can choose the area that will be integrated between the curves collected from the data and the adjustable line.

I was unable to find anything in androidplot that might allow me to do this, I'm fine with switching graphing libraries if that is what is needed.

Upvotes: 2

Views: 1203

Answers (1)

ahodder
ahodder

Reputation: 11439

Try looking into Path. That is probably the easiest class to use.

class Graph extends View {
    Graph(Context context) {
        super(context);
        // ... Init paints
    }

    @Override public void onDraw(Canvas canvas) {
        canvas.save(MATRIX_SAVE_FLAG);

        // Draw Y-axis
        canvas.drawLine(axisOffset, axisOffset, axisOffset, canvasHeight-axisOffset, paint);
        // Draw X-axis
        canvas.drawLine(axisOffset, canvasHeight-axisOffset, canvasWidth-axisOffset, canvasHeight-axisOffset, paint);
        canvas.drawPath(new RectF(0.0f, 0.0f, 1.0f, 1.0f), mPath, paint);
        canvas.restore();
    }

    Path mPath = new Path(); // your open path
    float canvasWidth = 1.0f;
    float canvasHeight= 1.0f;
    float axisOffset = 0.1f; // The offset from the border of the canvas

    public void registerDataPlot(int xCoord, int yCoord) {
        // You need to convert the plot data to a location on the canvas
        // Just find the percent value from the base of the axis
        float x = xCoord / (canvasWidth - (2*axisOffset));
        float y = yCoord / (canvasHeight - (2*axisOffset));
        mPath.lineTo(x, y);
    }

Upvotes: 3

Related Questions