|
|
|
@ -0,0 +1,161 @@
|
|
|
|
|
package com.yinuo.safetywatcher.watcher.ui
|
|
|
|
|
|
|
|
|
|
import android.graphics.Color
|
|
|
|
|
import android.view.KeyEvent
|
|
|
|
|
import android.view.View
|
|
|
|
|
import com.common.commonlib.db.entity.Gas
|
|
|
|
|
import com.github.mikephil.charting.components.XAxis
|
|
|
|
|
import com.github.mikephil.charting.data.Entry
|
|
|
|
|
import com.github.mikephil.charting.data.LineData
|
|
|
|
|
import com.github.mikephil.charting.data.LineDataSet
|
|
|
|
|
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
|
|
|
|
|
import com.yinuo.safetywatcher.R
|
|
|
|
|
import com.yinuo.safetywatcher.databinding.ActivityChartBinding
|
|
|
|
|
import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity
|
|
|
|
|
import com.yinuo.safetywatcher.watcher.utils.ChartBridge
|
|
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
|
import java.util.Locale
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChartActivity : NoOptionsActivity() {
|
|
|
|
|
private val mBinding: ActivityChartBinding by lazy {
|
|
|
|
|
ActivityChartBinding.inflate(layoutInflater)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getTopBarTitle(): String {
|
|
|
|
|
return getString(R.string.chart)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun generateContentView(): View {
|
|
|
|
|
return mBinding.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val dataFormat = SimpleDateFormat("MM/dd HH:mm:ss", Locale.ROOT)
|
|
|
|
|
|
|
|
|
|
var mSelectGas: String? = ""
|
|
|
|
|
var mGases: List<Gas>? = null
|
|
|
|
|
private var mXPositionIndex = 0
|
|
|
|
|
var mXScale = 5f
|
|
|
|
|
private val pointCountPerPage = 8
|
|
|
|
|
|
|
|
|
|
override fun initView() {
|
|
|
|
|
initData()
|
|
|
|
|
setKeyControl()
|
|
|
|
|
setChartView()
|
|
|
|
|
setXAxis()
|
|
|
|
|
setData()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun initData() {
|
|
|
|
|
mSelectGas = ChartBridge.getSelectGas()
|
|
|
|
|
val mapData = ChartBridge.getMapData()
|
|
|
|
|
mGases = mapData?.get(mSelectGas)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setKeyControl() {
|
|
|
|
|
mBinding.container.setOnKeyListener { v, keyCode, event ->
|
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.action == KeyEvent.ACTION_DOWN) {
|
|
|
|
|
// 获取图标Matrix参数
|
|
|
|
|
if (mXPositionIndex < mXScale - 1) {
|
|
|
|
|
mXPositionIndex++
|
|
|
|
|
// 获取图标Matrix参数
|
|
|
|
|
val mMatrix = mBinding.chart.viewPortHandler.matrixTouch
|
|
|
|
|
// 设置平移距离(这里是平移到图标的最右边)
|
|
|
|
|
val distance: Float = mBinding.chart.contentRect.width() * (mXPositionIndex)
|
|
|
|
|
mMatrix.setTranslate(-distance, 0f)
|
|
|
|
|
//两个参数分别是x,y轴的缩放比例。
|
|
|
|
|
mMatrix.preScale(mXScale, 1f)
|
|
|
|
|
// 刷新
|
|
|
|
|
mBinding.chart.viewPortHandler.refresh(mMatrix, mBinding.chart, true)
|
|
|
|
|
return@setOnKeyListener true
|
|
|
|
|
}
|
|
|
|
|
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && event.action == KeyEvent.ACTION_DOWN) {
|
|
|
|
|
// 获取图标Matrix参数
|
|
|
|
|
if (mXPositionIndex > 0) {
|
|
|
|
|
mXPositionIndex--
|
|
|
|
|
// 获取图标Matrix参数
|
|
|
|
|
val mMatrix = mBinding.chart.viewPortHandler.matrixTouch
|
|
|
|
|
// 设置平移距离(这里是平移到图标的最右边)
|
|
|
|
|
val distance: Float = mBinding.chart.contentRect.width() * (mXPositionIndex)
|
|
|
|
|
mMatrix.setTranslate(-distance, 0f)
|
|
|
|
|
//两个参数分别是x,y轴的缩放比例。
|
|
|
|
|
mMatrix.preScale(mXScale, 1f)
|
|
|
|
|
// 刷新
|
|
|
|
|
mBinding.chart.viewPortHandler.refresh(mMatrix, mBinding.chart, true)
|
|
|
|
|
return@setOnKeyListener true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return@setOnKeyListener false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setChartView() {
|
|
|
|
|
mBinding.chart.apply {
|
|
|
|
|
// disable description text
|
|
|
|
|
description.isEnabled = false
|
|
|
|
|
// enable touch gestures
|
|
|
|
|
setTouchEnabled(true)
|
|
|
|
|
setDrawGridBackground(false)
|
|
|
|
|
isScaleYEnabled = false
|
|
|
|
|
|
|
|
|
|
legend.isEnabled = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setXAxis() {
|
|
|
|
|
mBinding.chart.apply {
|
|
|
|
|
xAxis.apply {
|
|
|
|
|
position = XAxis.XAxisPosition.BOTTOM
|
|
|
|
|
textColor = Color.WHITE
|
|
|
|
|
textSize = 16f
|
|
|
|
|
|
|
|
|
|
setLabelCount((pointCountPerPage + 1).toInt(), true)
|
|
|
|
|
setVisibleXRangeMaximum(pointCountPerPage.toFloat())
|
|
|
|
|
granularity = 1f; // 设置X轴坐标之间的最小间隔
|
|
|
|
|
valueFormatter = object : IndexAxisValueFormatter() {
|
|
|
|
|
override fun getFormattedValue(value: Float): String {
|
|
|
|
|
if (value < (mGases?.size ?: 0)) {
|
|
|
|
|
val baseTime = mGases?.get(value.toInt())?.time
|
|
|
|
|
return dataFormat.format(baseTime)
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
axisRight.isEnabled = false
|
|
|
|
|
axisLeft.textColor = Color.WHITE
|
|
|
|
|
axisLeft.textSize = 20f
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setData() {
|
|
|
|
|
mBinding.chart.apply {
|
|
|
|
|
//设置数据
|
|
|
|
|
val entries = arrayListOf<Entry>()
|
|
|
|
|
mGases?.forEachIndexed { index, gas ->
|
|
|
|
|
entries.add(Entry(index.toFloat(), gas.gasValue.toFloat()))
|
|
|
|
|
}
|
|
|
|
|
//一个LineDataSet就是一条线
|
|
|
|
|
val lineDataSet = LineDataSet(entries, mSelectGas);
|
|
|
|
|
lineDataSet.valueTextColor = Color.WHITE
|
|
|
|
|
lineDataSet.valueTextSize = 20f
|
|
|
|
|
lineDataSet.color = Color.GREEN
|
|
|
|
|
|
|
|
|
|
val index = entries.size - 1
|
|
|
|
|
val scale = if (index % pointCountPerPage == 0) (
|
|
|
|
|
index / pointCountPerPage
|
|
|
|
|
) else (
|
|
|
|
|
(index + pointCountPerPage) / pointCountPerPage
|
|
|
|
|
)
|
|
|
|
|
mBinding.chart.xAxis.axisMaximum = (scale * pointCountPerPage).toFloat()
|
|
|
|
|
mXScale = scale.toFloat()
|
|
|
|
|
val data = LineData(lineDataSet);
|
|
|
|
|
setData(data)
|
|
|
|
|
|
|
|
|
|
val mMatrix = mBinding.chart.viewPortHandler.matrixTouch
|
|
|
|
|
mMatrix.postScale(mXScale, 1f);//两个参数分别是x,y轴的缩放比例。例如:将x轴的数据放大为之前的1.5倍
|
|
|
|
|
mBinding.chart.viewPortHandler.refresh(mMatrix, this, false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|