Hi I am trying to draw one line graph and for that I am using this code.
DrawGraph dg = new DrawGraph(this);
pane.addView(dg);
dg.setData(10, 10, 100, 100); //cords. for Firs Line
pane.invalidate();
dg.setData(100, 100, 100, 300); //cords. for second Line
pane.invalidate();
and this my DrawGraph class is here.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class DrawGraph extends View{
Paint p;
int x1;
int y1;
int x2;
int y2;
Canvas fc;
int i = 0;
public DrawGraph(Context context) {
super(context);
p = new Paint();
fc = new Canvas();
}
public void setData(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void onDraw(Canvas c){
c.drawLine(x1, y1, x2, y2, p);
}
}
But the problem is there, I can see only one line in the graph (second line) and I need both Lines
I means all the Lines in graph at a time.
any solution please. Thanks.
You have called setData twice, so the second call will overwrite the first values you set.
If you are trying to draw graphs, you can use a library such as AndroidPlot to do the heavy lifting for you. (http://androidplot.com/wiki/Home) There are other libraries but I’ve personally used AndroidPlot and it worked well for me – I had to draw 2 lines on a graph as well.