关于Kotlin:AnyChart Android刷新图表

AnyChart android refreshing a chart

我正在使用适用于android的anychart库来显示饼图,它使用齐射和节点JS API从mysql获取数据,从而简化了操作

当从数据库中获得新数据时,我只是在刷新图表时遇到问题。
我使用Log.d来确保数据实际上正在被更新,我检查了所有图表github并尝试了它们列出的解决方案,主要是我只需要用新数据再次设置数据,那是行不通的
请任何帮助,不胜感激。
我什至尝试创建一个按钮,并尝试使用完整的新数据和硬编码数据设置图形。 但这不起作用,我尝试使用走了可见的东西,这不起作用

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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>

基本上,使用新数据更新图表所需要做的就是使其处于活动状态并应用新数据。
有关详细信息,请检查以下问题线程:
第1期
第2期


直接不可能,您需要为其添加SocketIO。 因此,您需要同时集成socketIO服务器端和客户端。
请检查:-

https://stackoverflow.com/a/25229881/11393364