How to Draw graph in Android?

How about trying OpenGL ES ?

you can create a GraphView which will extends GLSurfaceView

example code-

public class GraphView extends GLSurfaceView {

private Renderer renderer;

public GraphView(Context context) {
    super(context);
    renderer = new GraphRenderer();
    setRenderer(renderer);
}
}

And your GraphRender

ublic class GraphRenderer implements Renderer {

public void onDrawFrame(GL10 gl) {
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();

GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 1.0f);
gl.glColor4f(1, 0, 0, .5f);
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);

float ratio = (float) width / height;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

}

private void drawGraph(GL10 gl) {
gl.glLineWidth(1.0f);

// put your code here ..


}

public static int loadShader(int type, String shaderCode) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}

}

You can try this way.


You could create a SurfaceView, in which you can draw to a Canvas in the onDraw() method. To draw your graph, you can use the Path class, and it's moveTo() and lineTo() methods. To change the appearance of the lines, use the Paint class. Then use the Canvases drawPath() method, which takes a Path, and a Paint object. I think it's a bit easier to start with, than OpenGl.

  • SurfaceView
  • Canvas
  • Path
  • Paint

Some tutorial

Update: @Shakti Malik found a pretty good looking library, which looks easy to use: MPAndroidChart

Tags:

Android

Graph