diff --git a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt index 51b77eb..5a509d2 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt @@ -26,8 +26,6 @@ R.color.color_7, R.color.color_8, R.color.color_9 ) - val YEARS = arrayOf(2018, 2019, 2020, 2021, 2022) - const val INTENT_PARAM = "intentParam" const val DEFAULT_SERVER_CONFIG = "defaultServerConfig" const val ACCOUNT = "account" diff --git a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt index 51b77eb..5a509d2 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt @@ -26,8 +26,6 @@ R.color.color_7, R.color.color_8, R.color.color_9 ) - val YEARS = arrayOf(2018, 2019, 2020, 2021, 2022) - const val INTENT_PARAM = "intentParam" const val DEFAULT_SERVER_CONFIG = "defaultServerConfig" const val ACCOUNT = "account" diff --git a/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..02f30c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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 isWaterData = true + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(isWater: Boolean) { + this.isWaterData = isWater + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + if (isWaterData) { + dataView.text = + String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + } else { + dataView.text = + String.format("设备数量:${decimalFormat.format(e.y.toString().toDouble())}") + } + } 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/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt index 51b77eb..5a509d2 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt @@ -26,8 +26,6 @@ R.color.color_7, R.color.color_8, R.color.color_9 ) - val YEARS = arrayOf(2018, 2019, 2020, 2021, 2022) - const val INTENT_PARAM = "intentParam" const val DEFAULT_SERVER_CONFIG = "defaultServerConfig" const val ACCOUNT = "account" diff --git a/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..02f30c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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 isWaterData = true + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(isWater: Boolean) { + this.isWaterData = isWater + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + if (isWaterData) { + dataView.text = + String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + } else { + dataView.text = + String.format("设备数量:${decimalFormat.format(e.y.toString().toDouble())}") + } + } 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/qd/smartwell/widgets/DetailsMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt deleted file mode 100644 index c6f92df..0000000 --- a/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.casic.qd.smartwell.widgets - -import android.content.Context -import android.widget.TextView -import com.casic.qd.smartwell.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 DetailsMarkerView(context: Context?) : MarkerView(context, R.layout.popu_marker) { - - private val decimalFormat = DecimalFormat("##0.0") - private val dayView: TextView = findViewById(R.id.dayView) - private val factoryView: TextView = findViewById(R.id.factoryView) - 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) - val data = e.data as String - try { - factoryView.text = data - dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") - if (data == "阀门" || data == "水表" || data == "管线" || data == "噪声仪") { - dayView.text = String.format("${decimalFormat.format(e.x)}年") - } else { - val date = xAxisDate[(e.x).toInt()] - val split = date.split("/") - dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") - } - } 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/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt index 51b77eb..5a509d2 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt @@ -26,8 +26,6 @@ R.color.color_7, R.color.color_8, R.color.color_9 ) - val YEARS = arrayOf(2018, 2019, 2020, 2021, 2022) - const val INTENT_PARAM = "intentParam" const val DEFAULT_SERVER_CONFIG = "defaultServerConfig" const val ACCOUNT = "account" diff --git a/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..02f30c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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 isWaterData = true + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(isWater: Boolean) { + this.isWaterData = isWater + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + if (isWaterData) { + dataView.text = + String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + } else { + dataView.text = + String.format("设备数量:${decimalFormat.format(e.y.toString().toDouble())}") + } + } 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/qd/smartwell/widgets/DetailsMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt deleted file mode 100644 index c6f92df..0000000 --- a/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.casic.qd.smartwell.widgets - -import android.content.Context -import android.widget.TextView -import com.casic.qd.smartwell.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 DetailsMarkerView(context: Context?) : MarkerView(context, R.layout.popu_marker) { - - private val decimalFormat = DecimalFormat("##0.0") - private val dayView: TextView = findViewById(R.id.dayView) - private val factoryView: TextView = findViewById(R.id.factoryView) - 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) - val data = e.data as String - try { - factoryView.text = data - dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") - if (data == "阀门" || data == "水表" || data == "管线" || data == "噪声仪") { - dayView.text = String.format("${decimalFormat.format(e.x)}年") - } else { - val date = xAxisDate[(e.x).toInt()] - val split = date.split("/") - dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") - } - } 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/qd/smartwell/widgets/LineChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt new file mode 100644 index 0000000..41302c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt @@ -0,0 +1,45 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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.0") + private val dayView: TextView = findViewById(R.id.dayView) + private val factoryView: TextView = findViewById(R.id.factoryView) + 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) + val data = e.data as String + try { + factoryView.text = data + dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + val date = xAxisDate[(e.x).toInt()] + val split = date.split("/") + dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") + } 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/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt index 51b77eb..5a509d2 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt @@ -26,8 +26,6 @@ R.color.color_7, R.color.color_8, R.color.color_9 ) - val YEARS = arrayOf(2018, 2019, 2020, 2021, 2022) - const val INTENT_PARAM = "intentParam" const val DEFAULT_SERVER_CONFIG = "defaultServerConfig" const val ACCOUNT = "account" diff --git a/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..02f30c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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 isWaterData = true + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(isWater: Boolean) { + this.isWaterData = isWater + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + if (isWaterData) { + dataView.text = + String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + } else { + dataView.text = + String.format("设备数量:${decimalFormat.format(e.y.toString().toDouble())}") + } + } 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/qd/smartwell/widgets/DetailsMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt deleted file mode 100644 index c6f92df..0000000 --- a/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.casic.qd.smartwell.widgets - -import android.content.Context -import android.widget.TextView -import com.casic.qd.smartwell.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 DetailsMarkerView(context: Context?) : MarkerView(context, R.layout.popu_marker) { - - private val decimalFormat = DecimalFormat("##0.0") - private val dayView: TextView = findViewById(R.id.dayView) - private val factoryView: TextView = findViewById(R.id.factoryView) - 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) - val data = e.data as String - try { - factoryView.text = data - dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") - if (data == "阀门" || data == "水表" || data == "管线" || data == "噪声仪") { - dayView.text = String.format("${decimalFormat.format(e.x)}年") - } else { - val date = xAxisDate[(e.x).toInt()] - val split = date.split("/") - dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") - } - } 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/qd/smartwell/widgets/LineChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt new file mode 100644 index 0000000..41302c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt @@ -0,0 +1,45 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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.0") + private val dayView: TextView = findViewById(R.id.dayView) + private val factoryView: TextView = findViewById(R.id.factoryView) + 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) + val data = e.data as String + try { + factoryView.text = data + dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + val date = xAxisDate[(e.x).toInt()] + val split = date.split("/") + dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") + } 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/layout/popu_bar_chart_marker.xml b/app/src/main/res/layout/popu_bar_chart_marker.xml new file mode 100644 index 0000000..bfe4e5a --- /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/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt index 51b77eb..5a509d2 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt @@ -26,8 +26,6 @@ R.color.color_7, R.color.color_8, R.color.color_9 ) - val YEARS = arrayOf(2018, 2019, 2020, 2021, 2022) - const val INTENT_PARAM = "intentParam" const val DEFAULT_SERVER_CONFIG = "defaultServerConfig" const val ACCOUNT = "account" diff --git a/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..02f30c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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 isWaterData = true + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(isWater: Boolean) { + this.isWaterData = isWater + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + if (isWaterData) { + dataView.text = + String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + } else { + dataView.text = + String.format("设备数量:${decimalFormat.format(e.y.toString().toDouble())}") + } + } 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/qd/smartwell/widgets/DetailsMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt deleted file mode 100644 index c6f92df..0000000 --- a/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.casic.qd.smartwell.widgets - -import android.content.Context -import android.widget.TextView -import com.casic.qd.smartwell.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 DetailsMarkerView(context: Context?) : MarkerView(context, R.layout.popu_marker) { - - private val decimalFormat = DecimalFormat("##0.0") - private val dayView: TextView = findViewById(R.id.dayView) - private val factoryView: TextView = findViewById(R.id.factoryView) - 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) - val data = e.data as String - try { - factoryView.text = data - dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") - if (data == "阀门" || data == "水表" || data == "管线" || data == "噪声仪") { - dayView.text = String.format("${decimalFormat.format(e.x)}年") - } else { - val date = xAxisDate[(e.x).toInt()] - val split = date.split("/") - dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") - } - } 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/qd/smartwell/widgets/LineChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt new file mode 100644 index 0000000..41302c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt @@ -0,0 +1,45 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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.0") + private val dayView: TextView = findViewById(R.id.dayView) + private val factoryView: TextView = findViewById(R.id.factoryView) + 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) + val data = e.data as String + try { + factoryView.text = data + dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + val date = xAxisDate[(e.x).toInt()] + val split = date.split("/") + dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") + } 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/layout/popu_bar_chart_marker.xml b/app/src/main/res/layout/popu_bar_chart_marker.xml new file mode 100644 index 0000000..bfe4e5a --- /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..f93f212 --- /dev/null +++ b/app/src/main/res/layout/popu_line_chart_marker.xml @@ -0,0 +1,41 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt index 13e8a73..87c1c61 100644 --- a/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt +++ b/app/src/main/java/com/casic/qd/smartwell/extensions/LineChart.kt @@ -17,8 +17,8 @@ rightAxis.isEnabled = false val leftAxis: YAxis = this.axisLeft leftAxis.axisMinimum = 0f - this.isScaleXEnabled = false //X轴不可缩放 - this.isScaleYEnabled = false //Y轴不可缩放 + this.isScaleXEnabled = true //X轴可缩放 + this.isScaleYEnabled = true //Y轴可缩放 //设置x轴 val xAxis: XAxis = this.xAxis xAxis.textColor = R.color.mainTextColor.convertColor() diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt index 6061377..c0f5c2d 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/ChartViewHelper.kt @@ -5,7 +5,8 @@ import com.casic.qd.smartwell.extensions.convertColor import com.casic.qd.smartwell.extensions.init import com.casic.qd.smartwell.model.LineChartEntryModel -import com.casic.qd.smartwell.widgets.DetailsMarkerView +import com.casic.qd.smartwell.widgets.BarChartMarkerView +import com.casic.qd.smartwell.widgets.LineChartMarkerView import com.github.mikephil.charting.charts.BarChart import com.github.mikephil.charting.charts.HorizontalBarChart import com.github.mikephil.charting.charts.LineChart @@ -57,7 +58,7 @@ val lineData = LineData(lineDataSets) lineData.setDrawValues(false) //添加自定义Marker - val markerView = DetailsMarkerView(BaseApplication.obtainInstance()) + val markerView = LineChartMarkerView(BaseApplication.obtainInstance()) markerView.chartView = chart markerView.setXAxisDate(xAxisDate) chart.marker = markerView @@ -98,6 +99,12 @@ 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(true) + chart.marker = markerView chart.data = barData chart.invalidate() } @@ -124,6 +131,12 @@ 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(false) + chart.marker = markerView chart.data = barData chart.invalidate() } diff --git a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt index 51b77eb..5a509d2 100644 --- a/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt +++ b/app/src/main/java/com/casic/qd/smartwell/utils/Constant.kt @@ -26,8 +26,6 @@ R.color.color_7, R.color.color_8, R.color.color_9 ) - val YEARS = arrayOf(2018, 2019, 2020, 2021, 2022) - const val INTENT_PARAM = "intentParam" const val DEFAULT_SERVER_CONFIG = "defaultServerConfig" const val ACCOUNT = "account" diff --git a/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt new file mode 100644 index 0000000..02f30c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/BarChartMarkerView.kt @@ -0,0 +1,50 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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 isWaterData = true + + fun setXAxisLabel(label: MutableList) { + this.xAxisLabel = label + } + + fun setBarDataType(isWater: Boolean) { + this.isWaterData = isWater + } + + //每次重绘,会调用此方法刷新数据 + override fun refreshContent(e: Entry, highlight: Highlight) { + super.refreshContent(e, highlight) + try { + factoryView.text = xAxisLabel[(e.x).toInt()] + if (isWaterData) { + dataView.text = + String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + } else { + dataView.text = + String.format("设备数量:${decimalFormat.format(e.y.toString().toDouble())}") + } + } 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/qd/smartwell/widgets/DetailsMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt deleted file mode 100644 index c6f92df..0000000 --- a/app/src/main/java/com/casic/qd/smartwell/widgets/DetailsMarkerView.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.casic.qd.smartwell.widgets - -import android.content.Context -import android.widget.TextView -import com.casic.qd.smartwell.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 DetailsMarkerView(context: Context?) : MarkerView(context, R.layout.popu_marker) { - - private val decimalFormat = DecimalFormat("##0.0") - private val dayView: TextView = findViewById(R.id.dayView) - private val factoryView: TextView = findViewById(R.id.factoryView) - 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) - val data = e.data as String - try { - factoryView.text = data - dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") - if (data == "阀门" || data == "水表" || data == "管线" || data == "噪声仪") { - dayView.text = String.format("${decimalFormat.format(e.x)}年") - } else { - val date = xAxisDate[(e.x).toInt()] - val split = date.split("/") - dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") - } - } 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/qd/smartwell/widgets/LineChartMarkerView.kt b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt new file mode 100644 index 0000000..41302c1 --- /dev/null +++ b/app/src/main/java/com/casic/qd/smartwell/widgets/LineChartMarkerView.kt @@ -0,0 +1,45 @@ +package com.casic.qd.smartwell.widgets + +import android.content.Context +import android.widget.TextView +import com.casic.qd.smartwell.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.0") + private val dayView: TextView = findViewById(R.id.dayView) + private val factoryView: TextView = findViewById(R.id.factoryView) + 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) + val data = e.data as String + try { + factoryView.text = data + dataView.text = String.format("用水量:${decimalFormat.format(e.y.toString().toDouble())}t") + val date = xAxisDate[(e.x).toInt()] + val split = date.split("/") + dayView.text = String.format(Locale.CHINA, "${split[0]}月${split[1]}日") + } 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/layout/popu_bar_chart_marker.xml b/app/src/main/res/layout/popu_bar_chart_marker.xml new file mode 100644 index 0000000..bfe4e5a --- /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..f93f212 --- /dev/null +++ b/app/src/main/res/layout/popu_line_chart_marker.xml @@ -0,0 +1,41 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/popu_marker.xml b/app/src/main/res/layout/popu_marker.xml deleted file mode 100644 index f93f212..0000000 --- a/app/src/main/res/layout/popu_marker.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - \ No newline at end of file