示例图
1.添加依赖
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
2.布局文件
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/lineChart"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:layout_height="280dp"/>
3.新建drawable ,命名fade_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#26cc86ff"
android:endColor="#40cc86ff" />
</shape>
4.使用
private fun initLineChart() {
lineChart.description.isEnabled = false //lineChart是布局里面的id
// 可以分别在x,y轴缩放
lineChart.isScaleXEnabled = false
lineChart.isScaleYEnabled = false
lineChart.setPinchZoom(false) //是否双指缩放
val xAxis = lineChart.xAxis
xAxis.enableGridDashedLine(10f, 10f, 0f)
xAxis.position = XAxis.XAxisPosition.BOTTOM
xAxis.setDrawGridLines(false) //x轴不要表格
val yAxis = lineChart.axisLeft
yAxis.axisMaximum = 10f //y轴最大值 ,这里关系到y轴直接显示的数据
yAxis.axisMinimum = 0f //y轴最小值
yAxis.setDrawGridLines(true) //y轴不要表格
yAxis.enableGridDashedLine(10f, 10f, 0f)
yAxis.setDrawZeroLine(false)
lineChart.axisRight.isEnabled = false
lineChart.animateX(2500)
val l = lineChart.legend //设置l.isEnabled = false 可以去掉底部的说明文字
l.form = Legend.LegendForm.LINE
val xdata = ArrayList<String>()
xdata.add(0, "周一")
xdata.add(1, "周二")
xdata.add(2, "周三")
xdata.add(3, "周四")
xdata.add(4, "周五")
val ydata = ArrayList<String>()
ydata.add(0, "很差")
ydata.add(1, "1")
ydata.add(2, "不佳")
ydata.add(3, "3")
ydata.add(4, "平平")
ydata.add(5, "5")
ydata.add(6, "还行")
ydata.add(7, "7")
ydata.add(8, "不错")
ydata.add(9, "9")
ydata.add(10, "极佳")
xAxis.valueFormatter = object : IAxisValueFormatter {
val decimalDigits: Int
get() = 0
override fun getFormattedValue(value: Float, axis: AxisBase): String {
return String.format(xdata.get(value.toInt()))
}
}
yAxis.valueFormatter = object : IAxisValueFormatter {
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
return String.format(ydata.get(value.toInt()))
}
}
setData(7, 100f)
}
private fun setData(count: Int, range: Float) {
val values = ArrayList<Entry>()
/**
* 设置折线数据
*/
values.add(Entry(0f, 0f))
values.add(Entry(1f, 2f))
values.add(Entry(2f, 0f))
values.add(Entry(3f, 3f))
values.add(Entry(4f, 5f))
val set1: LineDataSet //lineChart是布局里面的id
if (lineChart.data != null && lineChart.data.dataSetCount > 0) {
set1 = lineChart.data.getDataSetByIndex(0) as LineDataSet
set1.values = values
lineChart.data.notifyDataChanged()
lineChart.notifyDataSetChanged()
} else {
set1 = LineDataSet(values, "")
set1.setDrawCircles(false)
set1.mode = LineDataSet.Mode.HORIZONTAL_BEZIER//设置为曲线而不是折线
set1.color = resources.getColor(R.color.linechartColor)
set1.lineWidth = 3f
set1.setDrawFilled(true) //曲线下面填充颜色
if (android.os.Build.VERSION.SDK_INT >= 18) {
val drawable = ContextCompat.getDrawable(mActivity, R.drawable.fade_red)
set1.fillDrawable = drawable
} else {
set1.fillColor = Color.BLACK;
}
val dataSets = ArrayList<ILineDataSet>()
dataSets.add(set1)
val data = LineData(dataSets)
data.setDrawValues(false)
lineChart.data = data
}
}