📄 linechart.java
字号:
package line;
import java.awt.*;
import java.awt.image.*;
import ajax.DataExchange;
public class LineChart{
private String[] title = {"JAVA",".NET","C","PB","PASCAL"};
private Color[] color = {Color.red,Color.green,Color.yellow,Color.blue,Color.black};
private int[] sales = new int[6];
private int[] month = new int[6];
//获取销售量
public int[] getData(int month) {
DataExchange de = new DataExchange();
return de.getData(month);
}
public BufferedImage drawLineChart(){
int width = 500, height = 375;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d =image.createGraphics();
//打开反锯齿功能
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width, height);
// 绘制新背景
GradientPaint grayGP = new GradientPaint(0, 0, Color.red, width, height, new Color(200,200,200), true);
g2d.setPaint(grayGP);
g2d.fillRoundRect(5, 5, width-5, height-5, 50, 50);
g2d.setPaint(Color.orange);
g2d.fillRoundRect(2, 2, width-10, height-10, 50, 50);
BasicStroke bs = new BasicStroke(5.0f);
g2d.setStroke(bs);
g2d.setPaint(Color.black);
g2d.drawRoundRect(2, 2, width-10, height-10, 50, 50);
GradientPaint blueGP = new GradientPaint(40, 40, Color.red, 120, 300, Color.WHITE, true);
g2d.setPaint(blueGP);
g2d.fillRect(75, 35, 390, 300); //画图区域
// 绘制图表标题
String chartTitle = "2007年上半年计算机类图书销售统计图";
g2d.setFont(new Font("方正粗宋简体", Font.PLAIN, 20));
int length = g2d.getFontMetrics().stringWidth(chartTitle);
g2d.setColor(Color.BLACK);
g2d.drawString(chartTitle, (width-length)/2, 25);
// 创建虚线笔划
float[] dashes = {3.f};
bs = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, dashes, 0);
g2d.setStroke(bs);
g2d.setFont(new Font("宋体", Font.PLAIN, 12));
String str="";
int stringLength = 0;
for (int i = 1; i <= 6; i++){
// 绘制垂直方向虚线
g2d.drawLine(5+i*70, 35, 5+i*70, 335);
// 绘制横轴上月份的说明文字
str += i + "月";
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 5+i*70-stringLength/2, 348);
str = "";
}
str ="";
int stringHeight=0;
for (int i=0; i < 300; i += 30){
// 绘制水平方向虚线
g2d.drawLine(75, 40+i,455, 40+i);
// 绘制纵轴上销售量的说明文字
str += 100-i/3;
stringHeight = g2d.getFontMetrics().getAscent();
stringLength = g2d.getFontMetrics().stringWidth(str);
g2d.drawString(str, 65-stringLength, 40+i + stringHeight/2);
str = "";
}
// 绘制坐标轴
g2d.setStroke(new BasicStroke(3.0f));
g2d.setColor(new Color(53, 76, 112));
g2d.drawLine(75, 35, 75, 335);
g2d.drawLine(75, 335, 465, 335);
//实现折线的生成
g2d.setFont(new Font("Courier New", Font.PLAIN, 12));
for (int i=0; i < title.length; i++){
// 初始化绘制数据
int Sales = 0;
int[] booksales = new int[5];
for (int j = 0; j < month.length; j++){
booksales = getData(j+1);
Sales = booksales[i+1];
sales[j] = 340-Sales*3;
month[j] = 75+70*j;
}
// 重新设置笔划
g2d.setStroke(new BasicStroke(5.0f));
g2d.setColor(color[i]);
// 绘制月销售量折线
g2d.drawPolyline(month, sales, month.length);
// 绘制图例
g2d.fillRect(70+i*50, 350, 10, 10);
g2d.setColor(Color.BLACK);
g2d.drawString(title[i], 80+i*50, 360);
}
return image;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -