Horizo​​ntalBarChart moveViewToX 不起作用 [英] HorizontalBarChart moveViewToX doesn't work

查看:109
本文介绍了Horizo​​ntalBarChart moveViewToX 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结使用 BarChart 时,一切都按预期工作,但 moveViewToX 在 Horizo​​ntalBarChart 上不起作用.我用同样的代码测试过,只改变了XML,错误只影响Horizo​​ntalBarChart

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

我搜索了未解决的问题(最相关的是 #2546,它已关闭,建议的解决方案在我的情况下不起作用)并在 StackOverflow 上搜索,但我还没有找到解决方案.

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.

设备:设备:小米红米 Note 4安卓版本 6.0库版本 3.1.0-alpha

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

这里是相关的代码,是用Kotlin写的.这是xml:

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>

和kt文件:

    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)
        }
    }
}

我已经在github上打开了一个问题:https://github.com/PhilJay/MPAndroidChart/issues/4397

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.

推荐答案

我已经解决了:

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

这篇关于Horizo​​ntalBarChart moveViewToX 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