package smart.jrsoft.chart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import javax.servlet.http.*;
/**
* @author oracle
*/
public class DrawDotLine {
private static DrawDotLine instance = null; //画点线图的实例
private String title="点线图实例"; //点线图标题
private String XTitle="横坐标标题"; //点线图横坐标题目
private String YTitle="纵坐标标题"; //点线图纵坐标题目
private int width=800; //图片宽度
private int height=450; //图片高度
private String fileName="d:\\temp.jpg"; //暂时保存的图片名称
private boolean isV=true; //是否垂直线显示
private Color bgColor=Color.WHITE; //图像背景颜色
private Color foreColor=Color.lightGray; //会点线图区域背景颜色
private Color spaceLineColor=Color.white; //间隔线颜色
FileOutputStream fosJpg = null; //生成点线图的输出流
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
/**
* 获取该类单一对象
* @return 该类事例
*/
public static synchronized DrawDotLine getInstance() {
if (instance==null)
instance=new DrawDotLine();
return instance;
}
/**
* 设置图像标题名称
* @param title 图像标题名称
*/
public void setTitle(String title){
this.title=title;
}
/**
* 设置图像的水平坐标名称
* @param title 水平坐标名称
*/
public void setXTitle(String title){
this.XTitle=title;
}
/**
* 设置图像的垂直坐标的名称
* @param title 图像的垂直坐标的名称
*/
public void setYTitle(String title){
this.YTitle=title;
}
/**
* 设置图像的宽度
* @param width 图像的宽度
*/
public void setWidth(int width){
this.width=width;
}
/**
* 设置图像的宽度
* @param width 图像的宽度
*/
public void setHeight(int height){
this.height=height;
}
/**
* 设置保存文件名称
* @param fileName 保存文件名称
*/
private void setFileName(String fileName){
this.fileName=fileName;
}
/**
* 设置绘水平图还是垂直图
* @param isV 水平图还是垂直图
*/
public void setIsV(boolean isV){
this.isV=isV;
}
/**
* 设置背景颜色
* @param color 背景颜色
*/
public void setBgColor(Color color){
this.bgColor=color;
}
/**
* 设置背景颜色
* @param red red色素值
* @param green green色素值
* @param blue blue色素值
*/
public void setBgColor(int red,int green,int blue){
this.bgColor=new Color(red,green,blue);
}
/**
* 改变背景颜色
* @param str 背景颜色描述 比如:BLACK black blue Blue 等
*/
public void setBgcolor(String str){
this.bgColor=ChangeColor.getColor(str);
}
/**
* 设置绘点线图区域的背景颜色
* @param color 绘点线图区域的背景颜色
*/
public void setForeColor(Color color){
this.foreColor=color;
}
/**
* 设置绘点线图区域的背景颜色
* @param red red色素值
* @param green green色素值
* @param blue blue色素值
*/
public void setForeColor(int red,int green,int blue){
this.foreColor=new Color(red,green,blue);
}
/**
* 设置绘点线图区域的背景颜色
* @param str 绘点线图区域的背景颜色 比如:BLACK black blue Blue 等
*/
public void setForeColor(String str){
this.foreColor=ChangeColor.getColor(str);
}
/**
* 设置间隔线的颜色
* @param red red色素值
* @param green green色素值
* @param blue blue色素值
*/
public void setSpaceLineColor(int red,int green,int blue){
this.spaceLineColor=new Color(red,green,blue);
}
/**
* 设置间隔线的颜色
* @param color 间隔线的颜色
*/
public void setSpaceLineColor(Color color){
this.spaceLineColor=color;
}
/**
* 设置间隔线的颜色
* @param str 间隔线的颜色 比如:BLACK black blue Blue 等
*/
public void setSpaceLineColor(String str){
this.spaceLineColor=ChangeColor.getColor(str);
}
/**
*初始化相关参数
*/
public void init(){
setTitle("点线图实例");
setXTitle("横坐标标题");
setYTitle("纵坐标标题");
setWidth(800);
setHeight(450);
setIsV(true);
setBgColor(255,255,255);
setFileName("d:\\temp.jpg");
setForeColor(Color.lightGray);
setSpaceLineColor(Color.white);
}
/**
*恢复到初始化状态
*/
public void reset(){
init();
}
/**
* 添加要绘制点线图的数据
* @param typeName 数据类型名称
* @param value 数据值
* @param groupName 数据组
*/
public void addValue(String typeName,double value,String groupName){
defaultcategorydataset.addValue(value,groupName,typeName);
}
/**
* 生成点线图像
* @param fileName 保存文件名称 文件名称为(使用路径为): d:\\web\test.jpg
* @return 成功:true 失败:false
*/
public boolean saveAbs(String fileName) {
JFreeChart jfreechart = null;
if (fileName != null) {
this.setFileName(fileName);
}
if (isV == true) {
jfreechart = ChartFactory.createLineChart(this.title, this.XTitle,
this.YTitle,
defaultcategorydataset,
PlotOrientation.VERTICAL, true, true, false);
}
else {
jfreechart = ChartFactory.createLineChart(this.title, this.XTitle,
this.YTitle,
defaultcategorydataset,
PlotOrientation.HORIZONTAL, true, true, false);
}
jfreechart.setBackgroundPaint(this.bgColor);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(this.foreColor);
categoryplot.setRangeGridlinePaint(this.spaceLineColor);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)
categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true);
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}
, 0.0F));
lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
6F, 6F
}
, 0.0F));
lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
2.0F, 6F
}
, 0.0F));
FileOutputStream fosJpg = null;
try {
fosJpg = new FileOutputStream(fileName);
ChartUtilities.writeChartAsJPEG(fosJpg, 100, jfreechart, this.width, this.height, null);
}
catch (Exception e) {
return false;
}
finally {
this.defaultcategorydataset.clear();
try {
fosJpg.close();
}
catch (Exception e) {}
}
return true;
}
/**
* 保存生成的点线图
* @param request 在jsp页面中的request对象 用于获取文件路径
* @param fileName 保存文件名称 文件名称必须使用站点的绝对路径 如 : “/admin/test.jpg"
* @return true 成功 false 失败
*/
public boolean saveWebFile(HttpServletRequest request, String fileName) {
JFreeChart jfreechart = null;
if (fileName != null) {
this.setFileName(fileName);
}
fileName = request.getRealPath("") + fileName;
if (isV == true) {
jfreechart = ChartFactory.createLineChart(this.title, this.XTitle,
this.YTitle,
defaultcategorydataset,
PlotOrientation.VERTICAL, true, true, false);
}
else {
jfreechart = ChartFactory.createLineChart(this.title, this.XTitle,
this.YTitle,
defaultcategorydataset,
PlotOrientation.HORIZONTAL, true, true, false);
}
jfreechart.setBackgroundPaint(this.bgColor);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(this.foreColor);
categoryplot.setRangeGridlinePaint(this.spaceLineColor);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)
categoryplot.getRenderer();
lineandshaperenderer.setShapesVisible(true);
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
10F, 6F
}
, 0.0F));
lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
6F, 6F
}
, 0.0F));
lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] {
2.0F, 6F
}
, 0.0F));
FileOutputStream fosJpg = null;
try {
fosJpg = new FileOutputStream(fileName);
ChartUtilities.writeChartAsJPEG(fosJpg, 100, jfreechart, this.width, this.height, null);
}
catch (Exception e) {
return false;
}
finally {
this.defaultcategorydataset.clear();
try {
fosJpg.close();
}
catch (Exception e) {}
}
return true;
}
public static void main(String[] args) {
//1.生成绘点线图的对象
DrawDotLine drawDotLine =new DrawDotLine();
//2.添加绘图的数据
drawDotLine.addValue("s1",2.0,"group1");
drawDotLine.addValue("s2",3.0,"group1");
drawDotLine.addValue("s3",3.0,"group1");
drawDotLine.addValue("s4",2.0,"group1");
drawDotLine.addValue("s5",1.0,"group1");
drawDotLine.addValue("s6",4.0,"group1");
drawDotLine.addValue("s7",3.0,"group1");
drawDotLine.addValue("s8",2.0,"group1");
drawDotLine.addValue("s9",5.0,"group1");
drawDotLine.addValue("s10",1.0,"group1");
drawDotLine.addValue("s1",3.0,"group2");
drawDotLine.addValue("s2",1.0,"group2");
drawDotLine.addValue("s3",5.0,"group2");
drawDotLine.addValue("s4",1.0,"group2");
drawDotLine.addValue("s5",3.0,"group2");
drawDotLine.addValue("s6",2.0,"group2");
drawDotLine.addValue("s7",5.0,"group2");
drawDotLine.addValue("s8",2.0,"group2");
drawDotLine.addValue("s9",1.0,"group2");
drawDotLine.addValue("s10",4.0,"group2");
//3.设置图片属性
drawDotLine.init();
drawDotLine.setTitle("点线图演示");
drawDotLine.setXTitle("点线图横坐标标题");
drawDotLine.setYTitle("点线图纵坐标标题");
drawDotLine.setBgColor(Color.white);
drawDotLine.setForeColor(Color.lightGray);
drawDotLine.setSpaceLineColor(Color.white);
drawDotLine.setWidth(800);
drawDotLine.setHeight(450);
//4.保存图片
drawDotLine.saveAbs("e:\\DrawDotLine.jpg");
}
}
最近下载更多
最近浏览更多
3334004690 LV11
2024年6月22日
fesfefe LV13
2023年11月1日
dsadasdwf LV12
2023年7月7日
lironggang LV38
2023年3月20日
东北人 LV12
2022年9月20日
杠上炮 LV6
2022年6月28日
微信网友_5957378031800320 LV3
2022年5月18日
小爷葛优躺 LV2
2021年6月22日
Ciaoss LV1
2021年6月7日
andy xiao2222222 LV9
2021年6月2日

