如何解决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
解决方法
这是我的解决方案:
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()
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。