2
1

More than 3 years have passed since last update.

MPAndroidChart y軸に単位を表示する方法

Posted at

MPAndroidChartでy軸に単位を表示する方法を紹介したいと思います。
あと自分が忘れないために記録用として。

1.まずは、ValueFormatterクラスを継承したクラス作成し、getFormattedValueメソッドをオーバーライドする。

class MyYAxisValueFormatter : ValueFormatter() {
    override fun getFormattedValue(value: Float): String {
        //valueはy軸の目盛りの値。最後にString型にして、自分の付けたい単位足す。
        return value.toInt().toString() + "冊"
    }
}

2.chartのaxisLeft.Formatterに先ほど作ったクラスのインスタンスを代入する。

 chart.axisLeft.valueFormatter = MyYAxisValueFormatter()

実行すると....
Screenshot_1604367947.png

y軸に単位が表示されました!!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1