diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_text_tag_blue.xml b/app/src/main/res/drawable/bg_text_tag_blue.xml new file mode 100644 index 0000000..6b497b0 --- /dev/null +++ b/app/src/main/res/drawable/bg_text_tag_blue.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_text_tag_blue.xml b/app/src/main/res/drawable/bg_text_tag_blue.xml new file mode 100644 index 0000000..6b497b0 --- /dev/null +++ b/app/src/main/res/drawable/bg_text_tag_blue.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index acd7eb7..2c64371 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -77,6 +77,12 @@ + + + + + + diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_text_tag_blue.xml b/app/src/main/res/drawable/bg_text_tag_blue.xml new file mode 100644 index 0000000..6b497b0 --- /dev/null +++ b/app/src/main/res/drawable/bg_text_tag_blue.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index acd7eb7..2c64371 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -77,6 +77,12 @@ + + + + + + diff --git a/app/src/main/res/layout/include_home_alarm_chart.xml b/app/src/main/res/layout/include_home_alarm_chart.xml new file mode 100644 index 0000000..fbac58f --- /dev/null +++ b/app/src/main/res/layout/include_home_alarm_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_text_tag_blue.xml b/app/src/main/res/drawable/bg_text_tag_blue.xml new file mode 100644 index 0000000..6b497b0 --- /dev/null +++ b/app/src/main/res/drawable/bg_text_tag_blue.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index acd7eb7..2c64371 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -77,6 +77,12 @@ + + + + + + diff --git a/app/src/main/res/layout/include_home_alarm_chart.xml b/app/src/main/res/layout/include_home_alarm_chart.xml new file mode 100644 index 0000000..fbac58f --- /dev/null +++ b/app/src/main/res/layout/include_home_alarm_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/include_home_device_type_chart.xml b/app/src/main/res/layout/include_home_device_type_chart.xml new file mode 100644 index 0000000..f107c0c --- /dev/null +++ b/app/src/main/res/layout/include_home_device_type_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_text_tag_blue.xml b/app/src/main/res/drawable/bg_text_tag_blue.xml new file mode 100644 index 0000000..6b497b0 --- /dev/null +++ b/app/src/main/res/drawable/bg_text_tag_blue.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index acd7eb7..2c64371 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -77,6 +77,12 @@ + + + + + + diff --git a/app/src/main/res/layout/include_home_alarm_chart.xml b/app/src/main/res/layout/include_home_alarm_chart.xml new file mode 100644 index 0000000..fbac58f --- /dev/null +++ b/app/src/main/res/layout/include_home_alarm_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/include_home_device_type_chart.xml b/app/src/main/res/layout/include_home_device_type_chart.xml new file mode 100644 index 0000000..f107c0c --- /dev/null +++ b/app/src/main/res/layout/include_home_device_type_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/include_home_order_chart.xml b/app/src/main/res/layout/include_home_order_chart.xml new file mode 100644 index 0000000..952b01f --- /dev/null +++ b/app/src/main/res/layout/include_home_order_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_text_tag_blue.xml b/app/src/main/res/drawable/bg_text_tag_blue.xml new file mode 100644 index 0000000..6b497b0 --- /dev/null +++ b/app/src/main/res/drawable/bg_text_tag_blue.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index acd7eb7..2c64371 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -77,6 +77,12 @@ + + + + + + diff --git a/app/src/main/res/layout/include_home_alarm_chart.xml b/app/src/main/res/layout/include_home_alarm_chart.xml new file mode 100644 index 0000000..fbac58f --- /dev/null +++ b/app/src/main/res/layout/include_home_alarm_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/include_home_device_type_chart.xml b/app/src/main/res/layout/include_home_device_type_chart.xml new file mode 100644 index 0000000..f107c0c --- /dev/null +++ b/app/src/main/res/layout/include_home_device_type_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/include_home_order_chart.xml b/app/src/main/res/layout/include_home_order_chart.xml new file mode 100644 index 0000000..952b01f --- /dev/null +++ b/app/src/main/res/layout/include_home_order_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/popu_bar_chart_marker.xml b/app/src/main/res/layout/popu_bar_chart_marker.xml new file mode 100644 index 0000000..ba27061 --- /dev/null +++ b/app/src/main/res/layout/popu_bar_chart_marker.xml @@ -0,0 +1,32 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt index 8e01e1d..d321829 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/extensions/Long.kt @@ -20,4 +20,12 @@ } return true +} + +fun Long.timestampToLastWeekTime(): String { + val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) + val calendar = Calendar.getInstance() + calendar.time = Date(this) + calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6) + return dateFormat.format(calendar.time) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt index 17dd212..e68e7c1 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/fragment/HomePageFragment.kt @@ -2,7 +2,6 @@ import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -15,10 +14,8 @@ import com.amap.api.maps.CoordinateConverter import com.amap.api.maps.model.* import com.casic.app.smartwell.sanxi.R -import com.casic.app.smartwell.sanxi.extensions.show -import com.casic.app.smartwell.sanxi.extensions.showRouteOnMap -import com.casic.app.smartwell.sanxi.extensions.toDeptName -import com.casic.app.smartwell.sanxi.extensions.toTypeName +import com.casic.app.smartwell.sanxi.extensions.* +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel import com.casic.app.smartwell.sanxi.model.WellListModel import com.casic.app.smartwell.sanxi.utils.ChartViewHelper import com.casic.app.smartwell.sanxi.utils.Constant @@ -28,14 +25,19 @@ import com.casic.app.smartwell.sanxi.vm.WorkOrderViewModel import com.casic.app.smartwell.sanxi.widgets.EasyPopupWindow import com.casic.app.smartwell.sanxi.widgets.GaoDeClusterMarkerView +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.PieEntry import com.google.android.material.bottomsheet.BottomSheetBehavior import com.pengxh.app.multilib.utils.SizeUtil import com.pengxh.app.multilib.widget.dialog.AlertControlDialog import kotlinx.android.synthetic.main.fragment_home.view.* +import kotlinx.android.synthetic.main.include_home_alarm_chart.* import kotlinx.android.synthetic.main.include_home_data_overview.view.* import kotlinx.android.synthetic.main.include_home_device_chart.* +import kotlinx.android.synthetic.main.include_home_device_type_chart.* import kotlinx.android.synthetic.main.include_home_well_chart.* +import kotlinx.android.synthetic.main.include_home_well_type_chart.* class HomePageFragment : Fragment(), AMap.OnMapLoadedListener, AMap.OnCameraChangeListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, AMap.OnInfoWindowClickListener { @@ -67,6 +69,8 @@ * 自定义Marker弹出框 * */ private var infoWindow: View? = null + private val xAxisDate: MutableList = ArrayList() + private val entryModels: MutableList = ArrayList() override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -95,7 +99,7 @@ val bottomSheetBehavior = BottomSheetBehavior.from(homeView.bottomBehaviorLayout) homeView.coordinatorLayout.post { bottomSheetBehavior.isFitToContents = false - bottomSheetBehavior.halfExpandedRatio = 0.3f + bottomSheetBehavior.halfExpandedRatio = 0.55f bottomSheetBehavior.isHideable = false bottomSheetBehavior.peekHeight = SizeUtil.dp2px(requireContext(), 30f) } @@ -133,17 +137,14 @@ .snippet(well.wellName) ) } else { - Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度不在国内,异常经纬度 ===> [${lng},${lat}]") } } else { - Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") +// Log.d(kTag, "${well.wellCode}闸井经纬度异常,异常经纬度 ===> [${lng},${lat}]") } } //计算所有点的中心点位置 val centerLatLng = LatLng(latitudeList.average(), longitudeList.average()) - Log.d( - kTag, "所有闸井中心点位置 ===> [${latitudeList.average()}, ${longitudeList.average()}]" - ) //移动到指定经纬度 val cameraPosition = CameraPosition(centerLatLng, 13f, 0f, 0f) val cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition) @@ -191,6 +192,93 @@ homeView.unCompleteOrderView.text = (beforeGet + beforeConfirm + inHandle).toString() }) + + //获取闸井类别 + wellViewModel.obtainWellType() + wellViewModel.wellTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = java.util.ArrayList() + val barEntries: MutableList = java.util.ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.wellTypeName.toString()) + val dataValue = if (dataBean.wellCount.toString().isBlank()) { + 0f + } else { + dataBean.wellCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + wellTypeChart, xAxisLabel, barEntries, "wellType" + ) + } + }) + + //获取设备类别 + deviceViewModel.obtainDeviceType() + deviceViewModel.deviceTypeModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val xAxisLabel: MutableList = ArrayList() + val barEntries: MutableList = ArrayList() + it.data?.forEachIndexed { index, dataBean -> + xAxisLabel.add(dataBean.deviceType.toString()) + val dataValue = if (dataBean.deviceCount.toString().isBlank()) { + 0f + } else { + dataBean.deviceCount!!.toFloat() + } + //柱状图 + barEntries.add(BarEntry(index.toFloat(), dataValue)) + } + ChartViewHelper.setBarChartData( + deviceTypeChart, xAxisLabel, barEntries, "deviceType" + ) + } + }) + + //近一周报警统计 + val currentTimeMillis = System.currentTimeMillis() + alarmViewModel.obtainDayAlarm( + "", currentTimeMillis.timestampToLastWeekTime(), currentTimeMillis.timestampToTime(), + ) + alarmViewModel.dayAlarmModel.observe(viewLifecycleOwner, { + if (it.code == 200) { + val dataModel = it.data!! + dataModel.forEach { dateModel -> + val split = dateModel.date.toString().split("年") + xAxisDate.add(split[1]) + } + + val lines = 2 + for (i in 0 until lines) { + //每个entries都是一条折线 + val entries: ArrayList = ArrayList() + val entryModel = LineChartEntryModel() + + dataModel.forEachIndexed { j, dataBean -> + entryModel.lineColor = Constant.COLORS[i].convertColor(requireContext()) + + if (i == 0) { + entries.add( + Entry( + j.toFloat(), dataBean.alarmTimes!!.toFloat(), "报警次数" + ) + ) + } else { + entries.add( + Entry( + j.toFloat(), dataBean.alarmWells!!.toFloat(), "报警井数" + ) + ) + } + } + entryModel.entryList = entries + entryModels.add(entryModel) + } + ChartViewHelper.setLineChartData(alarmLineChart, xAxisDate, entryModels) + } + }) return homeView } diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java new file mode 100644 index 0000000..3025ee0 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DayAlarmModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DayAlarmModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String alarmWells; + private String date; + private String alarmTimes; + + public String getAlarmWells() { + return alarmWells; + } + + public void setAlarmWells(String alarmWells) { + this.alarmWells = alarmWells; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAlarmTimes() { + return alarmTimes; + } + + public void setAlarmTimes(String alarmTimes) { + this.alarmTimes = alarmTimes; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java new file mode 100644 index 0000000..e146a8f --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/DeviceTypeModel.java @@ -0,0 +1,64 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class DeviceTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String deviceType; + private String deviceCount; + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public String getDeviceCount() { + return deviceCount; + } + + public void setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt new file mode 100644 index 0000000..01babab --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/LineChartEntryModel.kt @@ -0,0 +1,14 @@ +package com.casic.app.smartwell.sanxi.model + +import com.casic.app.smartwell.sanxi.R +import com.casic.app.smartwell.sanxi.base.BaseApplication +import com.casic.app.smartwell.sanxi.extensions.convertColor +import com.github.mikephil.charting.data.Entry + +class LineChartEntryModel { + //折线数据 + var entryList: List? = null + + //折线颜色 + var lineColor = R.color.mainThemeColor.convertColor(BaseApplication.obtainInstance()) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java new file mode 100644 index 0000000..9d3581b --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/model/WellTypeModel.java @@ -0,0 +1,73 @@ +package com.casic.app.smartwell.sanxi.model; + +import java.util.List; + +public class WellTypeModel { + + private int code; + private List data; + private String message; + private boolean success; + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public static class DataDTO { + private String wellTypeName; + private String wellCount; + private String wellType; + + public String getWellTypeName() { + return wellTypeName; + } + + public void setWellTypeName(String wellTypeName) { + this.wellTypeName = wellTypeName; + } + + public String getWellCount() { + return wellCount; + } + + public void setWellCount(String wellCount) { + this.wellCount = wellCount; + } + + public String getWellType() { + return wellType; + } + + public void setWellType(String wellType) { + this.wellType = wellType; + } + } +} diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt index a011b12..91dc43c 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/ChartViewHelper.kt @@ -4,10 +4,16 @@ import com.casic.app.smartwell.sanxi.base.BaseApplication import com.casic.app.smartwell.sanxi.extensions.convertColor import com.casic.app.smartwell.sanxi.extensions.init +import com.casic.app.smartwell.sanxi.model.LineChartEntryModel +import com.casic.app.smartwell.sanxi.view.LineChartMarkerView +import com.casic.app.smartwell.sanxi.widgets.BarChartMarkerView +import com.github.mikephil.charting.charts.HorizontalBarChart +import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.charts.PieChart -import com.github.mikephil.charting.data.PieData -import com.github.mikephil.charting.data.PieDataSet -import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.* +import com.github.mikephil.charting.formatter.ValueFormatter +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet /** @@ -48,62 +54,77 @@ chart.invalidate() } + /** + * 横向柱状图 + * */ + fun setBarChartData( + chart: HorizontalBarChart, + xAxisLabel: MutableList, + entries: List, + dataType: String + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化饼图 + chart.init(xAxisLabel) + val barDataSets: MutableList = ArrayList() + //绑定数据 + val dataSet = BarDataSet(entries, "") + dataSet.color = R.color.color_6.convertColor(context) + barDataSets.add(dataSet) + val barData = BarData(barDataSets) + barData.barWidth = 0.75f + //添加自定义Marker + val markerView = BarChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisLabel(xAxisLabel) + markerView.setBarDataType(dataType) + chart.marker = markerView + chart.data = barData + chart.invalidate() + } /** * 折线图 * */ -// fun setLineChartData( -// chart: LineChart, -// xAxisDate: MutableList, -// entryModels: List -// ) { -// //每次加载数据都初始化折线图 -// chart.init() -// //绑定数据 -// val lineDataSets: MutableList = ArrayList() -// val entrySize = entryModels[0].entryList!!.size -// entryModels.forEachIndexed { index, it -> -// //设置数据 -// val dataSet = if (entrySize == 1) { -// LineDataSet(it.entryList, it.entryList?.get(0)?.data.toString()) -// } else { -// LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) -// } -// dataSet.setDrawCircles(true) -// //线条颜色 -// dataSet.color = it.lineColor -// //圆点颜色 -// dataSet.setCircleColor(it.lineColor) -// dataSet.setDrawFilled(true) -// dataSet.fillColor = it.lineColor -// dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER -// lineDataSets.add(dataSet) -// } -// val lineData = LineData(lineDataSets) -// lineData.setDrawValues(false) -// //添加自定义Marker -// val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisDate(xAxisDate) -// chart.marker = markerView -// //设置X轴坐标 -// val xAxis = chart.xAxis -// xAxis.valueFormatter = if (entrySize == 1) { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[0] -// } -// } -// } else { -// object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return xAxisDate[value.toInt()] -// } -// } -// } -// chart.data = lineData -// chart.invalidate() -// } + fun setLineChartData( + chart: LineChart, xAxisDate: MutableList, entryModels: List + ) { + val context = BaseApplication.obtainInstance() + //每次加载数据都初始化折线图 + chart.init() + //绑定数据 + val lineDataSets: MutableList = ArrayList() + entryModels[0].entryList!!.size + entryModels.forEachIndexed { index, it -> + //设置数据 + val dataSet = LineDataSet(it.entryList, it.entryList?.get(index)?.data.toString()) + dataSet.setDrawCircles(true) + //线条颜色 + dataSet.color = it.lineColor + //圆点颜色 + dataSet.setCircleColor(it.lineColor) + dataSet.setDrawFilled(true) + dataSet.fillColor = it.lineColor + dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER + lineDataSets.add(dataSet) + } + val lineData = LineData(lineDataSets) + lineData.setDrawValues(false) + //添加自定义Marker + val markerView = LineChartMarkerView(context) + markerView.chartView = chart + markerView.setXAxisDate(xAxisDate) + chart.marker = markerView + //设置X轴坐标 + val xAxis = chart.xAxis + xAxis.valueFormatter = object : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisDate[value.toInt()] + } + } + chart.data = lineData + chart.invalidate() + } /** * 竖直柱状图 @@ -137,36 +158,4 @@ // chart.data = barData // chart.invalidate() // } - - /** - * 横向柱状图 - * */ -// fun setBarChartData( -// chart: HorizontalBarChart, -// xAxisLabel: MutableList, -// entries: List -// ) { -// //每次加载数据都初始化饼图 -// chart.init(xAxisLabel) -// val barDataSets: MutableList = ArrayList() -// //绑定数据 -// val dataSet = BarDataSet(entries, "") -// dataSet.color = R.color.color_6.convertColor() -// dataSet.valueFormatter = object : ValueFormatter() { -// override fun getFormattedValue(value: Float): String { -// return DecimalFormat("##0").format(value.toDouble()) -// } -// } -// barDataSets.add(dataSet) -// val barData = BarData(barDataSets) -// barData.barWidth = 0.5f -// //添加自定义Marker -// val markerView = BarChartMarkerView(BaseApplication.obtainInstance()) -// markerView.chartView = chart -// markerView.setXAxisLabel(xAxisLabel) -// markerView.setBarDataType("") -// chart.marker = markerView -// chart.data = barData -// chart.invalidate() -// } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt index 7b63a83..2314a33 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitService.kt @@ -185,10 +185,27 @@ /** * 获取闸井类别 */ - @GET("/dict/code/sluicewellType") + @GET("/statics/wellStaticsByType") suspend fun obtainWellType(@Header("token") token: String): String /** + * 获取设备类别 + */ + @GET("/statics/deviceStaticsByType") + suspend fun obtainDeviceType(@Header("token") token: String): String + + /** + * 按天获取报警 + */ + @GET("/statics/alarmsByDay") + suspend fun obtainDayAlarm( + @Header("token") token: String, + @Query("deviceType") deviceType: String, + @Query("beginTime") beginTime: String, + @Query("endTime") endTime: String + ): String + + /** * 获取获取闸井列表 * * @param keywords 关键字 diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt index 0d19361..b6d6134 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/utils/retrofit/RetrofitServiceManager.kt @@ -186,14 +186,28 @@ suspend fun countWorkOrderByState(): String { return api.countWorkOrderByState(AuthenticationHelper.token!!) } -// -// /** -// * 获取闸井类别 -// */ -// suspend fun obtainWellType(): String { -// return api.obtainWellType(AuthenticationHelper.token!!) -// } -// + + /** + * 获取闸井类别 + */ + suspend fun obtainWellType(): String { + return api.obtainWellType(AuthenticationHelper.token!!) + } + + /** + * 获取设备类别 + */ + suspend fun obtainDeviceType(): String { + return api.obtainDeviceType(AuthenticationHelper.token!!) + } + + /** + * 按天获取报警 + */ + suspend fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String): String { + return api.obtainDayAlarm(AuthenticationHelper.token!!, deviceType, startDate, endDate) + } + // /** // * 获取获取闸井列表 // */ diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt new file mode 100644 index 0000000..51d03f9 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/view/LineChartMarkerView.kt @@ -0,0 +1,40 @@ +package com.casic.app.smartwell.sanxi.view + +import android.content.Context +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class LineChartMarkerView(context: Context?) : + MarkerView(context, R.layout.popu_line_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val dayView: TextView = findViewById(R.id.dayView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisDate: MutableList = ArrayList() + + fun setXAxisDate(date: MutableList) { + this.xAxisDate = date + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + dataView.text = String.format("报警数:${decimalFormat.format(e.y.toString().toDouble())}") + dayView.text = xAxisDate[(e.x).toInt()] + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt index 3ee2f6f..c48debf 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/AlarmViewModel.kt @@ -9,6 +9,7 @@ import com.casic.app.smartwell.sanxi.model.AlarmContentModel import com.casic.app.smartwell.sanxi.model.AlarmListModel import com.casic.app.smartwell.sanxi.model.AlarmTypeModel +import com.casic.app.smartwell.sanxi.model.DayAlarmModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -21,6 +22,7 @@ val typeModel = MutableLiveData() val contentModel = MutableLiveData() val alarmCountModel = MutableLiveData() + val dayAlarmModel = MutableLiveData() fun obtainAlarmType() = launch({ val response = RetrofitServiceManager.obtainAlarmType() @@ -104,4 +106,18 @@ }, { it.printStackTrace() }) + + fun obtainDayAlarm(deviceType: String, startDate: String, endDate: String) = launch({ + val response = RetrofitServiceManager.obtainDayAlarm(deviceType, startDate, endDate) + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + dayAlarmModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt index 10be999..af684df 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/DeviceViewModel.kt @@ -6,14 +6,17 @@ import com.casic.app.smartwell.sanxi.extensions.separateResponseCode import com.casic.app.smartwell.sanxi.extensions.show import com.casic.app.smartwell.sanxi.extensions.toErrorMessage +import com.casic.app.smartwell.sanxi.model.DeviceTypeModel import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import org.json.JSONObject class DeviceViewModel : BaseViewModel() { private val gson = Gson() val countResultModel = MutableLiveData>() + val deviceTypeModel = MutableLiveData() fun countDeviceByState() = launch({ val response = RetrofitServiceManager.countDeviceByState() @@ -33,4 +36,18 @@ }, { it.printStackTrace() }) + + fun obtainDeviceType() = launch({ + val response = RetrofitServiceManager.obtainDeviceType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + deviceTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt index 80df905..66a4d85 100644 --- a/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/vm/WellViewModel.kt @@ -8,6 +8,7 @@ import com.casic.app.smartwell.sanxi.extensions.toErrorMessage import com.casic.app.smartwell.sanxi.model.WellDetailModel import com.casic.app.smartwell.sanxi.model.WellListModel +import com.casic.app.smartwell.sanxi.model.WellTypeModel import com.casic.app.smartwell.sanxi.utils.LoadState import com.casic.app.smartwell.sanxi.utils.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -20,6 +21,7 @@ val detailModel = MutableLiveData() val allWellModel = MutableLiveData() val countResultModel = MutableLiveData>() + val wellTypeModel = MutableLiveData() fun obtainWellDetail(id: String) = launch({ loadState.value = LoadState.Loading @@ -71,4 +73,18 @@ }, { it.printStackTrace() }) + + fun obtainWellType() = launch({ + val response = RetrofitServiceManager.obtainWellType() + val responseCode = response.separateResponseCode() + if (responseCode == 200) { + wellTypeModel.value = gson.fromJson( + response, object : TypeToken() {}.type + ) + } else { + response.toErrorMessage().show() + } + }, { + it.printStackTrace() + }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..d671c11 --- /dev/null +++ b/app/src/main/java/com/casic/app/smartwell/sanxi/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.app.smartwell.sanxi.widgets + +import android.content.Context +import android.util.Log +import android.widget.TextView +import com.casic.app.smartwell.sanxi.R +import com.github.mikephil.charting.components.MarkerView +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.utils.MPPointF +import java.text.DecimalFormat +import java.util.* + +class BarChartMarkerView(context: Context?) : MarkerView(context, R.layout.popu_bar_chart_marker) { + + private val decimalFormat = DecimalFormat("##0") + private val factoryView: TextView = findViewById(R.id.factoryView) + private val dataView: TextView = findViewById(R.id.dataView) + private var xAxisLabel: MutableList = ArrayList() + private var dateType = "" + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(type: String) { + this.dateType = type + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + val dataValue = decimalFormat.format(e.y.toString().toDouble()) + when (dateType) { + "wellType" -> dataView.text = String.format("井数量:${dataValue}") + "deviceType" -> dataView.text = String.format("设备数量:${dataValue}") + else -> dataView.text = String.format("数量:${dataValue}") + } + } catch (e1: Exception) { + e1.printStackTrace() + } + super.refreshContent(e, highlight) + } + + override fun getOffset(): MPPointF { + return MPPointF((-(width shr 1)).toFloat(), (-height).toFloat()) + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_stroke_layout_blue.xml b/app/src/main/res/drawable/bg_stroke_layout_blue.xml new file mode 100644 index 0000000..26ddc25 --- /dev/null +++ b/app/src/main/res/drawable/bg_stroke_layout_blue.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_text_tag_blue.xml b/app/src/main/res/drawable/bg_text_tag_blue.xml new file mode 100644 index 0000000..6b497b0 --- /dev/null +++ b/app/src/main/res/drawable/bg_text_tag_blue.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index acd7eb7..2c64371 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -77,6 +77,12 @@ + + + + + + diff --git a/app/src/main/res/layout/include_home_alarm_chart.xml b/app/src/main/res/layout/include_home_alarm_chart.xml new file mode 100644 index 0000000..fbac58f --- /dev/null +++ b/app/src/main/res/layout/include_home_alarm_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/include_home_device_type_chart.xml b/app/src/main/res/layout/include_home_device_type_chart.xml new file mode 100644 index 0000000..f107c0c --- /dev/null +++ b/app/src/main/res/layout/include_home_device_type_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/include_home_order_chart.xml b/app/src/main/res/layout/include_home_order_chart.xml new file mode 100644 index 0000000..952b01f --- /dev/null +++ b/app/src/main/res/layout/include_home_order_chart.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/popu_bar_chart_marker.xml b/app/src/main/res/layout/popu_bar_chart_marker.xml new file mode 100644 index 0000000..ba27061 --- /dev/null +++ b/app/src/main/res/layout/popu_bar_chart_marker.xml @@ -0,0 +1,32 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/popu_line_chart_marker.xml b/app/src/main/res/layout/popu_line_chart_marker.xml new file mode 100644 index 0000000..13fbdd2 --- /dev/null +++ b/app/src/main/res/layout/popu_line_chart_marker.xml @@ -0,0 +1,30 @@ + + + + + + + \ No newline at end of file