Anychart使用函数作为参数创建图形

时间:2020-09-15 19:19:14

标签: android kotlin anychart

我具有用于创建AnyChart图形的此功能。我想为像这样的不同图形提供很多功能,并将它们显示在不同的MaterialDialogs中。

        private fun createChart(): Cartesian {
            val cartesian: Cartesian = AnyChart.cartesian(
            (...)
            return cartesian
        }

为此任务,我做了一个常规功能,将使用特定的视图ID和用于创建图形的特定功能进行调用

private fun showDialog(layoutId : Int , chartId : Int, function: (Chart)) {

        val dialog = MaterialDialog(requireContext())
            .customView(layoutId)
            .cornerRadius(5.5f)

        val chartView: AnyChartView = dialog.findViewById(chartId)
        APIlib.getInstance().setActiveAnyChartView(chartView)
        chartView.setDebug(true)

        chartView.setChart(function) // <------ The problem is here
        //chartView.setChart(createChart() works fine 
        dialog.show()
    }

我将在某些视图的onClick侦听器上调用此函数,并指定要显示的图表和要使用的绘制函数

private fun showWeatherDialog() {
        showDialog(R.layout.activity_graph_test,R.id.any_chart_view,createChart())
}

如果我使用普通的函数调用,则会正常绘制图形,但是当我调用函数参数时,图形将不起作用

   chartView.setChart(function) // <------ The problem is here
   //chartView.setChart(createChart() works fine

Anychart的错误:

E / AnyChart:未捕获的ReferenceError:未定义cartesian43

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

    private fun showWeatherDialog() {
        showDialog(R.layout.activity_graph_test, R.id.any_chart_view) { createChart() }
    }

    private fun showDialog(layoutId: Int, chartId: Int, drawingChart: (()-> Cartesian)) {
         (...)
        chartView.setChart(drawingChart.invoke())
        dialog.show()
    }