LoginSignup
0
0

【MPAndroidChart】円グラフの外側の余白を無くす

Last updated at Posted at 2023-05-16

はじめに

MPAndroidChartで円グラフ描画時、外側の余白が微妙にできてしまうので、その対策について解説。

余白あり 余白なし

方法

dataSet.selectionShiftを0にすれば良い。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.github.mikephil.charting.charts.PieChart
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/pie_chart"
	android:layout_width="match_parent"
	android:layout_height="match_parent"/>
MainActivity.kt
class MainActivity : AppCompatActivity() {

	/** ViewBinding */
	private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(binding.root)

		val list = listOf(PieEntry(10f), PieEntry(20f), PieEntry(30f))
		val dataSet = PieDataSet(list, null)
		dataSet.colors = listOf(Color.RED, Color.GREEN, Color.BLUE)

		//グラフ上の数値を非表示
		dataSet.setDrawValues(false)

		//外側の余白を無くす
		dataSet.selectionShift = 0f

		binding.pieChart.data = PieData(dataSet)

		//説明を非表示
		binding.pieChart.description.isEnabled = false

		//凡例を非表示
		binding.pieChart.legend.isEnabled = false
	}
}

関連記事

0
0
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
0
0