package com.tistory.android.mpchart;
import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; import com.github.mikephil.charting.utils.ColorTemplate;
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<Integer> jsonList = new ArrayList<>(); // ArrayList 선언 ArrayList<String> labelList = new ArrayList<>(); // 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")); title.setText("그래프");
DataVolley(); }
private void DataVolley() { // 1. RequestQueue 생성 및 초기화 RequestQueue requestQueue = Volley.newRequestQueue(context);
String url = "http://100.10.10.10/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) { labelList.clear(); jsonList.clear();
try { // 파싱처리 부분은 json 파일 포멧에 맞게 설정해야 한다. 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); labelList.add(DateChange(Sdate)); jsonList.add(Integer.parseInt(Mdata)); }
} catch (JSONException e) { e.printStackTrace(); }
LineChartGraph(labelList, jsonList); }
private void LineChartGraph(ArrayList labelList, ArrayList valList){ // LineChart 메소드 LineChart lineChart = (LineChart) findViewById(R.id.chart);
ArrayList<Entry> entries = new ArrayList<>(); for(int i=0; i < valList.size();i++){ entries.add(new Entry((Integer) valList.get(i), i)); }
LineDataSet depenses = new LineDataSet(entries, "# of Calls"); depenses.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<String> labels = new ArrayList<String>(); for(int i=0; i < labelList.size(); i++){ labels.add((String) labelList.get(i)); }
ArrayList<ILineDataSet> dataSets = new ArrayList<>(); dataSets.add((ILineDataSet)depenses); LineData data = new LineData(labels,dataSets); // 라이브러리 v3.x 사용하면 에러 발생함 depenses.setColors(ColorTemplate.COLORFUL_COLORS); // //depenses.setDrawCubic(true); //선 둥글게 만들기 //depenses.setDrawFilled(true); //그래프 밑부분 색칠
lineChart.setData(data); lineChart.animateXY(1000,1000); lineChart.invalidate(); }
private String DateChange(String sysdate){ String year = sysdate.split("-")[0]; String month = sysdate.split("-")[1]; String day = sysdate.split("-")[2];
StringBuilder finalString = new StringBuilder(); finalString.append(year.substring(2,4)); finalString.append("."); finalString.append(month); //finalString.append("."); //finalString.append(day); return finalString.toString(); }
@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; } }
} |