diff --git a/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt b/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt index 1500877..e092200 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt @@ -1,12 +1,15 @@ package com.casic.app.safetreecontroller.extensions import android.graphics.Color +import android.graphics.Paint +import com.casic.app.safetreecontroller.utils.LocaleConstant 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.LimitLine import com.github.mikephil.charting.components.XAxis -import com.github.mikephil.charting.components.YAxis import com.pengxh.kt.lite.extensions.dp2px +import com.pengxh.kt.lite.utils.SaveKeyValues fun LineChart.initConfig() { this.setNoDataText("无数据,无法绘制浓度趋势...") @@ -22,14 +25,30 @@ //去掉图例 this.legend.isEnabled = 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.axisRight.isEnabled = false + this.axisLeft.axisMinimum = 0f + this.axisLeft.setDrawGridLines(false) //设置x轴上每个点对应的线 + this.axisLeft.setDrawZeroLine(true) + //添加限制线 + val threshold = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1.99f) as Float + val limitLine = LimitLine(threshold, "阈值:${threshold}% VOL") + limitLine.lineWidth = 1f + limitLine.lineColor = Color.RED + //允许在虚线模式下绘制(线段长度,分隔长度,偏移量) + limitLine.enableDashedLine(10f, 10f, 0f) + //设置限制线标签的位置 + limitLine.labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP + limitLine.textSize = 10f + limitLine.textStyle = Paint.Style.FILL + //添加限制线 + this.axisLeft.addLimitLine(limitLine) + //只显示一条阈值线 + if (this.axisLeft.limitLines.size > 1) { + this.axisLeft.limitLines.removeAt(0) + } + limitLine.isEnabled = true //设置x轴 val xAxis: XAxis = this.xAxis @@ -39,4 +58,23 @@ xAxis.setDrawGridLines(false) //设置x轴上每个点对应的线 xAxis.granularity = 1f //禁止放大后x轴标签重绘 xAxis.position = XAxis.XAxisPosition.BOTTOM +} + +fun LineChart.updateLimitLine(threshold: Float) { + val limitLine = LimitLine(threshold, "阈值:${threshold}% VOL") + limitLine.lineWidth = 1f + limitLine.lineColor = Color.RED + //允许在虚线模式下绘制(线段长度,分隔长度,偏移量) + limitLine.enableDashedLine(10f, 10f, 0f) + //设置限制线标签的位置 + limitLine.labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP + limitLine.textSize = 10f + limitLine.textStyle = Paint.Style.FILL + //添加限制线 + this.axisLeft.addLimitLine(limitLine) + //只显示一条阈值线 + if (this.axisLeft.limitLines.size > 1) { + this.axisLeft.limitLines.removeAt(0) + } + limitLine.isEnabled = true } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt b/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt index 1500877..e092200 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/extensions/LineChart.kt @@ -1,12 +1,15 @@ package com.casic.app.safetreecontroller.extensions import android.graphics.Color +import android.graphics.Paint +import com.casic.app.safetreecontroller.utils.LocaleConstant 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.LimitLine import com.github.mikephil.charting.components.XAxis -import com.github.mikephil.charting.components.YAxis import com.pengxh.kt.lite.extensions.dp2px +import com.pengxh.kt.lite.utils.SaveKeyValues fun LineChart.initConfig() { this.setNoDataText("无数据,无法绘制浓度趋势...") @@ -22,14 +25,30 @@ //去掉图例 this.legend.isEnabled = 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.axisRight.isEnabled = false + this.axisLeft.axisMinimum = 0f + this.axisLeft.setDrawGridLines(false) //设置x轴上每个点对应的线 + this.axisLeft.setDrawZeroLine(true) + //添加限制线 + val threshold = SaveKeyValues.getValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, 1.99f) as Float + val limitLine = LimitLine(threshold, "阈值:${threshold}% VOL") + limitLine.lineWidth = 1f + limitLine.lineColor = Color.RED + //允许在虚线模式下绘制(线段长度,分隔长度,偏移量) + limitLine.enableDashedLine(10f, 10f, 0f) + //设置限制线标签的位置 + limitLine.labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP + limitLine.textSize = 10f + limitLine.textStyle = Paint.Style.FILL + //添加限制线 + this.axisLeft.addLimitLine(limitLine) + //只显示一条阈值线 + if (this.axisLeft.limitLines.size > 1) { + this.axisLeft.limitLines.removeAt(0) + } + limitLine.isEnabled = true //设置x轴 val xAxis: XAxis = this.xAxis @@ -39,4 +58,23 @@ xAxis.setDrawGridLines(false) //设置x轴上每个点对应的线 xAxis.granularity = 1f //禁止放大后x轴标签重绘 xAxis.position = XAxis.XAxisPosition.BOTTOM +} + +fun LineChart.updateLimitLine(threshold: Float) { + val limitLine = LimitLine(threshold, "阈值:${threshold}% VOL") + limitLine.lineWidth = 1f + limitLine.lineColor = Color.RED + //允许在虚线模式下绘制(线段长度,分隔长度,偏移量) + limitLine.enableDashedLine(10f, 10f, 0f) + //设置限制线标签的位置 + limitLine.labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP + limitLine.textSize = 10f + limitLine.textStyle = Paint.Style.FILL + //添加限制线 + this.axisLeft.addLimitLine(limitLine) + //只显示一条阈值线 + if (this.axisLeft.limitLines.size > 1) { + this.axisLeft.limitLines.removeAt(0) + } + limitLine.isEnabled = true } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt index e3548dc..51ea481 100644 --- a/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt +++ b/app/src/main/java/com/casic/app/safetreecontroller/fragments/MethaneMonitorFragment.kt @@ -11,6 +11,7 @@ import com.casic.app.safetreecontroller.R import com.casic.app.safetreecontroller.databinding.FragmentMethaneMonitorBinding import com.casic.app.safetreecontroller.extensions.initConfig +import com.casic.app.safetreecontroller.extensions.updateLimitLine import com.casic.app.safetreecontroller.service.SocketConnectionService import com.casic.app.safetreecontroller.utils.LocaleConstant import com.casic.app.safetreecontroller.widgets.LineChartMarkerView @@ -114,6 +115,7 @@ return@setOnClickListener } + binding.lineChart.updateLimitLine(threshold) SaveKeyValues.putValue(LocaleConstant.METHANE_DEFAULT_VALUE_KEY, threshold) val weakReferenceHandler = SocketConnectionService.weakReferenceHandler ?: return@setOnClickListener