I am using anychart library for android to display a pie chart, it gets the data from a mysql using volley and a node JS api to facilitate the crud operation
I am just facing a problem refresing the chart when new data is available from the data base. I used Log.d to make sure the data is actually being updated it, I checked anycharts github and tried the solutions they listed, mainly that I just need to set the data again with the new data, that didn't work Please any help is greatly appreciated. I even tried creating a button and try to set the graph with complety new and hardcoded data. but that didn't work, I tried to use gone and visible and that didn't work
here is my code:
package com.dana.baizaty_frontend.fragments
import Transaction
import android.os.Bundle
import android.provider.ContactsContract
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.anychart.APIlib
import com.anychart.AnyChart
import com.anychart.AnyChartView
import com.anychart.chart.common.dataentry.DataEntry
import com.anychart.chart.common.dataentry.ValueDataEntry
import com.anychart.enums.Align
import com.anychart.enums.LegendLayout
import com.dana.baizaty_frontend.R
import com.dana.baizaty_frontend.singleton.AppSingleton
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.fragment_pfm.*
class PFM : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// val anyChartView: AnyChartView? = any_chart_view
// Inflate the layout for this fragment
// createChart()
var v = inflater.inflate(R.layout.fragment_pfm, container, false)
return v
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// getTransactions()
button.setOnClickListener{
updateChart()
}
}
override fun onStart() {
super.onStart()
getTransactions()
Toast.makeText(context, "testing", "testing".length).show()
}
fun createChart (data1: MutableList<DataEntry>
) {
// var anyChartView: AnyChartView? = any_chart_view
// APIlib.getInstance().setActiveAnyChartView(any_chart_view)
// any_chart_view.clear()
any_chart_view.visibility=View.GONE
any_chart_view.setProgressBar(progress_bar)
var pie = AnyChart.pie3d()
// anyChartView?.setProgressBar(progress_bar)
// data.add(ValueDataEntry("Restarant", 30))
// data.add(ValueDataEntry("transportation", 23))
// data.add(ValueDataEntry("cafe", 10))
Log.d("catagor3",data1.toString())
pie.data(data1)
pie.title("spending")
pie.labels().position("outside")
pie.legend().title().enabled(true)
pie.legend()
.position("center-bottom")
.itemsLayout(LegendLayout.HORIZONTAL)
.align(Align.CENTER);
any_chart_view?.setChart(pie);
any_chart_view.visibility=View.VISIBLE
data1.clear()
}
fun updateChart(){
var data1 = mutableListOf<DataEntry>()
data1.add(ValueDataEntry("Restarant", 30))
data1.add(ValueDataEntry("transportation", 23))
data1.add(ValueDataEntry("cafe", 10))
var pie=AnyChart.pie3d()
any_chart_view.setChart(pie)
}
fun getTransactions() {
var data = mutableListOf<DataEntry>()
any_chart_view.invalidate()
var queue = AppSingleton.instance.getQueue(context)
var transactionsRequest = StringRequest(
Request.Method.GET,
AppSingleton.instance.getBaseUrl() + "transactions/3",
Response.Listener<String> { response ->
// Success case
var transactionList = Gson().fromJson<List<Transaction>>(response, object :
TypeToken<List<Transaction>>() {}.type)
var catagories = mutableListOf<String>()
var values = mutableListOf<Double>()
transactionList.forEach {
// Log.d("catgor",it.toString())
if (catagories.contains(it.catagorie)) {
var idx = catagories.indexOf(it.catagorie)
// Log.d("catgor1",idx.toString())
// Log.d("catgor2",catagories.toString())
values[idx] += it.amount
}else{
catagories.add(it.catagorie)
values.add(it.amount)
var idx=catagories.indexOf(it.catagorie)
// values[idx]=0.0
}
}
Log.d("catagor",catagories.toString())
Log.d("catagor",values.toString())
catagories.forEach{
var idx=catagories.indexOf(it)
data.add(ValueDataEntry(it,values[idx]))
}
Log.d("catagor3",data.toString())
createChart(data)
data.clear()
Log.d("catagor3",data.toString())
},
Response.ErrorListener {
// Error case
})
queue?.add(transactionsRequest)
}
}
and here is my xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".fragments.PFM">
<com.anychart.AnyChartView
android:id="@+id/any_chart_view"
android:layout_width="389dp"
android:layout_height="333dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginTop="78dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/any_chart_view" />
</androidx.constraintlayout.widget.ConstraintLayout>