我正在使用android的anychart库来显示饼图,它使用volley和node JS api从mysql获取数据,以便于crud操作
当数据库中有新数据可用时,我正面临刷新图表的问题。我使用Log.d来确保数据确实在更新,我检查了任何图表github,并尝试了他们列出的解决方案,主要是我只需要用新数据重新设置数据,这不起作用请提供任何帮助。我甚至尝试创建一个按钮,并尝试使用完整的新数据和硬编码数据来设置图表。但这不起作用,我试着使用gone和visible,这不起作用
下面是我的代码:
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)
}
}下面是我的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>发布于 2020-02-17 12:08:46
发布于 2020-02-14 19:55:34
直接这是不可能的,你需要为它添加套接字SocketIO。因此您需要将socketIO服务器端和客户端都集成在一起。请核对以下资料:
https://stackoverflow.com/questions/60225644
复制相似问题