Newer
Older
Endoscope / app / src / main / java / com / casic / endoscope / extensions / LineChart.kt
package com.casic.endoscope.extensions

import android.content.Context
import android.graphics.Color
import com.github.mikephil.charting.animation.Easing
import com.github.mikephil.charting.charts.Chart
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.pengxh.kt.lite.extensions.dp2px


fun LineChart.init(context: Context) {
    this.setNoDataText("无数据,无法渲染...")
    this.setNoDataTextColor(Color.RED)
    this.getPaint(Chart.PAINT_INFO).textSize = 12f.dp2px(context)
    this.setDrawGridBackground(false)
    this.setDrawBorders(false)
    this.animateY(1200, Easing.EaseInOutQuad)
    //设置样式
    val rightAxis: YAxis = this.axisRight
    //设置图表右边的y轴禁用
    rightAxis.isEnabled = false
    val leftAxis: YAxis = this.axisLeft
    leftAxis.axisMinimum = 0f
    leftAxis.setDrawGridLines(false) //设置x轴上每个点对应的线
    leftAxis.setDrawZeroLine(true)
    this.isScaleXEnabled = true //X轴可缩放
    this.isScaleYEnabled = false //Y轴不可缩放
    //设置x轴
    val xAxis: XAxis = this.xAxis
    xAxis.textSize = 10f
    xAxis.setDrawLabels(true) //绘制标签  指x轴上的对应数值
    xAxis.setDrawAxisLine(true) //是否绘制轴线
    xAxis.setDrawGridLines(false) //设置x轴上每个点对应的线
    xAxis.granularity = 1f //禁止放大后x轴标签重绘
    xAxis.position = XAxis.XAxisPosition.BOTTOM
    this.extraTopOffset = 20f
    this.extraBottomOffset = 10f //解决X轴显示不完全问题
    //去掉描述
    this.description.isEnabled = false
    //去掉图例
    this.legend.isEnabled = false
}