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){
|
}
|
}
|