2

I am new to android and I need to know how to make a graph or chart (eg. line chart) move i.e, from right to left.

Basically I am going to draw the graph in accordance with the RAM Memory Usage of the phone. I need to draw a graph similarly present in Task Manager of Windows.

Please help on this.

Sudarshan
  • 1,291
  • 3
  • 26
  • 35
  • i think you should look to this tutorial http://www.jjoe64.com/p/graphview-library.html Author has givenn the right things to explain – raghav chopra Sep 01 '12 at 13:09

3 Answers3

3

Take A Look This Class.

It's Simple and Useful.

Original Author is Arno den Hond, Google it.

I redesign this Class.

import android.content.Context; 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.view.View;

/**
 * GraphView creates a scaled line or bar graph with x and y axis labels. 
 * @author Arno den Hond
 * @redesign Reinhard & kimkmkm
 *
 */
public class GraphView extends View {

public static boolean BAR = true;
public static boolean LINE = false;

private Paint paint;
private float[] values;
private String[] horlabels;
private String[] verlabels;
private String title;
private boolean type;

/**
 *  Generate Graph
 *  Variable Array for GraphView
 *  verlabel : Background Height Values
 *  horlabel : Background Width Values
 *  values : Max Values of Foreground Active Graph
 *  
 *  basic draw rule
 *  if Array is not null
 *  Draw Background width, height & active Graph all time
 *  or Array is null
 *  Draw Background width, height
 *  active Graph fixed 0
 *    
 */
public GraphView(Context context, float[] values, String title, String[] horlabels, String[] verlabels, boolean type) {
    super(context);
    if (values == null)
        values = new float[0];
    else
        this.values = values;
    if (title == null)
        title = "";
    else
        this.title = title;
    if (horlabels == null)
        this.horlabels = new String[0];
    else
        this.horlabels = horlabels;
    if (verlabels == null)
        this.verlabels = new String[0];
    else
        this.verlabels = verlabels;
    this.type = type;
    paint = new Paint();
}
/**
 *  Graph for Background
 *  &
 *  Value for Background
 */
@Override
protected void onDraw(Canvas canvas) {
    float border = 20;
    float horstart = border * 2;
    float height = getHeight();
    float width = getWidth() - 1;
    float max = 700;
    float min = 0;
    float diff = max - min;
    float graphheight = height - (2 * border);
    float graphwidth = width - (2 * border);

    paint.setTextAlign(Align.LEFT); 
    /** vers : BackGround Height Values length*/
    int vers = verlabels.length - 1; 
    for (int i = 0; i < verlabels.length; i++) {

        paint.setColor(Color.LTGRAY);
        // Width Line of background

        float y = ((graphheight / vers) * i) + border;
        // float y : ((getHeight / Height values length) * Height values length ) + 20

        canvas.drawLine(horstart, y, width, y, paint);
        // drawLine ( 40, y, getWidth()-1, y, paint)

        paint.setColor(Color.WHITE);
        // Left Height of background

        canvas.drawText(verlabels[i], 0, y, paint);
    }

    /** hors : BackGround width Values length*/
    int hors = horlabels.length - 1;
    for (int i = 0; i < horlabels.length; i++) {
        // Height Line of background
        paint.setColor(Color.DKGRAY); 
        float x = ((graphwidth / hors) * i) + horstart;
        canvas.drawLine(x, height - border, x, border, paint);
        paint.setTextAlign(Align.CENTER);
        if (i==horlabels.length-1)
            paint.setTextAlign(Align.RIGHT);
        if (i==0)
            paint.setTextAlign(Align.LEFT);
         // Value of Width
        paint.setColor(Color.WHITE); 
        canvas.drawText(horlabels[i], x, height - 4, paint);
    }

    paint.setTextAlign(Align.CENTER);
    canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);

