HorizontalBarChart moveViewToX doesn't work

会有一股神秘感。 提交于 2020-08-11 06:18:26

问题


Summary When using BarChart everything work as expected, but moveViewToX doesn't work on HorizontalBarChart. I've tested with the same code, only changing the XML, and the error is only affecting HorizontalBarChart

I've searched opened issues (most relevant was #2546 which is closed and the proposed solution didn't worked in my case) and searched on StackOverflow but I haven't found a solution yet.

Device: Device: Xiaomi Redmi Note 4 Android Version 6.0 Library Version 3.1.0-alpha

Here is the code related, is written in Kotlin. Here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.github.mikephil.charting.charts.HorizontalBarChart
        android:id="@+id/priceAlertsChart"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>
</LinearLayout>

and the kt file:

    override fun onCreate(savedInstanceState: Bundle?) {
        val priceAlerts = mutableListOf<PriceChangeInfo>()
        for(i in 0..20) {
            priceAlerts.add(i,PriceChangeInfo(i.toString()
            ,Random.nextDouble(-2.5,2.5)))
        }
        showPercentChangeDialog(priceAlerts.toTypedArray(), Date().time)
    }

    @SuppressLint("InflateParams")
    private fun showPercentChangeDialog(priceChangeInfo:Array<Parcelable>?, lastUpdate:Long)
    {
        val items = priceChangeInfo?.map {
            it as PriceChangeInfo
        }?.reversed() ?: return

        val v = layoutInflater.inflate(R.layout.price_alerts_dialogos,null)
       // v.lastUpdateTextView.text=getString(R.string.changes_since,timeLapse(lastUpdate))
        v.priceAlertsChart.apply {
            xAxis.apply {
                position = XAxis.XAxisPosition.BOTTOM
                setDrawAxisLine(true)
                setDrawGridLines(false)
                granularity = .3f
                labelCount = if(items.size>10)10 else items.size
                valueFormatter = IAxisValueFormatter { value, _ ->
                    items[value.toInt()].symbol
                }
            }

            withMultiple(axisLeft,axisRight){
                setDrawAxisLine(true)
                setDrawGridLines(true)
            }

            legend.isEnabled = false
            setDrawBarShadow(false)
            //setDrawValueAboveBar(true)
            description.isEnabled = false
            setPinchZoom(false)
            setDrawGridBackground(false)

            data = {
                val green = Color.rgb(110, 190, 102)
                val red = Color.rgb(211, 74, 88)


                val entries = mutableListOf<BarEntry>()
                val colors = mutableListOf<Int>()

                items.forEachIndexed { index, info ->
                    colors.add(if(info.change>0) green else red)
                    entries.add(BarEntry(index.toFloat(),info.change.toFloat(),info.symbol))
                }

                val set1 = BarDataSet(entries,"changes").apply {
                    setDrawIcons(false)
                    setDrawValues(false)
                    setColors(colors)
                    valueFormatter = IValueFormatter {
                        _, entry, _, _ ->
                        formatNumber(entry.y.toDouble(),false,false,decimals = 2)+"%"
                    }
                }

                val dataSets = mutableListOf<IBarDataSet>().apply {
                    add(set1)
                }

                BarData(dataSets).apply {
                    setValueTextSize(10f)
                    barWidth = .4f
                }

            }()

            setVisibleXRangeMaximum(10f)
            //this doesn't work on HorizontalBarChart
            moveViewToX(data.entryCount.toFloat())
        }
        AlertDialog.Builder(this).setTitle(R.string.price_alerts).setView(v)
                .setPositiveButton(R.string.ok) { _: DialogInterface, _: Int ->
                    intent?.extras?.clear()
                }.show()


    }

//dependencies
fun<T> withMultiple(vararg args: T, block: T.() -> Unit) {
  args.forEach {
    it.apply (block)
  }
}

data class PriceChangeInfo(var symbol:String, var  change:Double):Parcelable {
    constructor(parcel: Parcel) : this(
            parcel.readString()!!,
            parcel.readDouble()) {}

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(symbol)
        parcel.writeDouble(change)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<PriceChangeInfo> {
        override fun createFromParcel(parcel: Parcel): PriceChangeInfo {
            return PriceChangeInfo(parcel)
        }

        override fun newArray(size: Int): Array<PriceChangeInfo?> {
            return arrayOfNulls(size)
        }
    }
}

I've already opened an issue on github: https://github.com/PhilJay/MPAndroidChart/issues/4397

Any tip | hack | possible solution will be highly appreciated. Thanks in advance.


回答1:


I've solved it using:

moveViewTo(0f,data.entryCount.f,YAxis.AxisDependency.LEFT)


来源:https://stackoverflow.com/questions/54270146/horizontalbarchart-moveviewtox-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!