最近项目用到java作图,首先想到的是利用开源插件jfreechart,文档收费,网上资料参差不齐,最后还是觉得采用java2D bufferedImage作图。 现在分享一下。
把一张图片分为border部分和主体部分,如下图所示:
定义一个边界类
/**
* Copyright (c) 2014, alax
* All Rights Reserved.
*/
package chapterOne.axis;
/**
*
* @author alax_app@yeah.net
* @Date 2014-3-18 下午11:41:10
*
*/
public class AxisBorder {
private int left;
private int right;
private int top;
private int bottom;
private int backgroundColor;//背景色
public AxisBorder(){
}
public AxisBorder(int left, int right, int top, int bottom,
int backgroundColor) {
super();
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
this.backgroundColor = backgroundColor;
}
public int getLeft() {
return left;
}
public void setLeft(int left) {
this.left = left;
}
public int getRight() {
return right;
}
public void setRight(int right) {
this.right = right;
}
public int getTop() {
return top;
}
public void setTop(int top) {
this.top = top;
}
public int getBottom() {
return bottom;
}
public void setBottom(int bottom) {
this.bottom = bottom;
}
public int getBackgroundColor() {
return backgroundColor;
}
public void setBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
}
}
接下来定义坐标轴实例
/**
* Copyright (c) 2014, alax
* All Rights Reserved.
*/
package chapterOne.axis;
import java.util.Date;
/**
*
* @author alax_app@yeah.net
* @Date 2014-3-19 上午12:10:01
*
*/
public class Axis {
public static final String VALUE = "value";
public static final String DATETIME = "datetime";
private int lineColor;//y轴颜色
private int lineWidth;//y轴宽度
private String title;//y轴标题
private String type;//y轴类型 支持value 类型 和datetime类型
private double max;//最大值
private double min;//最小值
private int ticks;//刻度个数
private Date startDate;
private Date endDate;
private String labelFormat; //刻度值排版
public Axis(){
}
public Axis(int lineColor, int lineWidth, String title, String type,
double max, double min, int ticks, Date startDate, Date endDate,
String labelFormat) {
super();
this.lineColor = lineColor;
this.lineWidth = lineWidth;
this.title = title;
this.type = type;
this.ma