Bladeren bron

content-service/exam 返回前端 echart 库的 line chart 数据

reghao 7 maanden geleden
bovenliggende
commit
574cfa07b9

+ 15 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/exam/controller/ExamStatisticController.java

@@ -1,6 +1,8 @@
 package cn.reghao.tnb.content.app.exam.controller;
 
 import cn.reghao.jutil.web.WebResult;
+import cn.reghao.tnb.content.app.exam.model.chart.LineChartData;
+import cn.reghao.tnb.content.app.exam.service.ChartService;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
 import org.springframework.http.MediaType;
@@ -19,6 +21,12 @@ import java.util.List;
 @RestController
 @RequestMapping("/api/content/exam/statistic")
 public class ExamStatisticController {
+    private final ChartService chartService;
+
+    public ExamStatisticController(ChartService chartService) {
+        this.chartService = chartService;
+    }
+
     @Operation(summary = "获取用户考试结果的统计数据", description = "N")
     @GetMapping(value = "/count", produces = MediaType.APPLICATION_JSON_VALUE)
     public String getExamCount() {
@@ -36,4 +44,11 @@ public class ExamStatisticController {
         list.add("0.5,1.0,1.0,1.0,0.0,0.0,0.0");
         return WebResult.success(list);
     }
+
+    @Operation(summary = "获取折线图数据", description = "N")
+    @GetMapping(value = "/linechart", produces = MediaType.APPLICATION_JSON_VALUE)
+    public String getLineChart() {
+        LineChartData lineChartData = chartService.getLineChartData();
+        return WebResult.success(lineChartData);
+    }
 }

+ 28 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/exam/model/chart/LineChartData.java

@@ -0,0 +1,28 @@
+package cn.reghao.tnb.content.app.exam.model.chart;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @author reghao
+ * @date 2025-08-14 09:28:59
+ */
+@Setter
+@Getter
+public class LineChartData {
+    private String xName;
+    private String[] xLabel = {};
+    private Integer[] xValue = {};
+    private String yName;
+    private int yMin;
+    private int yMax;
+    private int ySplitNumber;
+
+    public LineChartData(String yName, int yMin, int yMax, int ySplitNumber, String xName) {
+        this.xName = xName;
+        this.yName = yName;
+        this.yMin = yMin;
+        this.yMax = yMax;
+        this.ySplitNumber = ySplitNumber;
+    }
+}

+ 79 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/exam/service/ChartService.java

@@ -0,0 +1,79 @@
+package cn.reghao.tnb.content.app.exam.service;
+
+import cn.reghao.jutil.jdk.text.TextFile;
+import cn.reghao.tnb.content.app.exam.model.chart.LineChartData;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * @author reghao
+ * @date 2025-08-14 09:31:22
+ */
+@Service
+public class ChartService {
+    private final TextFile textFile = new TextFile();
+
+    public LineChartData getLineChartData() {
+        String filePath = "/home/reghao/Downloads/2010四川高考一分一段.txt";
+        List<String> lines = textFile.read(filePath);
+        Map<Integer, Integer> map = new TreeMap<>();
+        for (String line : lines) {
+            String[] arr = line.split("\\s+");
+            int score = Integer.parseInt(arr[0].replace("分", ""));
+            int num = Integer.parseInt(arr[1]);
+            int total = Integer.parseInt(arr[2]);
+            map.put(score, num);
+        }
+
+        int level1 = 512;
+        int level2 = 441;
+        int level3 = 413;
+        int level4 = 361;
+        int level0 = level1+100;
+        int level5 = level4-50;
+
+        Map<String, Integer> map1 = new TreeMap<>();
+        int num0 = getNum(map, level0, 750);
+        map1.put("level0", num0);
+
+        int num1 = getNum(map, level1, level0);
+        map1.put("level1", num1);
+
+        int num2 = getNum(map, level2, level1);
+        map1.put("level2", num2);
+
+        int num3 = getNum(map, level3, level2);
+        map1.put("level3", num3);
+
+        int num4 = getNum(map, level4, level3);
+        map1.put("level4", num4);
+
+        int num5 = getNum(map, level5, level4);
+        map1.put("level5", num5);
+
+        int num6 = getNum(map, 1, level5);
+        map1.put("level6", num6);
+
+        int max = map1.values().stream().mapToInt(i -> i).max().orElse(-1);
+        LineChartData chartData = new LineChartData("人数", 0, max, 10, "分数段");
+        chartData.setXLabel(map1.keySet().toArray(new String[0]));
+        chartData.setXValue(map1.values().toArray(new Integer[0]));
+        return chartData;
+    }
+
+    private int getNum(Map<Integer, Integer> map, int start, int end) {
+        int num = 0;
+        for (int i = start; i < end; i++) {
+            if (map.get(i) != null) {
+                num += map.get(i);
+            } else {
+                System.out.println(i + " get null");
+            }
+        }
+
+        return num;
+    }
+}