1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| import android.graphics.Color import android.view.ViewGroup import android.view.ViewGroup.LayoutParams.MATCH_PARENT import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.viewinterop.AndroidView import androidx.core.graphics.toColorInt import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.PieData import com.github.mikephil.charting.data.PieDataSet import com.github.mikephil.charting.data.PieEntry
@Composable fun PieChartView() { AndroidView( modifier = Modifier .fillMaxSize(), factory = { ctx -> PieChart(ctx).apply { layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) setBackgroundColor(Color.TRANSPARENT)
description.isEnabled = false
setTouchEnabled(false)
legend.textColor = "#1D1D1D".toColorInt() legend.textSize = 16f legend.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER
data = generatePieData()
} } ) }
private fun generatePieData(): PieData { val entries = mutableListOf<PieEntry>()
entries.add(PieEntry(40f, "优秀")) entries.add(PieEntry(20f, "满分")) entries.add(PieEntry(30f, "及格")) entries.add(PieEntry(10f, "不及格"))
val dataSet = PieDataSet(entries, "") dataSet.apply { colors = listOf( "#0085F7".toColorInt(), "#FE9429".toColorInt(), "#37C427".toColorInt(), "#FCC138".toColorInt(), "#F24949".toColorInt(), "#ED4A99".toColorInt(), "#6B30FF".toColorInt(), "#798CB5".toColorInt(), "#5DDFF0".toColorInt(), "#2F65FA".toColorInt(), "#FF6D1F".toColorInt(), ) valueTextColor = "#1D1D1D".toColorInt() valueTextSize = 20f setDrawValues(true) yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE valueLineColor = Color.BLACK; valueLinePart1OffsetPercentage = 100f; valueLinePart1Length = 0.5f; valueLinePart2Length = 0.5f; } return PieData(dataSet) }
|