Advertisement
Guest User

Grafik

a guest
Oct 24th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. package com.alufstudio.e_reporting.activity;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.graphics.Color;
  6. import android.os.Bundle;
  7. import android.preference.PreferenceManager;
  8. import android.support.annotation.Nullable;
  9. import android.support.v4.app.Fragment;
  10. import android.support.v4.util.ArrayMap;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.Toast;
  16.  
  17. import com.alufstudio.e_reporting.R;
  18. import com.alufstudio.e_reporting.model.Api;
  19. import com.android.volley.DefaultRetryPolicy;
  20. import com.android.volley.Request;
  21. import com.android.volley.RequestQueue;
  22. import com.android.volley.Response;
  23. import com.android.volley.VolleyError;
  24. import com.android.volley.toolbox.JsonObjectRequest;
  25. import com.android.volley.toolbox.Volley;
  26. import com.github.mikephil.charting.charts.HorizontalBarChart;
  27. import com.github.mikephil.charting.data.BarData;
  28. import com.github.mikephil.charting.data.BarDataSet;
  29. import com.github.mikephil.charting.data.BarEntry;
  30.  
  31. import org.json.JSONException;
  32. import org.json.JSONObject;
  33.  
  34. import java.util.ArrayList;
  35. import java.util.Map;
  36.  
  37. /**
  38. * Created by alex on 18/10/16.
  39. */
  40. public class GrafikDuaFragment extends Fragment {
  41. SharedPreferences shared;
  42. SharedPreferences.Editor editor;
  43.  
  44. View view;
  45.  
  46. @Nullable
  47. @Override
  48. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  49. view = inflater.inflate(R.layout.fragment_grafik_dua, container, false);
  50. shared = PreferenceManager.getDefaultSharedPreferences(getContext());
  51. editor = shared.edit();
  52. execGetData();
  53. return view;
  54. }
  55.  
  56. public void execGetData(){
  57. try {
  58. JSONObject parameter = new JSONObject();
  59. parameter.accumulate("api_key", shared.getString("api_key",""));
  60. JSONObject param = new JSONObject(parameter.toString());
  61. Map<String, Object> jsonParams = new ArrayMap<>();
  62. jsonParams.put("jsonrpc", 2);
  63. jsonParams.put("method", "GET");
  64. jsonParams.put("object", "report_proses_paket");
  65. jsonParams.put("filter", 1);
  66. jsonParams.put("param", param);
  67. Log.i("MainActivity","param : "+param);
  68. JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, Api.server, new JSONObject(jsonParams),
  69. new Response.Listener<JSONObject>(){
  70. @Override
  71. public void onResponse(JSONObject response){
  72. Log.i("MainActivity","response : "+response);
  73. try {
  74. JSONObject jsonObject = response.getJSONObject("result");
  75. JSONObject pagu = jsonObject.getJSONObject("HPS");
  76. JSONObject hps = jsonObject.getJSONObject("PENAWARAN");
  77. JSONObject kontrak = jsonObject.getJSONObject("SELISIH");
  78.  
  79. HorizontalBarChart barChart = (HorizontalBarChart) view.findViewById(R.id.chart);
  80. ArrayList<String> labels = new ArrayList<String>();
  81. labels.add("Barang");
  82. labels.add("Jasa/Lainnya");
  83. labels.add("Konsultansi");
  84. labels.add("Konstruksi");
  85.  
  86. //for create Grouped Bar chart
  87. ArrayList<BarEntry> group1 = new ArrayList<>();
  88. group1.add(new BarEntry(pagu.getLong("0"), 0));
  89. group1.add(new BarEntry(pagu.getLong("1"), 1));
  90. group1.add(new BarEntry(pagu.getLong("2"), 2));
  91. group1.add(new BarEntry(pagu.getLong("3"), 3));
  92.  
  93. ArrayList<BarEntry> group2 = new ArrayList<>();
  94. group2.add(new BarEntry(hps.getLong("0"), 0));
  95. group2.add(new BarEntry(hps.getLong("1"), 1));
  96. group2.add(new BarEntry(hps.getLong("2"), 2));
  97. group2.add(new BarEntry(hps.getLong("3"), 3));
  98.  
  99. ArrayList<BarEntry> group3 = new ArrayList<>();
  100. group3.add(new BarEntry(kontrak.getLong("0"), 0));
  101. group3.add(new BarEntry(kontrak.getLong("1"), 1));
  102. group3.add(new BarEntry(kontrak.getLong("2"), 2));
  103. group3.add(new BarEntry(kontrak.getLong("3"), 3));
  104.  
  105. BarDataSet barDataSet1 = new BarDataSet(group1, "HPS");
  106. barDataSet1.setColor(Color.rgb(0, 0, 255));
  107.  
  108. BarDataSet barDataSet2 = new BarDataSet(group2, "PENAWARAN");
  109. barDataSet2.setColor(Color.rgb(0, 255, 0));
  110.  
  111. BarDataSet barDataSet3 = new BarDataSet(group3, "SELISIH");
  112. //barDataSet3.setColors(ColorTemplate.JOYFUL_COLORS);
  113. barDataSet3.setColor(Color.rgb(200, 0, 0));
  114.  
  115. ArrayList<BarDataSet> dataset = new ArrayList<>();
  116. dataset.add(barDataSet1);
  117. dataset.add(barDataSet2);
  118. dataset.add(barDataSet3);
  119.  
  120. BarData data = new BarData(labels, dataset);
  121. barChart.setData(data);
  122. barChart.setDescription(" x 1000.000.000");
  123. barChart.animateY(5000);
  124. } catch (JSONException e) {
  125. e.printStackTrace();
  126. try {
  127. JSONObject jsonObject = response.getJSONObject("error");
  128. if(jsonObject.getString("code").equals("-32600")){
  129. Toast.makeText(getContext(), "Sesi login berakhir. Silahkan login kembali", Toast.LENGTH_LONG).show();
  130. editor.putBoolean("isloggedin", false);
  131. editor.putString("api_key", null);
  132. editor.putString("username", null);
  133. editor.putString("email", null);
  134. editor.putString("id", null);
  135. editor.putString("nama", null);
  136. editor.putString("nip", null);
  137. editor.putString("unit", null);
  138. editor.putString("group", null);
  139. editor.commit();
  140. startActivity(new Intent(getContext(), Login.class));
  141. }
  142. } catch (JSONException e1) {
  143. e1.printStackTrace();
  144. }
  145. }
  146. }
  147. },
  148. new Response.ErrorListener(){
  149. @Override
  150. public void onErrorResponse(VolleyError error){
  151. Toast.makeText(getContext(), "Terjadi kesalahan", Toast.LENGTH_SHORT).show();
  152. Log.i("MainActivity","error : "+error);
  153. }
  154. });
  155. request.setRetryPolicy(new DefaultRetryPolicy(15000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  156. RequestQueue requestQueue = Volley.newRequestQueue(getContext());
  157. requestQueue.add(request);
  158. } catch (JSONException e) {
  159. e.printStackTrace();
  160. }
  161.  
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement