Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.alufstudio.e_reporting.activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.preference.PreferenceManager;
- import android.support.annotation.Nullable;
- import android.support.v4.app.Fragment;
- import android.support.v4.util.ArrayMap;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Toast;
- import com.alufstudio.e_reporting.R;
- import com.alufstudio.e_reporting.model.Api;
- import com.android.volley.DefaultRetryPolicy;
- 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.JsonObjectRequest;
- import com.android.volley.toolbox.Volley;
- import com.github.mikephil.charting.charts.HorizontalBarChart;
- import com.github.mikephil.charting.data.BarData;
- import com.github.mikephil.charting.data.BarDataSet;
- import com.github.mikephil.charting.data.BarEntry;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.Map;
- /**
- * Created by alex on 18/10/16.
- */
- public class GrafikDuaFragment extends Fragment {
- SharedPreferences shared;
- SharedPreferences.Editor editor;
- View view;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- view = inflater.inflate(R.layout.fragment_grafik_dua, container, false);
- shared = PreferenceManager.getDefaultSharedPreferences(getContext());
- editor = shared.edit();
- execGetData();
- return view;
- }
- public void execGetData(){
- try {
- JSONObject parameter = new JSONObject();
- parameter.accumulate("api_key", shared.getString("api_key",""));
- JSONObject param = new JSONObject(parameter.toString());
- Map<String, Object> jsonParams = new ArrayMap<>();
- jsonParams.put("jsonrpc", 2);
- jsonParams.put("method", "GET");
- jsonParams.put("object", "report_proses_paket");
- jsonParams.put("filter", 1);
- jsonParams.put("param", param);
- Log.i("MainActivity","param : "+param);
- JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, Api.server, new JSONObject(jsonParams),
- new Response.Listener<JSONObject>(){
- @Override
- public void onResponse(JSONObject response){
- Log.i("MainActivity","response : "+response);
- try {
- JSONObject jsonObject = response.getJSONObject("result");
- JSONObject pagu = jsonObject.getJSONObject("HPS");
- JSONObject hps = jsonObject.getJSONObject("PENAWARAN");
- JSONObject kontrak = jsonObject.getJSONObject("SELISIH");
- HorizontalBarChart barChart = (HorizontalBarChart) view.findViewById(R.id.chart);
- ArrayList<String> labels = new ArrayList<String>();
- labels.add("Barang");
- labels.add("Jasa/Lainnya");
- labels.add("Konsultansi");
- labels.add("Konstruksi");
- //for create Grouped Bar chart
- ArrayList<BarEntry> group1 = new ArrayList<>();
- group1.add(new BarEntry(pagu.getLong("0"), 0));
- group1.add(new BarEntry(pagu.getLong("1"), 1));
- group1.add(new BarEntry(pagu.getLong("2"), 2));
- group1.add(new BarEntry(pagu.getLong("3"), 3));
- ArrayList<BarEntry> group2 = new ArrayList<>();
- group2.add(new BarEntry(hps.getLong("0"), 0));
- group2.add(new BarEntry(hps.getLong("1"), 1));
- group2.add(new BarEntry(hps.getLong("2"), 2));
- group2.add(new BarEntry(hps.getLong("3"), 3));
- ArrayList<BarEntry> group3 = new ArrayList<>();
- group3.add(new BarEntry(kontrak.getLong("0"), 0));
- group3.add(new BarEntry(kontrak.getLong("1"), 1));
- group3.add(new BarEntry(kontrak.getLong("2"), 2));
- group3.add(new BarEntry(kontrak.getLong("3"), 3));
- BarDataSet barDataSet1 = new BarDataSet(group1, "HPS");
- barDataSet1.setColor(Color.rgb(0, 0, 255));
- BarDataSet barDataSet2 = new BarDataSet(group2, "PENAWARAN");
- barDataSet2.setColor(Color.rgb(0, 255, 0));
- BarDataSet barDataSet3 = new BarDataSet(group3, "SELISIH");
- //barDataSet3.setColors(ColorTemplate.JOYFUL_COLORS);
- barDataSet3.setColor(Color.rgb(200, 0, 0));
- ArrayList<BarDataSet> dataset = new ArrayList<>();
- dataset.add(barDataSet1);
- dataset.add(barDataSet2);
- dataset.add(barDataSet3);
- BarData data = new BarData(labels, dataset);
- barChart.setData(data);
- barChart.setDescription(" x 1000.000.000");
- barChart.animateY(5000);
- } catch (JSONException e) {
- e.printStackTrace();
- try {
- JSONObject jsonObject = response.getJSONObject("error");
- if(jsonObject.getString("code").equals("-32600")){
- Toast.makeText(getContext(), "Sesi login berakhir. Silahkan login kembali", Toast.LENGTH_LONG).show();
- editor.putBoolean("isloggedin", false);
- editor.putString("api_key", null);
- editor.putString("username", null);
- editor.putString("email", null);
- editor.putString("id", null);
- editor.putString("nama", null);
- editor.putString("nip", null);
- editor.putString("unit", null);
- editor.putString("group", null);
- editor.commit();
- startActivity(new Intent(getContext(), Login.class));
- }
- } catch (JSONException e1) {
- e1.printStackTrace();
- }
- }
- }
- },
- new Response.ErrorListener(){
- @Override
- public void onErrorResponse(VolleyError error){
- Toast.makeText(getContext(), "Terjadi kesalahan", Toast.LENGTH_SHORT).show();
- Log.i("MainActivity","error : "+error);
- }
- });
- request.setRetryPolicy(new DefaultRetryPolicy(15000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
- RequestQueue requestQueue = Volley.newRequestQueue(getContext());
- requestQueue.add(request);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement