I'm currently working on project that uses AnyChart available at https://github.com/AnyChart/AnyChart-Android and I structured my app using jetpack compose. AnyChart uses a xml view:
<com.anychart.AnyChartView
android:id="@+id/any_chart_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
I need some help to integrate the xml view in a composable function. Can someone help me? Thank you in advance
To use AnyChart I should have a xml view that I set like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.anychart.AnyChartView
android:id="@+id/any_chart_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
And to use in a composable function I used AndroidView like this:
@Composable
fun HeatMapChart() {
// Use AndroidView to embed the AnyChartView
AndroidView(
factory ={context ->
val view = LayoutInflater.from(context).inflate(R.layout.heat_map_view, null,false)
val anyview = view.findViewById<AnyChartView>(R.id.any_chart_view)
AnyChartView(context).apply {
}
view
},
update = {view ->
}
)
val riskMap = AnyChart.heatMap()
riskMap.stroke("1 #fff")
riskMap.title().enabled(false)
riskMap.labels().enabled(false)
// Sample data (replace this with your actual data)
val data: MutableList<DataEntry> = ArrayList()
data.add(CustomHeatDataEntry("Rare", "Insignificant", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Rare", "Minor", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Rare", "Moderate", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Rare", "Major", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Rare", "Extreme", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Unlikely", "Insignificant", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Unlikely", "Minor", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Unlikely", "Moderate", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Unlikely", "Major", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Unlikely", "Extreme", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Possible", "Insignificant", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Possible", "Minor", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Possible", "Moderate", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Possible", "Major", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Possible", "Extreme", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Likely", "Insignificant", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Likely", "Minor", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Likely", "Moderate", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Likely", "Major", 2, "#ef6c00"))
data.add(CustomHeatDataEntry("Likely", "Extreme", 2, "#ef6c00"))
data.add(CustomHeatDataEntry("Almost\\nCertain", "Insignificant", 0, "#90caf9"))
data.add(CustomHeatDataEntry("Almost\\nCertain", "Minor", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Almost\\nCertain", "Moderate", 1, "#ffb74d"))
data.add(CustomHeatDataEntry("Almost\\nCertain", "Major", 2, "#ef6c00"))
data.add(CustomHeatDataEntry("Almost\\nCertain", "Extreme", 3, "#d84315"))
// Set the data
riskMap.data(data)
val context = LocalContext.current
AndroidView(
factory ={context ->
val view = LayoutInflater.from(context).inflate(R.layout.heat_map_view, null,false)
val anyview = view.findViewById<AnyChartView>(R.id.any_chart_view)
view
},
update = {view ->
AnyChartView(context).apply {
setChart(riskMap)
}
}
)
}
And I keep getting this error when launching the app : FATAL EXCEPTION: main Process: com.example.anycharttest, PID: 11037 java.lang.NullPointerException: Attempt to invoke virtual method 'com.anychart.AnyChartView$JsListener com.anychart.AnyChartView.getJsListener()' on a null object reference