gpt4 book ai didi

android - MPAndroidChart PieChart 标签在值之上

转载 作者:行者123 更新时间:2023-12-04 23:58:56 24 4
gpt4 key购买 nike

MPAndroidChart饼图,是否可以在值顶部显示标签?
enter image description here

最佳答案

您可以定义自定义 CustomPieChartRenderer它是 PieChartRenderer 的子类并覆盖我们感兴趣的以下两种方法:

override fun drawEntryLabel(c: Canvas?, label: String, x: Float, y: Float)
override fun drawValue(c: Canvas?, valueText: String, x: Float, y: Float, color: Int)
drawEntryLabel当条目标签准备好在 Canvas 上指定的 x、y 位置绘制时调用函数。 drawValue当一个值准备好在 Canvas 上的指定 x、y 位置绘制时调用函数。
上述每个父类(super class)函数都使用像 c.drawText(label, x, y, mEntryLabelsPaint) 这样的 Paint 在 Canvas 上绘制文本。或 c.drawText(valueText, x, y, mValuePaint) .当准备好绘制标签值对时,将按顺序调用上述函数。所以在 CustomPieChartRenderer当调用每个函数而不是调用父类(super class)来绘制文本时,我们将每个数据(值/标签)临时保存,并在我们同时拥有标签/值 x、y 位置时绘制它们。要将标签与值对齐,我们只需在每个文本的绘制阶段将 labelY 位置与 valueY 位置交换。
以下是 CustomPieChartRenderer :
class CustomPieChartRenderer(chart: PieChart?, animator: ChartAnimator?, viewPortHandler: ViewPortHandler?) : PieChartRenderer(chart, animator, viewPortHandler) {

private var mHasLabelData = false
private var mHasValueData = false
private var mEntryLabelCanvas: Canvas? = null
private var mValueCanvas: Canvas? = null
private var mEntryLabel: String = ""
private var mValueText: String = ""
private var mEntryLabelX = 0f
private var mValueX = 0f
private var mEntryLabelY = 0f
private var mValueY = 0f
private var mValueColor = 0

override fun drawEntryLabel(c: Canvas?, label: String, x: Float, y: Float) {
//instead of calling super save the label data temporary
//super.drawEntryLabel(c, label, x, y)
mHasLabelData = true
//save all entry label information temporary
mEntryLabelCanvas = c
mEntryLabel = label
mEntryLabelX = x
mEntryLabelY = y
//and check if we have both label and value data temporary to draw them
checkToDrawLabelValue()
}

override fun drawValue(c: Canvas?, valueText: String, x: Float, y: Float, color: Int) {
//instead of calling super save the value data temporary
//super.drawValue(c, valueText, x, y, color)
mHasValueData = true
//save all value information temporary
mValueCanvas = c
mValueText = valueText
mValueX = x
mValueY = y
mValueColor = color
//and check if we have both label and value data temporary to draw them
checkToDrawLabelValue()
}

private fun checkToDrawLabelValue() {
if (mHasLabelData && mHasValueData) {
drawLabelAndValue()
mHasLabelData = false
mHasValueData = false
}
}

private fun drawLabelAndValue() {
//to show label on top of the value just swap the mEntryLabelY with mValueY
drawEntryLabelData(mEntryLabelCanvas, mEntryLabel, mEntryLabelX, mValueY)
drawValueData(mValueCanvas, mValueText, mValueX, mEntryLabelY, mValueColor)
}

//This is the same code used in super.drawEntryLabel(c, label, x, y) with any other customization you want in mEntryLabelsPaint
private fun drawEntryLabelData(c: Canvas?, label: String, x: Float, y: Float) {
val mEntryLabelsPaint: Paint = paintEntryLabels
mEntryLabelsPaint.setColor(Color.BLACK)
mEntryLabelsPaint.setTypeface(Typeface.DEFAULT_BOLD)
mEntryLabelsPaint.setTextAlign(Paint.Align.CENTER)
c?.drawText(label, x, y, mEntryLabelsPaint)
}

//This is the same code used in super.drawValue(c, valueText, x, y, color) with any other customization you want in mValuePaint
fun drawValueData(c: Canvas?, valueText: String, x: Float, y: Float, color: Int) {
mValuePaint.color = color
mValuePaint.textAlign = Paint.Align.CENTER
c?.drawText(valueText, x, y, mValuePaint)
}
}
这是一个示例用法:
class PieChartActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.piechart_layout)

val pieChart = findViewById<PieChart>(R.id.pieChart)

//initialize a List of colors one for each slice
val pieColors: ArrayList<Int> = ArrayList()
pieColors.add(Color.parseColor("#0f2c4b"))
pieColors.add(Color.parseColor("#1857a0"))
pieColors.add(Color.parseColor("#238837"))
pieColors.add(Color.parseColor("#3f9cff"))

//initialize a List of PieEntry with its value/label pair
val pieEntries: ArrayList<PieEntry> = ArrayList()
pieEntries.add(PieEntry(40f, "NA"))
pieEntries.add(PieEntry(18f, "MENA"))
pieEntries.add(PieEntry(20f, "EU"))
pieEntries.add(PieEntry(22f, "ASIA"))

//prepare the PieDataSet with the above pieEntries and pieColors
val pieDataSet = PieDataSet(pieEntries, "")
pieDataSet.valueTextSize = 14f
pieDataSet.colors = pieColors

//draw value/label outside the pie chart
pieDataSet.xValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
pieDataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
pieDataSet.valueLinePart1OffsetPercentage = 100f
pieDataSet.valueLinePart1Length = 0.8f
pieDataSet.valueLinePart2Length = 0f
pieDataSet.valueTextColor = Color.BLACK
pieDataSet.valueTypeface = Typeface.DEFAULT_BOLD
pieDataSet.valueLineColor = ColorTemplate.COLOR_NONE

//prepare the PieData
val pieData = PieData(pieDataSet)
pieData.setValueTextColor(Color.BLACK)
pieData.setDrawValues(true)
pieData.setValueFormatter(PercentFormatter(pieChart))

//set pieChart data and any other pieChart property needed
pieChart.data = pieData
pieChart.setExtraOffsets(35f, 35f, 35f, 35f)
pieChart.setEntryLabelColor(Color.BLACK)
pieChart.setEntryLabelTextSize(14f)
pieChart.setEntryLabelTypeface(Typeface.DEFAULT_BOLD)
pieChart.setUsePercentValues(true)
pieChart.legend.isEnabled = false
pieChart.description.isEnabled = false
pieChart.isRotationEnabled = true
pieChart.dragDecelerationFrictionCoef = 0.9f
pieChart.rotationAngle = 220f
pieChart.isHighlightPerTapEnabled = true
pieChart.animateY(1400, Easing.EaseInOutQuad)
pieChart.setHoleColor(Color.WHITE)

//set the custom renderer (CustomPieChartRenderer) used to draw each label on top of the value and call invalidate to redraw the chart
pieChart.renderer = CustomPieChartRenderer(pieChart, pieChart.animator, pieChart.viewPortHandler)
pieChart.invalidate()
}
}
饼图 Xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>
结果:
piechart
注意:这是用 'com.github.PhilJay:MPAndroidChart:v3.1.0' 测试的。

关于android - MPAndroidChart PieChart 标签在值之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69540724/

24 4 0