派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.qxueyou.scc.school.util;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Map;
 
 
/**
 * 学习报告学习时间分布图生成类utils
 * @author xiadehu
 *
 */
public class StudyReportUtils {
 
    /**
     * 
     * @param width 图片宽度
     * @param height 图片高度
     * @param startHour1 开始小时1
     * @param text1 显示文本1
     * @param startHour2 开始小时2
     * @param text2 显示文本2
     * @return BufferedImage 对象
     */
    public static BufferedImage drawPng(int width,int height,List<Map<String,Object>> times){
        
        //初始化画布
        BufferedImage image = new BufferedImage(width, height,     BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();
        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();
         
        //背景
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(new Color(248,197,116));
        double centerX = width/2;
        double centerY = height/2;
        double radius = width/2;
        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY+ radius);
        g2d.draw(circle);
        g2d.fill(circle);
        
        //绘制时间文字
        for(Map<String,Object> time:times){
            drawStudyTimeArc(width, height, (Integer)time.get("timeFrom"),(Integer)time.get("timeTo"), g2d,new Color(255,255,255));
        }
        
        for(Map<String,Object> time:times){
            drawStudyTimeText(width, height, (Integer)time.get("timeFrom"), (String)time.get("name")+(String)time.get("percent"), g2d,new Color(255,0,0));
        }
        
        g2d.setColor(new Color(255,255,255));
        circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerX, centerY, centerX + 6, centerY+ 6);
        g2d.draw(circle);
        g2d.setColor(new Color(248,197,116));
        circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerX, centerY, centerX + 5, centerY+ 5);
        g2d.fill(circle);
        
        //释放
        g2d.dispose();
         
        return image;
    }
 
    private static void drawStudyTimeArc(int width, int height, int startHour,int endHour,
             Graphics2D g2d,Color backGroundColor) {
        
        g2d.setColor(backGroundColor);
        g2d.drawArc(0, 0, width, height, -15*(startHour-5), 15*((24+endHour-startHour)%24));
        g2d.fillArc(0, 0, width, height, -15*(startHour-5), 15*((24+endHour-startHour)%24));
        
    }
    
    private static void drawStudyTimeText(int width, int height, int startHour1,
            String text1, Graphics2D g2d,Color textColor) {
        
        double sin = Math.sin(adjustStartIndex(startHour1)*15*Math.PI/180);
        double cos = Math.cos(adjustStartIndex(startHour1)*15*Math.PI/180);
        
        g2d.setColor(textColor);
        float x1 = (float)(adjustRadius(startHour1,width)/2.0d*sin);
        float x = x1+width/2;
        float y1=(float)(adjustRadius(startHour1,width)/2.0d*cos);
        float y = height/2-y1;
        g2d.setFont(new Font("宋体", Font.PLAIN, 12));
        g2d.drawString(text1, x, y);
    }
    
    private static float adjustStartIndex(int startHour){
        if(startHour<=3){
            return startHour;
        }
        if(startHour==5){
            return startHour+0.6f;
        }
        if(startHour==6||startHour==7){
            return startHour+0.9f;
        }
        if(startHour>=8&&startHour<=14){
            return startHour+1f;
        }
        if(startHour==15){
            return startHour+0.5f;
        }
        if(startHour==18){
            return startHour+0.3f;
        }
        if(startHour>=19&&startHour<=23){
            return startHour+0.2f;
        }
        return startHour+0.5f;
    }
    
    private static float adjustRadius(int startHour,int width){
        
        switch(startHour){
         case 2:return width-70;
         case 3:return width-70;
         case 4:return width-100;
         case 5:return width-100;
         case 6:return width-100;
         case 7:return width-100;
         case 8:return width-80;
         case 9:return width-70;
         case 11:return width-30;
         case 12:return width-70;
         case 13:return width-30;
         case 14:return width-10;
         case 15:return width-10;
         default :return width-50;
        }
    }
    
    public static void main(String[] args){
    }
}