How to Plot Graph using MY SQL Database. I am not able to pull data using array


#1

Hello,

I am not able to plot a graph using kotlin and back end is my sql database.

Code:

import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.support.v7.app.AppCompatActivity
import android.widget.ArrayAdapter
import android.widget.Toast
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.Volley

class MainActivity() : AppCompatActivity(), Parcelable {

constructor(parcel: Parcel) : this() {
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    setBarChart()
}

private fun setBarChart() {
    val url="http://192.168.1.7/count/get_user.php"
    val entries = ArrayList<BarEntry>()
    var rq:RequestQueue=Volley.newRequestQueue(this)
    var jar=JsonArrayRequest(Request.Method.GET,url,null,Response.Listener { response ->

        for(x in 0..response.length()-1)
            entries.add(response.getJSONObject(x).getString("usercount"))


        var adp= ArrayAdapter(this,R.layout.my_textview,entries)
       home_rate.adapter=adp

    }, Response.ErrorListener { error ->
        Toast.makeText(this,error.message, Toast.LENGTH_LONG).show()
    })
    rq.add(jar)



    val barDataSet = BarDataSet(entries, "Cells")




    val data = BarData(labels, barDataSet)
    barChart.data = data // set the data and list of lables into chart

    barChart.setDescription("User Count Weekly")  // set the description

    //barDataSet.setColors(ColorTemplate.COLORFUL_COLORS)
    barDataSet.color = resources.getColor(R.color.colorAccent)

    barChart.animateY(5000)
}


MY SQL Table:

user

date usercount
09/01/18  22
09/02/18  34
09/03/18 21