Search code examples
androidanychart

Android AnyChart - Change Charts trigger by BottomNavigationView


I am trying to display different charts based on the user selection via a BottomNavigationView. The UK looks like this:

enter image description here

The idea is that for each of the buttons clicked on the bottom a different chart will be shown.

The issue I have is that I cant get a different chart to show for each click.

So far I have tried using a single view and invalidating, adding a newly constructed chart and invalidating the view again.

  statsNavigationBar.setOnNavigationItemSelectedListener {
        when(it.itemId){
            R.id.chartView1 -> {
                chartView1.bringToFront()
                chartView1.invalidate()
                chartView1.setChart(createLineChart1())
                chartView1.invalidate()
            }
            R.id.chartView2 -> {
                chartView1.bringToFront()
                chartView1.invalidate()
                chartView1.setChart(createLineChart2())
                chartView1.invalidate()
            }
            R.id.chartView3 -> {
            }
        }
        true
    }

This just shows the same chart no matter which button I click.

I have tried multiple AnyChartView objects:

statsNavigationBar.setOnNavigationItemSelectedListener {
        when(it.itemId){
            R.id.chartView1 -> {
                chartView1.bringToFront()
                chartView1.invalidate()
                chartView1.setChart(createLineChart1())
                chartView1.invalidate()
            }
            R.id.chartView2 -> {
                chartView2.bringToFront()
                chartView2.invalidate()
                chartView2.setChart(createLineChart2())
                chartView2.invalidate()
            }
            R.id.chartView3 -> {
            }
        }
        true
    }

This doesn't show any charts.

I have also tried using the same chart and resetting the data, which works, but I on the last button I want to have a BarChart so, resetting data won't work.

If anyone can suggest a way to show different charts when the user clicks an Item on the BottomNavigationView, I would be most grateful.

Here is my Layout file:

<?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=".StatsActivity">

<com.anychart.AnyChartView
    android:id="@+id/chartView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/statsNavigationBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/colorPrimaryDark"
    />

<com.anychart.AnyChartView
    android:id="@+id/chartView2"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/statsNavigationBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/colorAccent"
    />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/statsNavigationBar"
    android:layout_width="0dp"
    android:layout_height="54dp"
    app:itemBackground="@color/colorAccent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/stats_navigation_items"
    android:background="@color/colorAccent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Please, try to make the view active when the related chart is selected. Like this:

    APIlib.getInstance().setActiveAnyChartView(anyChartView1);
    

    This approach is used when multiple charts are required in a single layout.