2011-03-14 2 views
6

나는 새로운 자바이고 안드로이드에 익숙하다. 나는 날짜를 기준으로 사용자의 데이터를 그래프로 보여주는 Android 앱을 작성하려고합니다. JavaScript를 사용하지 않고 Java에서이 작업을 수행 할 수 있습니까? 코드 아래안드로이드에서 그래프와 차트를 그리는 방법이 있습니까?

+3

가능한 중복 : [? 안드로이드에 대한 모든 좋은 그래프 패키지 (http://stackoverflow.com/questions/424752/any-good-graphing-packages for-android) –

+0

실제로 가장 많이 받아 들여지는 답변은 오래되어서는 안되며 (2009 년 4 월), 아마도 최신의 최신 질문/답변은 whi 르. –

+1

더 최근의 제안과 함께 더 적은 중복 : [안드로이드 차트 그리기 API] (http://stackoverflow.com/questions/3891141/api-for-drawing-chart-in-android) –

답변

7

시도

GraphActivity 클래스

package com.graph; 
import android.app.Activity; 
import android.os.Bundle; 

public class GraphActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    float[] values = new float[] { 2.0f,1.5f, 2.5f, 1.0f , 3.0f }; 
    String[] verlabels = new String[] { "great", "ok", "bad" }; 
    String[] horlabels = new String[] { "today", "tomorrow", "next week", "next month" }; 
    GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels, verlabels, GraphView.LINE); 
    setContentView(graphView); 
} 
} 

GraphView 클래스

package com.graph; 

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; 


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; 

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(); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    float border = 20; 
    float horstart = border * 2; 
    float height = getHeight(); 
    float width = getWidth() - 1; 
    float max = getMax(); 
    float min = getMin(); 
    float diff = max - min; 
    float graphheight = height - (2 * border); 
    float graphwidth = width - (2 * border); 

    paint.setTextAlign(Align.LEFT); 
    int vers = verlabels.length - 1; 
    for (int i = 0; i < verlabels.length; i++) { 
     paint.setColor(Color.DKGRAY); 
     float y = ((graphheight/vers) * i) + border; 
     canvas.drawLine(horstart, y, width, y, paint); 
     paint.setColor(Color.WHITE); 
     canvas.drawText(verlabels[i], 0, y, paint); 
    } 
    int hors = horlabels.length - 1; 
    for (int i = 0; i < horlabels.length; i++) { 
     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); 
     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); 

    if (max != min) { 
     paint.setColor(Color.LTGRAY); 
     if (type == BAR) { 
      float datalength = values.length; 
      float colwidth = (width - (2 * border))/datalength; 
      for (int i = 0; i < values.length; i++) { 
       float val = values[i] - min; 
       float rat = val/diff; 
       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; 
      } 
     } 
    } 
} 

private float getMax() { 
    float largest = Integer.MIN_VALUE; 
    for (int i = 0; i < values.length; i++) 
     if (values[i] > largest) 
      largest = values[i]; 
    return largest; 
} 

private float getMin() { 
    float smallest = Integer.MAX_VALUE; 
    for (int i = 0; i < values.length; i++) 
     if (values[i] < smallest) 
      smallest = values[i]; 
    return smallest; 
} 

}