Rabu, 15 Juli 2020

Android JSON Grafik

package com.example.spkpenilaiankaryawan;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

//import androidx.appcompat.app.AppCompatActivity;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

//import android.support.v7.app.AppCompatActivity;
public class myGraph extends AppCompatActivity {
    String ip="";

    JSONParser jParser = new JSONParser();
    JSONArray myJSON = null;

    ArrayList<HashMap<String, String>> arrayList;
    private static final String TAG_SUKSES = "sukses";
    private static final String TAG_record = "record";

    private static final String TAG_id_hasil = "id_hasil";
    private static final String TAG_nip = "nip";
    private static final String TAG_nama_penilaian = "nama_penilaian";

    String  nip,nama;
    int sukses;
    int jd=0;

    LineChartView lineChartView;
    String[] axisID;// = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept","Oct", "Nov", "Dec"};    String[] axisKet;
    int[] axisBobot;
    int[] axisRanking;
    //float[] yAxisData;// = {50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mygraph);


        ip = jParser.getIP();


        Intent i = getIntent();
        nip = i.getStringExtra("pk");
        nama= i.getStringExtra("nama");


        TextView txtketerangan = (TextView) findViewById(R.id.txtTitle);
        txtketerangan.setText(nama +" ("+nip+")");

                lineChartView = findViewById(R.id.chart);
        new load().execute();

    }



    void baca(){
        List yAxisValues = new ArrayList();
        List axisValues = new ArrayList();


        Line line = new Line(yAxisValues).setColor(Color.parseColor("#9C27B0"));

        for (int i = 0; i < axisKet.length; i++) {
            axisValues.add(i, new AxisValue(i).setLabel(axisKet[i]));
        }

        for (int i = 0; i < axisBobot.length; i++) {
            yAxisValues.add(new PointValue(i, axisBobot[i]));
        }

        List lines = new ArrayList();
        lines.add(line);

        LineChartData data = new LineChartData();
        data.setLines(lines);

        Axis axis = new Axis();
        axis.setValues(axisValues);
        axis.setTextSize(16);
        axis.setTextColor(Color.parseColor("#03A9F4"));
        data.setAxisXBottom(axis);

        Axis yAxis = new Axis();
        yAxis.setName("Sales in millions");
        yAxis.setTextColor(Color.parseColor("#03A9F4"));
        yAxis.setTextSize(16);
        data.setAxisYLeft(yAxis);

        lineChartView.setLineChartData(data);
        Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
        viewport.top = 110;
        lineChartView.setMaximumViewport(viewport);
        lineChartView.setCurrentViewport(viewport);
    }

    class load extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }
        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("nip", nip));

            JSONObject json = jParser.makeHttpRequest(ip+"hasil/grafik.php", "GET", params);
            Log.d("show: ", json.toString());
            try {
                sukses = json.getInt(TAG_SUKSES);
                if (sukses == 1) {
                    myJSON = json.getJSONArray(TAG_record);
                    jd=myJSON.length();
                        axisID=new String[jd];
                        axisBobot=new int[jd];
                        axisKet=new String[jd];
                        axisRanking=new int[jd];

                    //axisID axisRanking axisBobot axisKet
                    for (int i = 0; i < jd; i++) {
                        JSONObject c = myJSON.getJSONObject(i);
                        String id_hasil= c.getString(TAG_id_hasil);
                        String bobot =  c.getString("bobot");
                        String ranking =c.getString("ranking");
                        String ket=c.getString("deskripsi");

                        axisID[i]=id_hasil;
                        axisBobot[i]=Integer.parseInt(bobot);
                        axisKet[i]=ket;
                        axisRanking[i]=Integer.parseInt(ranking);

                    }
                } else {

                }
            }
            catch (JSONException e) {e.printStackTrace();}
            return null;
        }

        protected void onPostExecute(String file_url) {

            if (sukses == 0) {
                Toast.makeText(getBaseContext(), "Data Hasil belum ada...", Toast.LENGTH_LONG).show();
            }
            runOnUiThread(new Runnable() {
                public void run() {
                 baca();
                }
            });}
    }

}




mygraph.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:app="http://schemas.android.com/apk/res-auto"        xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        tools:context=".MainActivity">

    <TextView        android:id="@+id/txtTitle0"        android:layout_marginTop="7dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="SPK Penilaian Karyawan"        android:textSize="24dp"        android:textColor="#0000ff"        android:textStyle="bold"        android:layout_gravity="center"/>
    <TextView        android:id="@+id/txtTitle"        android:layout_marginTop="7dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="SPK Penilaian Karyawan"        android:textSize="20dp"        android:textColor="#ff0000"        android:textStyle="bold"        android:layout_gravity="center"/>

    <lecho.lib.hellocharts.view.LineChartView        android:id="@+id/chart"        android:layout_width="match_parent"        android:layout_height="match_parent" />

    </LinearLayout>



apply plugin: 'com.android.application'
android {
    compileSdkVersion 28    buildToolsVersion "29.0.3"    defaultConfig {
        applicationId "com.example.spkpenilaiankaryawan"        minSdkVersion 17        targetSdkVersion 28        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }
    buildTypes {
        release {
            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'        }
    }
    useLibrary 'org.apache.http.legacy'}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'    implementation 'com.android.support.constraint:constraint-layout:1.1.3'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.2'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:28.0.0'    implementation 'com.android.support:cardview-v7:28.0.0'    implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'}

Tidak ada komentar:

Posting Komentar