AnyChart / AnyChart-Android

AnyChart Android Chart is an amazing data visualization library for easily creating interactive charts in Android apps. It runs on API 19+ (Android 4.4) and features dozens of built-in chart types.
2.32k stars 369 forks source link

tree chart is showing on activity but not showing in fragment #257

Open hrgdeveloper opened 2 years ago

hrgdeveloper commented 2 years ago

hello , i am using any chart trial version 1.1.2 this code is working on activity and the chart is showing but when i use exactly same code in a fragment that is inside a viewpager a white screen is showing whit out any chart trial version lable and nothing show up. i have logged data and evry thing is okey . i dont see any log message from any chart in log cat

if i update library to 1.1.3 or 1.1.4 the chart wont show up even on activity . even with your sample code . https://github.com/AnyChart/AnyChart-Android/blob/master/sample/src/main/java/com/anychart/sample/charts/TreeMapChartActivity.java

this is the code that i am using on activity and chart is showing . but on the fragment after getting data from live data i do the same thing and only a white screen is showing

`class TreeChartActivity : BaseActivityNew() {

private val marketStatVM by viewModels<MarketStatViewModel>()
var marketStats = ArrayList<MarketStat>()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    marketStatVM.irtMarketsLiveData.observe(this) {
        marketStats.clear()
        marketStats.addAll(it)
        val treeMap: TreeMap = AnyChart.treeMap()
        val data: MutableList<DataEntry> = ArrayList()
        data.add(CustomTreeDataEntry("Markets", "Markets By Value", "Markets"))
        marketStats.forEach {
            val price =formatDoubleNew(it.latest,Precisions.getAmountPreciousByCurrency(it.srcCurrency),DoubleType.AMOUNT,true)
            data.add(CustomTreeDataEntry(it.srcCurrency,"Markets",it.srcCurrency.uppercase()+"/"+it.dstCurrency.uppercase(),
                it.volumeDst, it.dayChange,getColorByPercent(it.dayChange,
                ),   price
             ))
        }
        treeMap.data(data, TreeFillingMethod.AS_TABLE)
        treeMap.colorScale()
        treeMap.padding(0, 0, 50.0, 0)
        treeMap.maxDepth(2.0)
        treeMap.hovered().fill("#bdbdbd", 0.8)
        treeMap.selectionMode(SelectionMode.NONE)

        treeMap.labels().useHtml(true)
        treeMap.labels().fontColor("#121212")
        treeMap.labels().fontSize(12.0)

        treeMap.labels().format(
            "function() {\n" +
                    "var name = this.getData('product')\n"+
                    "var price = this.getData('extra')\n"+
                    "var value = this.getData('value')\n"+
                    "var all = name + '-'+price + '-' + value+'%'\n"+
                    "      return all;\n" +
                    "    }"
        )

        treeMap.headers().format(
            ("function() {\n" +
                    "    return this.getData('product');\n" +
                     "  }")
        )

        treeMap.tooltip()
            .useHtml(true)
            .titleFormat("function() {\n" +
                    "       return this.getData('extra');\n" +
                    "    }")
            .format(
                ("function() {\n" +
                        "       return this.getData('extra');\n" +
                        "    }")
            )

        binding.anyChart.setChart(treeMap)

    }

}

private fun getColorByPercent(percent:Float) : String{
    when {
        percent < -5 -> {
            return "#BE464E"
        }
        percent < -4 && percent >=-5 ->{
            return "#EE5862"
        }
        percent < -3 && percent >=-4 ->{
            return "#F17981"
        }
        percent < -2 && percent >=-3 ->{
            return "#F49AA0"
        }

        percent < -1 && percent >=-2 ->{
            return "#F8BCC0"
        }

        percent < 0 && percent >=-1 ->{
            return "#FBDDDF"
        }
        percent == 0f -> {
            return "#b3b3be"
        }
        percent > 0 && percent <=1 ->{
            return "#D2F3F1"
        }
        percent > 1 && percent <=2 ->{
            return "#A5E7E3"
        }
        percent > 2 && percent <=3 ->{
            return "#79DCD5"
        }
        percent > 3 && percent <=4 ->{
            return "#4CD0C7"
        }
        percent > 4 && percent <=5 ->{
            return "#20C5BA"
        }
        percent > 5 ->{
            return "#199D94"
        }else-> {
        return "#b3b3be"
    }

    }
}

override fun getViewBinding(): ActivityTreeChartBinding {
    return  ActivityTreeChartBinding.inflate(layoutInflater)
}

override fun getToolbar(): Toolbar? {
    return null
}

 class CustomTreeDataEntry : TreeDataEntryCustom {
    internal constructor(
        id: String?,
        parent: String?,
        product: String?,
        size : Number,
        value: Number?,
        fill:String,
        extra:String
    ) : super(id, parent,size, value) {
        setValue("product", product)
        setValue("fill", fill)
        setValue("extra", extra)
    }

    internal constructor(id: String?, parent: String?, product: String?) : super(id, parent) {
        setValue("product", product)
    }
}

}`

black-pwq commented 2 years ago

I got stuck for this too. Tree chart will show the first time i enter the fragment. But when i navigate to another fragment and back to it, it may possibly disappear (so strange) and only left with the watermark. However, when i back to the home screen (making the activity stopped) and reenter the app it shows again. I am using tree chart to show file size like a file explorer. What makes me quite confused is that it POSSIBLY shows white screen and sometimes works.