MPAndroidChart(待续)
MPAndroidChart:Android上强大图表工具
官方文档说明
Barchart(柱状图)
常用方法说明:
barChart.animateXY(800,1000)
barChart.setExtraOffsets(0f,0f,0f,10f)
barChart.minOffset = 0f
barChart.setNoDataText("好像没有数据")
barChart.description.isEnabled = false
barChart.setTouchEnabled(false)
val axisLeft = barChart.axisLeft
axisLeft.isEnabled = true
barChart.axisRight.isEnabled = false
axisLeft.setDrawAxisLine(false)
axisLeft.setDrawLabels(false)
axisLeft.setDrawGridLines(true)
axisLeft.gridColor = Color.parseColor("#e3e3e3")
axisLeft.spaceTop = 30f
val xAxis = barChart.xAxis
xAxis.axisLineColor = Color.parseColor("#e3e3e3")
xAxis.textColor = Color.parseColor("#484848")
xAxis.textSize = 8f
xAxis.valueFormatter = barChartValueFormatter
xAxis.setCenterAxisLabels(true)
xAxis.setDrawGridLines(false)
xAxis.position = XAxis.XAxisPosition.BOTTOM
xAxis.setAvoidFirstLastClipping(false)
xAxis.axisMaximum = 13f
xAxis.axisMinimum = 1f
xAxis.isGranularityEnabled = true
xAxis.granularity = 1f
xAxis.setCenterAxisLabels(true)
xAxis.setLabelCount(13,false)
val legend = barChart.legend
legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP
legend.orientation = Legend.LegendOrientation.HORIZONTAL
legend.textColor = Color.parseColor("#7e7e7e")
legend.textSize = 9f
legend.formToTextSpace = 5f
legend.formSize = 3f
legend.setDrawInside(false)
BarData设置说明:
val groupSpace = 0.64f
val barSpace = 0.03f
val barWidth = 0.2f
var barEntity = BarEntry(1f, 20.3f)
var barEntity1 = BarEntry(2f, 40.3f)
var barEntity2 = BarEntry(3f, 60.3f)
var barEntity3 = BarEntry(4f, 10.3f)
var barEntity31 = BarEntry(12f, 10.3f)
var barEntity4 = BarEntry(5f, 10.3f)
var barEntity5 = BarEntry(4f, 80.3f)
var barEntity6 = BarEntry(3f, 50.3f)
var barEntity61 = BarEntry(12f, 50.3f)
var barEntity7 = BarEntry(4f, 40.3f)
var barEntity8 = BarEntry(3f, 10.3f)
var barEntity9 = BarEntry(2f, 100.3f)
var barEntity91 = BarEntry(12f, 100.3f)
var bardata = BarData()
var barDataSet1 = BarDataSet(listOf(barEntity, barEntity1, barEntity2, barEntity3,barEntity31), "空调")
barDataSet1.color = Color.parseColor("#1d6359")
var barDataSet2 = BarDataSet(listOf(barEntity4, barEntity5, barEntity6,barEntity61), "灯光")
barDataSet2.color = Color.parseColor("#42b39a")
var barDataSet3 = BarDataSet(listOf(barEntity7, barEntity8, barEntity9,barEntity91), "其它")
barDataSet3.color = Color.parseColor("#73c382")
bardata.addDataSet(barDataSet1)
bardata.addDataSet(barDataSet2)
bardata.addDataSet(barDataSet3)
bardata.barWidth = 0.1f
bardata.groupBars(1f, groupSpace, barSpace)
PieChart(饼状图)
常用方法说明:
pieChart.animateY(800, Easing.EaseInOutQuad)
pieChart.setExtraOffsets(0f,0f,0f,0f)
pieChart.minOffset = 0f
pieChart.setNoDataText("好像没有数据")
pieChart.description.isEnabled = false
pieChart.setTouchEnabled(true)
pieChart.isRotationEnabled = true
pieChart.setUsePercentValues(true)
pieChart.setDrawEntryLabels(false)
pieChart.setEntryLabelTextSize(3f)
pieChart.isHighlightPerTapEnabled = false
pieChart.holeRadius = 52f/70f*100
pieChart.transparentCircleRadius = 2f
pieChart.centerText = generateCenterSpannableText()
val legend = pieChart.legend
legend.isEnabled = false
legend.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER
legend.verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
legend.orientation = Legend.LegendOrientation.HORIZONTAL
legend.textColor = Color.parseColor("#7e7e7e")
legend.textSize = 9f
legend.formToTextSpace = 5f
legend.formSize = 3f
legend.setDrawInside(false)
PieChart中心文字多样式例子
private fun generateCenterSpannableText(): SpannableString {
val s = SpannableString("23125.9\n10月份总用电")
s.setSpan(RelativeSizeSpan(1.5f), 0, 7, 0)
s.setSpan(StyleSpan(Typeface.NORMAL), 0, 7, 0)
s.setSpan(ForegroundColorSpan(Color.parseColor("#484848")), 0, 7, 0)
s.setSpan(RelativeSizeSpan(0.5f), 7, s.length, 0)
s.setSpan(StyleSpan(Typeface.NORMAL), 7, s.length,0)
s.setSpan(ForegroundColorSpan(Color.parseColor("#7e7e7e")), 7, s.length,0)
return s
}s
PieData设置说明
var barEntity = PieEntry(1f, "空调")
var barEntity1 = PieEntry(1f, "灯光")
var barEntity2 = PieEntry(1f, "其它")
var barSet = PieDataSet(listOf(barEntity,barEntity1,barEntity2),"用电比重")
val colors = listOf<Int>(Color.parseColor("#1d6359"), Color.parseColor("#42b39a"), Color.parseColor("#73c382"))
barSet.colors = colors
barSet.sliceSpace = 0f
var bardata = PieData(barSet)
bardata.setValueFormatter(PercentFormatter(pieChart,false))
bardata.setValueTextSize(3f)
bardata.setValueTextColor(Color.WHITE)