Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么我这个布局和代码不能显示图表 #190

Open
jixiuy opened this issue Oct 23, 2023 · 0 comments
Open

为什么我这个布局和代码不能显示图表 #190

jixiuy opened this issue Oct 23, 2023 · 0 comments

Comments

@jixiuy
Copy link

jixiuy commented Oct 23, 2023

<data>

</data>

<com.scwang.smart.refresh.layout.SmartRefreshLayout
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.scwang.smart.refresh.header.ClassicsHeader
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="ScrollViewCount">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >

            <com.github.aachartmodel.aainfographics.aachartcreator.AAChartView
                android:id="@+id/aa_chart_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </com.github.aachartmodel.aainfographics.aachartcreator.AAChartView>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:overScrollMode="never"
                android:background="#fff" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

    <com.scwang.smart.refresh.footer.ClassicsFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</com.scwang.smart.refresh.layout.SmartRefreshLayout>

class SortFragment : Fragment() {
private lateinit var sortViewModel: SortViewModel
private var binding: FragmentSortBinding? = null

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    binding = FragmentSortBinding.inflate(inflater, container, false)
    return binding!!.root
}

override fun onDestroyView() {
    super.onDestroyView()
    binding = null
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    observeUsers()

    setupRefreshLayout()

}

private fun setupRefreshLayout() {

    binding?.refreshLayout?.setRefreshHeader(ClassicsHeader(context))
    binding?.refreshLayout?.setRefreshFooter(ClassicsFooter(context))
    binding?.refreshLayout?.setOnRefreshListener { refreshlayout ->
        observeUsers()
        refreshlayout.finishRefresh(0)

    }
    binding?.refreshLayout?.setOnLoadMoreListener { refreshlayout ->
        refreshlayout.finishLoadMore(0)

    }
}

private fun drawChart() {
    val aaChartModel = AAChartModel()
        .chartType(Bar)
        .title("title")
        .subtitle("subtitle")
        .backgroundColor("#4b2b7f")
        .series(
            arrayOf(
                AASeriesElement()
                    .name("Tokyo")
                    .data(
                        arrayOf(
                            7.0,
                            6.9,
                            9.5,
                            14.5,
                            18.2,
                            21.5,
                            25.2,
                            26.5,
                            23.3,
                            18.3,
                            13.9,
                            9.6
                        )
                    ),
                AASeriesElement()
                    .name("NewYork")
                    .data(
                        arrayOf(
                            0.2,
                            0.8,
                            5.7,
                            11.3,
                            17.0,
                            22.0,
                            24.8,
                            24.1,
                            20.1,
                            14.1,
                            8.6,
                            2.5
                        )
                    ),
                AASeriesElement()
                    .name("London")
                    .data(
                        arrayOf(
                            0.9,
                            0.6,
                            3.5,
                            8.4,
                            13.5,
                            17.0,
                            18.6,
                            17.9,
                            14.3,
                            9.0,
                            3.9,
                            1.0
                        )
                    ),
                AASeriesElement()
                    .name("Berlin")
                    .data(
                        arrayOf(
                            3.9,
                            4.2,
                            5.7,
                            8.5,
                            11.9,
                            15.2,
                            17.0,
                            16.6,
                            14.2,
                            10.3,
                            6.6,
                            4.8
                        )
                    )
            )
        )
    binding?.aaChartView?.aa_drawChartWithChartModel(aaChartModel)

}

private fun observeUsers() {
    // 创建 sortViewModel 对象
    sortViewModel = ViewModelProvider(this)[SortViewModel::class.java]
    // 观察 getUsers 方法返回的 LiveData 对象
    sortViewModel.getUsers().observe(viewLifecycleOwner) { resource ->
        when (resource.status) {
            Resource.Status.LOADING -> {}

            Resource.Status.SUCCESS -> {

                val data = resource.data?.body().toString()
                // 从字符串中提取JSON数据
                val regex = "\\[(.*?)]".toRegex()
                val matchResult = regex.find(data)
                val jsonData = matchResult?.value
                val gson = Gson()
                val userData = gson.fromJson(jsonData, User::class.java)

                Log.d("firstTAG", "onViewCreated: $userData")

                drawChart()

            }

            Resource.Status.ERROR -> {
                "请连接校园网".showToast()
                // 处理错误状态
            }
        }
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant