Преглед на файлове

处理图表分时图bug

jieyu преди 4 години
родител
ревизия
ca89060914

+ 142 - 32
RMA/app/src/main/java/cn/muchinfo/rma/global/data/chart/ChartTSData.kt

@@ -6,8 +6,10 @@ import cn.muchinfo.rma.global.toShowTime2
 import cn.muchinfo.rma.view.autoWidget.toArrayList
 import com.blankj.utilcode.util.TimeUtils
 import kotlinx.coroutines.selects.select
+import java.text.ParsePosition
 import java.text.SimpleDateFormat
 import java.util.*
+import kotlin.collections.ArrayList
 
 data class ChartTSData(
     val decimalPlace: String? = "",  //                         小数位
@@ -17,36 +19,98 @@ data class ChartTSData(
     val outGoodsCode: String? = "",//                           外部商品代码
     val preSettle: String? = "",//                              昨结
     val startTime: String? = "",//                              开始时间
-    val tradeDate: String? = ""//                               交易日
+    val tradeDate: String? = "",//                               交易日
+    val Count: Int? = 0//                                      数据总条树
 ){
     /**
      * 获取用于分时图的收盘价列表
      */
-    fun getTimeChartPriceData() : List<Double> {
-        val returnPrice = if(historyDatas != null && historyDatas.isNotEmpty()){
-            historyDatas.map {
-                it.c!!.toDouble()
+    fun getTimeChartPriceData(timeList: List<String>) : List<Double> {
+        val historyHashMap = hashMapOf<String, Double>()
+        if(historyDatas != null && historyDatas.isNotEmpty()){
+            historyDatas.forEach {
+                historyHashMap[it.getTime().toShowTime2()] = it.c!!.toDouble()
             }
-        }else{
-            arrayListOf();
-        }.toArrayList()
-//        returnPrice.add(4000.0)
-        return returnPrice
+        }
+        return timeList.map{
+            if(historyHashMap.containsKey(it)){
+                historyHashMap[it]
+            }else{
+                -1.0
+            }
+        }.toArrayList() as List<Double>
+
+//        val returnPrice = if(historyDatas != null && historyDatas.isNotEmpty()){
+//            historyDatas.map {
+//                it.c!!.toDouble()
+//            }
+//        }else{
+//            arrayListOf();
+//        }.toArrayList()
+//////        returnPrice.add(4000.0)
+//        return returnPrice
     }
 
     /**
      * 获取用于分时图的时间列表
      */
     fun getTimeChartPriceTime() : List<String> {
-        val returnTime = if(historyDatas != null && historyDatas.isNotEmpty()){
-            historyDatas.map {
-                it.ts?.toShowTime2() ?: ""
+        val list = arrayListOf<String>()
+        var startTimeLong = startTime?.toLongTime()
+        val endTimeLong = endTime?.toLongTime()
+        if(historyDatas != null && historyDatas.isNotEmpty()){
+            val historyTimeStartLong = historyDatas.first().getTime().replace("T"," ").replace("+08:00", "").toLongFormatTime()  // 实际开始时间
+            var historyTimeEndLong = historyDatas.last().getTime().replace("T"," ").replace("+08:00", "").toLongFormatTime()  // 实际结束时间
+            if(historyTimeStartLong == startTimeLong){  // 开始时间一致
+                historyDatas.forEach {
+                    list.add(it.getTime().toShowTime2())
+                }
             }
-        }else{
-            arrayListOf()
-        }.toArrayList()
-//        returnTime.add(endTime?.toShowTime2() ?: "")
-        return returnTime
+
+            if(historyTimeStartLong > startTimeLong!!){
+                // 实际开始时间在开收市计划之外
+                // 则补充相应的时间节点
+                val startCount =  (historyTimeEndLong - startTimeLong) / 60000  // 需要加入点的个数
+                for(index in 0 until startCount){
+                    startTimeLong += 60000
+                    list.add(startTimeLong.toStringTime())
+                }
+                list.addAll(historyDatas.map{
+                    it.getTime().toShowTime2()
+                })
+            }
+
+            if(historyTimeEndLong < endTimeLong!!){
+                // 实际结束时间小于开收计划结束时间
+                // 则补充相应的时间点
+                val endCount = (endTimeLong - historyTimeEndLong) / 60000 // 需要补充的点的个数
+                for (index in 1..endCount){
+                    historyTimeEndLong += 60000
+                    list.add(historyTimeEndLong.toStringTime())
+                }
+            }
+        }
+
+//        val space = 60000
+//        val mCount = (endTimeLong!! - startTimeLong!!) / space
+//
+//        for(index in 0..mCount){
+//            // 第一个点是startTime
+//            // 最后一个点是endTime
+//            // 一共Count个点
+//            list.add(startTimeLong.toStringTime())
+//            startTimeLong += space
+//        }
+        return list
+//        val returnTime = if(historyDatas != null && historyDatas.isNotEmpty()){
+//            historyDatas.map {
+//                it.ts?.toShowTime2() ?: ""
+//            }
+//        }else{
+//            arrayListOf()
+//        }.toArrayList()
+////        returnTime.add(endTime?.toShowTime2() ?: "")
+//        return returnTime
     }
 
     @SuppressLint("SimpleDateFormat")
@@ -66,22 +130,56 @@ data class ChartTSData(
         return timeString
     }
 
+    @SuppressLint("SimpleDateFormat")
+    fun String.toLongFormatTime(): Long{
+        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).parse(this, ParsePosition(0)).time
+    }
+
+    @SuppressLint("SimpleDateFormat")
+    fun String.toLongTime(): Long{
+        return SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+08:00", Locale.CHINA).parse(this, ParsePosition(0)).time
+    }
+
+    fun Long.toStringTime(): String{
+        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(this)
+    }
+
+
     /**
      * 如果均线数据是补得数据  则计算数值时直接沿用上一口价格
      * 计算分时图 AVG10
      */
-    fun getTimeChartAVG(avg: Int) : List<Double>{
-        var avg10 = arrayListOf<Double>()
-        var save10 = arrayListOf<Double>()  // 这里保存的是10个有效的价格(补得数据则被跳过)
+    fun getTimeChartAVG(avg: Int, list: List<String>) : List<Double>{
+        var defaultList = arrayListOf<HistoryDatas>()
+        val historyMap = hashMapOf<String, HistoryDatas>()
+        val avg10 = arrayListOf<Double>()
+        val save10 = arrayListOf<Double>()  // 这里保存的是10个有效的价格(补得数据则被跳过)
         if(historyDatas != null && historyDatas.isNotEmpty()){
-            for(index in historyDatas.indices){
-                if(historyDatas[index].getIsSupplementaryData()){
+            // 这里提前处理一下historyData 方便查询
+            historyDatas.forEach {
+                historyMap[it.getTime().toShowTime2()] = it
+            }
+            // 这里不能循环historyData了  需要循环已经生成的 x 轴点数据
+            // 这里生成一个history列表  列表里一部分数据来自historyData  另一部分则是默认历史数据
+            val default = HistoryDatas()
+            default.f = "false" // 不是补数据
+            default.c = preSettle!!
+            defaultList = list.map {
+                if(historyMap.containsKey(it)){  // 如果有这个点 则把数据插入
+                    historyMap[it]
+                }else{
+                    default
+                }
+            }.toArrayList() as ArrayList<HistoryDatas>
+            // 先生成所有数据 里面填入的都是昨结
+            for(index in defaultList.indices){
+                if(defaultList[index].getIsSupplementaryData()){
                     if(avg10.size > 0) {
                         avg10.add(avg10.last())
                     }
                     continue
                 }else{
-                    save10.add(historyDatas[index].getClosePrice().toDouble())
+                    save10.add(defaultList[index].getClosePrice().toDouble())
                     if(save10.size == avg){ // 第一次满足10个数据  计算代数平均
                         avg10.add(save10.sum().div(save10.size))
                     }else if(save10.size > avg){ // 之后每次满足都要移除最前面的数据
@@ -92,18 +190,30 @@ data class ChartTSData(
                         continue
                     }
                 }
-//                if(index == 0){
-//                    avg10.add(historyDatas[index].c!!.toDouble())
+            }
+//        if(historyDatas != null && historyDatas.isNotEmpty()){
+//            for(index in historyDatas.indices){
+//                if(historyDatas[index].getIsSupplementaryData()){
+//                    if(avg10.size > 0) {
+//                        avg10.add(avg10.last())
+//                    }
+//                    continue
 //                }else{
-//                    if(historyDatas[index - 1].c.equals(historyDatas[index].c)){
-//                        avg10.add(historyDatas[index].c!!.toDouble())
-//                    }else {
-//                        avg10.add(((historyDatas[index - 1].c!!.toDouble() * historyDatas.size) + historyDatas[index].c!!.toDouble()) / (historyDatas.size + 1))
-//                    }6
+//                    save10.add(historyDatas[index].getClosePrice().toDouble())
+//                    if(save10.size == avg){ // 第一次满足10个数据  计算代数平均
+//                        avg10.add(save10.sum().div(save10.size))
+//                    }else if(save10.size > avg){ // 之后每次满足都要移除最前面的数据
+//                        save10.removeAt(0)
+//                        avg10.add(save10.sum().div(save10.size))
+//                    }else{
+//                        avg10.add(save10.sum().div(save10.size))
+//                        continue
+//                    }
 //                }
-            }
+//            }
         }
         return avg10;
+
     }
 
     fun getMaxValue() : Double{

+ 2 - 2
RMA/app/src/main/java/cn/muchinfo/rma/global/data/chart/HistoryDatas.kt

@@ -4,8 +4,8 @@ package cn.muchinfo.rma.global.data.chart
  * 历史数据
  */
 data class HistoryDatas(
-    val c: String? = "", // 收盘价
-    val f: String? = "", // 是否补充数据
+    var c: String? = "", // 收盘价
+    var f: String? = "", // 是否补充数据
     val h: String? = "", // 最高价
     val hv: String? = "",// 持仓量
     val l: String? = "",//  最低价

+ 9 - 5
RMA/app/src/main/java/cn/muchinfo/rma/view/base/chart/old/timeCharts/TimeChartView.java

@@ -104,14 +104,18 @@ public class TimeChartView extends FrameLayout implements ITouchEventResponse {
         // 分钟线
         LineEntity minutesLine = new LineEntity();
         minutesLine.setTitle("M");
-        minutesLine.setLineData(timeChartData.getTimeChartPriceData());
+
+        //时间列表
+        List<String> timeList = timeChartData.getTimeChartPriceTime();
+
+        minutesLine.setLineData(timeChartData.getTimeChartPriceData(timeList));
         minutesLine.setLineColor(getResources().getColor(R.color.chart_time_min_line_color));
-        minutesLine.setLineTime(timeChartData.getTimeChartPriceTime());
+        minutesLine.setLineTime(timeList);
         // 十日均线
         LineEntity avg10Line = new LineEntity();
         avg10Line.setTitle("AVG10");
-        avg10Line.setLineData(timeChartData.getTimeChartAVG(1440));
-        avg10Line.setLineTime(timeChartData.getTimeChartPriceTime());
+        avg10Line.setLineData(timeChartData.getTimeChartAVG(1440, timeList));
+        avg10Line.setLineTime(timeList);
         avg10Line.setLineColor(getResources().getColor(R.color.chart_avg_line_color));
 
         lines.add(avg10Line);
@@ -131,7 +135,7 @@ public class TimeChartView extends FrameLayout implements ITouchEventResponse {
                 timeChartData.getDecimalPlace());
         lineChart.setDecimalNum(Integer.parseInt(timeChartData.getDecimalPlace()));
         lineChart.setLineData(lines);
-        lineChart.setDatasNum((timeChartData.getHistoryDatas().size()));
+        lineChart.setDatasNum(timeChartData.getHistoryDatas().size());
 
         if (textView != null) {
             setTimeText(this.textView);