    /**
     * Yellow Line Graph
     * continue Repaint....
     * 
     */
    if (max != min) {
        paint.setColor(Color.YELLOW);
        if (type == BAR) {
            float datalength = values.length;
            float colwidth = (width - (10 * border)) / datalength;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;

                float rat = val / diff;
                //diff : max - min

                float h = graphheight * rat;
                canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);
            }
        } else {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            float halfcol = colwidth / 2;
            float lasth = 0;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                if (i > 0)
                    canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
                lasth = h;
            }
        }
    }
}
reinhard.lee
  • 503
  • 4
  • 10
  • 24
  • Sir can u explain what is happening with this code...Atlest a screen shot so i can understand the concept – Sudarshan Jun 12 '12 at 09:21
  • @user1448299 Sorry, I forget about it. this is [link](http://cfile23.uf.tistory.com/image/2041E13A4F01569737FB1D) sample screenshot. this graph is moving and refresh. – reinhard.lee Jun 12 '12 at 14:41
  • @user1448299 and [Sample Graph Movie By Flash](http://tvpot.daum.net/clip/ClipViewByVid.do?vid=zJ5VrtWuiwo$) – reinhard.lee Jun 12 '12 at 14:44
  • @ reinhard.lee Sir for me chart is not moving...I need to give the Memory Info value as a value to the line graph... I ll get Memory info using the following code " MemoryInfo mi = new ActivityManager.MemoryInfo(); mi.availMem " Now i need to send this value to the graph and also i need to update the graph for a period of 1 sec..Can u help me on this – Sudarshan Jun 13 '12 at 08:22
2

It's My Sample Code.

I did not test yet.

But I think This code works fine.

public class DrawGraph extends Activity{
    /**
     * Variable Array for GraphView
     * verlabel : Background Height Values
     * horlabel : Background Width Values
     * values : Max Values of Foreground Active Graph
     */
    private float[] values = new float[60];
    private String[] verlabels = new String[] { "600","500","400","300","200","100","80","60","40","20","0", };
    private String[] horlabels = new String[] { "0","10", "20", "30", "40", "50", "60"};
    private GraphView graphView;
    private LinearLayout graph;
    private boolean runnable = false;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        graph = (LinearLayout)findViewById(R.id.graph);
        graphView = new GraphView(DrawGraph.this, values, "TEST GRAPH", horlabels, verlabels, GraphView.LINE);
        graph.addView(graphView);
        runnable = true;
        startDraw.start();
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        runnable = false;
    }

    public void setGraph(int data){
        for(int i=0; i<values.length-1; i++){
            values[i] = values[i+1];
        }

        values[values.length-1] = (float)data;
        graph.removeView(graphView);
        graph.addView(graphView);
    }        

    public Handler handler = new Handler(){
        @Override
        public void handleMessage(android.os.Message msg){
            switch(msg.what){

            case 0x01:
               int testValue = (int)(Math.random() * 600)+1;
               setGraph(testValue);
               break;
            }
        }
    }

    public Thread startDraw = new Thread(){
        @Override
        public void run(){
            while(runnable){
                handler.sendEmptyMessage(0x01);
                try{
                    Thread.sleep(1000);
                } catch (Exception e){
                     e.printstacktrace();
                }
            }
        }
    }
reinhard.lee
  • 503
  • 4
  • 10
  • 24
  • @reinhard.lee...Sir i had fixed it,..Thanks for ur help..its working fine..Thank u very much – Sudarshan Jun 13 '12 at 12:04
  • [link](http://stackoverflow.com/questions/10975548/how-to-increase-the-ram-memory-usage-programatically)...Sir can u check on the link and if u have any idea share it...Thanks – Sudarshan Jun 14 '12 at 11:22
0

Try AndroidPlot, it's a simple to use library!

See also achartengine.

blackwolf
  • 927
  • 2
  • 11
  • 23
  • Hi sir,Hi had checked achartengine but in that the chart are static till we scroll..But i need the graph in marquee style – Sudarshan Jun 12 '12 at 09:14