Search code examples
androidanychart

AnyCharts Android hide/reload information


I have been trying to figure this out for a while, however I can't get it to work. This code (below) is inside a function. The first time I call it, it works well. But it seems to do nothing after the first call. After debuging it I know it enters the statements which verify if I want the humidity and/or the temperature, however I dont understend why it doesn't change the chart. I also tried to show all the information and hide it after. No success either. Does anyone know what am I doing wrong? Thanks!

        APIlib.getInstance().setActiveAnyChartView(chartView)
        chartView.setProgressBar(findViewById(R.id.progress_bar))

        val chart = AnyChart.line()
        chart.animation(false)


        chart.crosshair().enabled(true)
        chart.crosshair()
            .yLabel(true)
            .yStroke(null as Stroke?, null, null, null as String?, null as String?)


        chart.tooltip().positionMode(TooltipPositionMode.POINT)


        chart.yAxis(0).title("Nivel de Humidade e Temperatura")
        chart.title("Progresso de Humidade e Temperatura")

        val seriesData: MutableList<DataEntry> = ArrayList()
        //---------------------------------(somehard coded entries)-----------------------------------
        seriesData.add(customDataEntry("1986", 6.9, 0.0))
        seriesData.add(customDataEntry("1987", 7.1, 4.0))
        seriesData.add(customDataEntry("1988", 8.5, 6.2))
        seriesData.add(customDataEntry("1989", 9.2, 11.8))
        seriesData.add(customDataEntry("1990", 10.1, 13.0))
        seriesData.add(customDataEntry("1991", 11.6, 13.9))
        //-------------------------------------------------------------------------------------------------
        val set = Set.instantiate()

        set.data(seriesData)
        val series1Mapping: Mapping = set.mapAs("{ x: 'x', value: 'value1' }")
        val series2Mapping: Mapping = set.mapAs("{ x: 'x', value: 'value2' }")

        if(checkBoxTemperature.isChecked)
        {
            val series: Line = chart.line(series1Mapping)
            series.name("Temperatura")
            series.hovered().markers().enabled(true)
            series.hovered().markers()
                .type(MarkerType.CIRCLE)
                .size(4.0)

            series.tooltip()
                .position("right")
                .anchor(Anchor.LEFT_CENTER)
        }

        if(checkBoxHumidade.isChecked)
        {

            val series: Line = chart.line(series2Mapping)
            series.name("Humidade")
            series.hovered().markers().enabled(true)
            series.hovered().markers()
                .type(MarkerType.CIRCLE)
                .size(4.0)
            series.tooltip()
                .position("right")
                .anchor(Anchor.LEFT_CENTER)

        }

        chart.legend().enabled(true)
        chart.legend().fontSize(13.0)

        chartView.setChart(chart)
    }```

Solution

  • I managed to get it working. For anyone struggling with this, all you have to do is:

    Instead of declaring

    val set = Set.instantiate() inside the function

    Declared it as global

    private var set: Set? = null

    Then assign it on the onCreate() method

    set = Set.instantiate()

    Now, everytime I want to make a change on the chart I use this variable.

    Example:

    chart!!.yAxis(0).title("Humidity & Temperature Levels")
    chart!!.title("Progress of Humidity and Temperature between " + custom_interval_start.get(Calendar.DAY_OF_MONTH) + "/" + custom_interval_start.get(Calendar.MONTH) + "/" + custom_interval_start.get(Calendar.YEAR) +  " e " + custom_interval_end.get(Calendar.DAY_OF_MONTH) + "/" + custom_interval_end.get(Calendar.MONTH) + "/" + custom_interval_end.get(Calendar.YEAR))
    
    if(seriesData.isEmpty()){
       chartView!!.visibility = View.INVISIBLE
    }
    else
    {
        chartView!!.visibility = View.VISIBLE
        set!!.data(seriesData)
    }