Android Graph 그리는 걸 테스트 해보고 있다.
http://www.android-graphview.org/ 에서 제공하는 라이브러리를 이용하여 그래프를 그리는 방법이다.
위 사이트에 가면 쉽게 예제코드를 복사해서 사용할 수 있도록 되어 있다.
JSON 으로 받은 데이터를 직접 대입하는 방법으로 테스트한 결과를 확인해봤다.
X축을 값으로 대체하여 넣은 방법이 없는 것인지 아직 내가 파악을 못한 것인지 몰라도 이 부분은 약간 불편하다.
build.gradle
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:support-v4:27.1.1' implementation 'com.android.support:recyclerview-v7:27.1.1' // ListView 개선 버전 implementation 'com.android.support:cardview-v7:27.1.1' implementation 'com.android.support:design:27.1.1' implementation 'gun0912.ted:tedpermission:2.0.0' implementation 'com.android.volley:volley:1.1.0' implementation 'com.github.bumptech.glide:glide:3.8.0' // 이미지 라이브러리 implementation 'com.jjoe64:graphview:4.2.2' // 그래프 라이브러리 } |
XML Layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" > <ImageView android:id="@+id/navibar_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/navibar_bg" android:adjustViewBounds="false" android:scaleType="fitXY" />
<TextView android:id="@+id/navibar_text" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="@android:color/white" android:textSize="22dp" android:text="No Title" android:singleLine="true" />
<Button android:id="@+id/back_btn" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:background="@drawable/btn_back_c" />
<Button android:id="@+id/home_btn" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:background="@drawable/btn_home_c" />
</RelativeLayout>
<com.jjoe64.graphview.GraphView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/graph" />
</LinearLayout>
</LinearLayout> |
Java 코드
graphView Class 생성시 GraphView 로 생성하면 에러가 발생한다.
import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.TextView;
import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.ValueDependentColor; import com.jjoe64.graphview.series.BarGraphSeries; import com.jjoe64.graphview.series.DataPoint;
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
import java.util.ArrayList;
public class graphView extends AppCompatActivity implements View.OnClickListener { Context context;
// ArrayList 선언 ArrayList<Integer> jsonList = new ArrayList<>();
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_graphview); context = this.getBaseContext(); initView(); }
private void initView() { // button action... findViewById(R.id.back_btn).setOnClickListener(this); findViewById(R.id.home_btn).setOnClickListener(this);
// title set TextView title = (TextView) this.findViewById(R.id.navibar_text); // title title.setText(getIntent().getExtras().getString("title"));
DataVolley(); }
private void DataVolley() { // 1. RequestQueue 생성 및 초기화 RequestQueue requestQueue = Volley.newRequestQueue(context);
String url = "http://100.100.100.100/chart/data.json";
// 2. Request Obejct인 StringRequest 생성 StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { //Log.d("result", "[" + response + "]"); showJSONList(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("error", "[" + error.getMessage() + "]"); } }) {
};
// 3) 생성한 StringRequest를 RequestQueue에 추가 requestQueue.add(request); }
private void showJSONList(String JSONdata) {
try { JSONObject jsonObj = new JSONObject(JSONdata); JSONArray jsonArray = jsonObj.getJSONArray("content"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject c = jsonArray.getJSONObject(i); final String Sdate = c.getString("sysdate"); final String Mdata = c.getString("mcount"); //System.out.println(i + " mcount : " + Mdata); jsonList.add(Integer.parseInt(Mdata)); }
} catch (JSONException e) { e.printStackTrace(); }
// http://www.android-graphview.org/bar-chart/ GraphView graph = (GraphView) findViewById(R.id.graph); BarGraphSeries<DataPoint> series = new BarGraphSeries<>(new DataPoint[] { new DataPoint(0, jsonList.get(0)), new DataPoint(1, jsonList.get(1)), new DataPoint(2, jsonList.get(2)), new DataPoint(3, jsonList.get(3)), new DataPoint(4, jsonList.get(4)), new DataPoint(5, jsonList.get(5)), new DataPoint(6, jsonList.get(6)), new DataPoint(7, jsonList.get(7)) }); graph.addSeries(series);
// styling series.setValueDependentColor(new ValueDependentColor<DataPoint>() { @Override public int get(DataPoint data) { return Color.rgb((int) data.getX()*255/4, (int) Math.abs(data.getY()*255/6), 100); } });
series.setSpacing(50);
// draw values on top series.setDrawValuesOnTop(true); series.setValuesOnTopColor(Color.RED); }
@Override public void onClick(View view) { switch (view.getId()) { case R.id.back_btn: finish(); overridePendingTransition(R.anim.rightin, R.anim.rightout); break; case R.id.home_btn: Intent intent = new Intent(graphView.this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); break; default: break; } } }
|