HorizontalBarChart moveViewToX 不起作用
HorizontalBarChart moveViewToX doesn't work
总结
使用 BarChart 时,一切都按预期工作,但 moveViewToX 在 HorizontalBarChart 上不起作用。我用相同的代码进行了测试,只更改了 XML,错误只影响 HorizontalBarChart
我已经搜索了未解决的问题(最相关的是#2546,它已关闭并且建议的解决方案在我的案例中不起作用)并在 Whosebug 上进行了搜索,但我还没有找到解决方案。
设备:
设备:小米红米 Note 4
Android 版本 6.0
库版本 3.1.0-alpha
这里是相关的代码,是用Kotlin写的。
这是 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
任何提示 |破解 |可能的解决方案将不胜感激。提前致谢。
我已经解决了它:
moveViewTo(0f,data.entryCount.f,YAxis.AxisDependency.LEFT)
这对我有用:
chart.resetViewPortOffsets()
chart.moveViewToX(chart.highlighted[0].x)
总结 使用 BarChart 时,一切都按预期工作,但 moveViewToX 在 HorizontalBarChart 上不起作用。我用相同的代码进行了测试,只更改了 XML,错误只影响 HorizontalBarChart
我已经搜索了未解决的问题(最相关的是#2546,它已关闭并且建议的解决方案在我的案例中不起作用)并在 Whosebug 上进行了搜索,但我还没有找到解决方案。
设备: 设备:小米红米 Note 4 Android 版本 6.0 库版本 3.1.0-alpha
这里是相关的代码,是用Kotlin写的。 这是 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
任何提示 |破解 |可能的解决方案将不胜感激。提前致谢。
我已经解决了它:
moveViewTo(0f,data.entryCount.f,YAxis.AxisDependency.LEFT)
这对我有用:
chart.resetViewPortOffsets()
chart.moveViewToX(chart.highlighted[0].x)