JFreeChart3D柱状图

/*
* @(#)DrawBar.java
* All rights reserved.
*/
package com.system.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAnchor;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

/**
* Class description goes here
*
* @author Fly Liu
* @date Jan 8, 2010
* @description
*/
public class DrawBar {

private String title = ""; // 图片标题
private String XTitle = ""; // 图片横坐标标题
private String YTitle = ""; // 图片垂直坐标标题
private int Xsz = 10; // X轴标尺字体大小
private int Ysz = 10; // X轴标尺字体大小
private Color bgcolor = null; // 图片背景颜色
private int width = 800; // 要生成的图片的宽度
private int height = 600; // 要生成的图片的高度
private double margin = 0.2; // 每组柱间的间距 0--1之间
private boolean isV = true; // 柱图显示方式:0:垂直 1:水平显示
private String fileName = ""; // 图片名称(可以加路经)
private float diaphaneity = 0.8f ;//柱体透明度
private double poleSpaceBetween = 0 ;//组内柱间距
private Color wallPaintColor = Color.white;//3D墙的颜色
private Color rangeGridline = Color.black;//外围背景颜色
private Color rangeGridInside = Color.white;//网格背景色
private DefaultCategoryDataset dataset = null; // 显示图片需用的数据集
private FileOutputStream fosJpg = null; // 生成图片是用到的输出流
private Font xFont = new Font("宋体", Font.PLAIN, 14);//x轴 坐标 字体 样式
private Font yFont = new Font("宋体", Font.PLAIN, 14);//y轴 坐标 字体 样式
private Font xTitleFont = new Font("黑体",Font.PLAIN,14);//x轴 标题 字体 样式
private Font yTitleFont = new Font("黑体",Font.PLAIN,14);//y轴 标题 字体 样式
private Color xPaint = Color.black;//横坐标字体颜色
private Color yPaint = Color.black;//纵坐标字体颜色
private Color viewNumberColor = Color.black;//图上显示数字的颜色
private Font viewNumberFont = new Font("宋体",Font.PLAIN,10);图上显示数字的颜色
private double xCircumgyratePercent = 0.0d;//x轴坐标旋转角度
private double yTitleCircumgyratePercent = 0.0d;//y轴标题旋转角度
private static DrawBar instance = null;


/**
* 单态模式生成类对象
*
* @return 该类的一个对象
*/
public static synchronized DrawBar getInstance() {
if (instance == null)
instance = new DrawBar();
return instance;
}
/**
* 柱间距
* @param d
*/
public void setPoleSpaceBetween(double d){
this.poleSpaceBetween = d ;
}
/**
* 3D墙的颜色
* @param d
*/
public void setWallPaintColor(Color color){
this.wallPaintColor = color ;
}
/**
* 3D墙的颜色
*
* @param red
* 红色色素值
* @param green
* 绿色色素值
* @param blue
* 兰色色素值
*/
public void setWallPaintColor(int red, int green, int blue){
this.wallPaintColor = new Color(red, green, blue);
}
/**
* 网格内部背景的颜色
* @param d
*/
public void setRangeGridInside(Color color){
this.rangeGridInside = color ;
}
/**
* 网格内部背景的颜色
*
* @param red
* 红色色素值
* @param green
* 绿色色素值
* @param blue
* 兰色色素值
*/
public void setRangeGridInside(int red, int green, int blue){
this.rangeGridInside = new Color(red, green, blue);
}
/**
* 网格背景的颜色
* @param d
*/
public void setRangeGridline(Color color){
this.rangeGridline = color ;
}
/**
* 网格背景的颜色
*
* @param red
* 红色色素值
* @param green
* 绿色色素值
* @param blue
* 兰色色素值
*/
public void setRangeGridline(int red, int green, int blue){
this.rangeGridline = new Color(red, green, blue);
}
/**
* 柱状图透明度
*
* @param f
*/
public void setDiaphaneity(float f){
this.diaphaneity = f ;
}
/**
* 改变 图表标题
*
* @param str
* 图表标题
*/
public void setTitle(String str) {
this.title = str;
}

/**
* 改变 目录轴的显示标签
*
* @param str
* 目录轴的显示标签
*/
public void setXTitle(String str) {
this.XTitle = str;
}

/**
* 改变 数值轴的显示标签
*
* @param str
* 数值轴的显示标签
*/
public void setYTitle(String str) {
this.YTitle = str;
}

/**
* 改变 轴的显示标签
*
* @param i
* 轴的显示标签
*/
public void setXFontSize(int i) {
this.Xsz = i;
}

/**
* 改变 轴的显示标签
*
* @param i
* 轴的显示标签
*/
public void setYFontSize(int i) {
this.Ysz = i;
}
/**
* 设置背景颜色
*
* @param red
* 红色色素值
* @param green
* 绿色色素值
* @param 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(Color color) {
this.bgcolor = color;
}

/**
* 改变背景颜色
*
* @param str
* 背景颜色描述 比如:BLACK black blue Blue 等
*/
public void setBgColor(String str) {
this.bgcolor = ChangeColor.getColor(str);
}

/**
* 改变图片宽度
*
* @param width
* 图片宽度
*/
public void setWidth(int width) {
this.width = width;
}

/**
* 改变图片高度
*
* @param height
* 图片高度
*/
public void setHeight(int height) {
this.height = height;
}

/**
* 设置每组柱间的距离
*
* @param margin
* 间距
*/
public void setMargin(double margin) {
this.margin = margin;
}

/**
* 改变图片显示方式
*
* @param str
* 图片显示方式 垂直显示 水平显示
*/
public void setIsV(boolean str) {
this.isV = str;
}

/**
* 改变文件名称
*
* @param str
* 文件名称
*/
private void setFileName(String str) {
this.fileName = str;
}
/**
* x轴坐标字体
* @param font
*/
public void setXFont(Font font) {
xFont = font;
}
/**
* y轴坐标字体
* @param font
*/
public void setYFont(Font font) {
yFont = font;
}
/**
* x轴标题字体
* @param titleFont
*/
public void setXTitleFont(Font titleFont) {
xTitleFont = titleFont;
}
/**
* x轴坐标 字体 颜色
* @param red
* @param green
* @param blue
*/
public void setXPaint(int red, int green, int blue) {
xPaint = new Color(red, green, blue);
}
/**
* y轴坐标 字体 颜色
* @param red
* @param green
* @param blue
*/
public void setYPaint(int red, int green, int blue) {
yPaint = new Color(red, green, blue);
}
/**
* x轴坐标 字体 颜色
* @param paint
*/
public void setXPaint(Color paint) {
xPaint = paint;
}
/**
* y坐标 字体 颜色
* @param paint
*/
public void setYPaint(Color paint) {
yPaint = paint;
}
/**
* y轴标题 字体 样式
* @param titleFont
*/
public void setYTitleFont(Font titleFont) {
yTitleFont = titleFont;
}
/**
* 图示 显示数字 颜色
* @param red
* @param green
* @param blue
*/
public void setViewNumberColor(int red, int green, int blue) {
this.viewNumberColor = new Color(red, green, blue);
}
/**
* 图示 显示数字 颜色
* @param viewNumberColor
*/
public void setViewNumberColor(Color viewNumberColor) {
this.viewNumberColor = viewNumberColor;
}
/**
* 图示 显示数字 样式
* @param viewNumberFont
*/
public void setViewNumberFont(Font viewNumberFont) {
this.viewNumberFont = viewNumberFont;
}
/**
* x轴坐标旋转百分比
* @param circumgyratePercent
*/
public void setXCircumgyratePercent(double circumgyratePercent) {
xCircumgyratePercent = circumgyratePercent;
}
/**
* y轴标题旋转百分比
* @param titleCircumgyratePercent
*/
public void setYTitleCircumgyratePercent(double titleCircumgyratePercent) {
yTitleCircumgyratePercent = titleCircumgyratePercent;
}
/**
* 初始化参数
*/
public void init() {
setTitle("柱状图");
setXTitle("横标题");
setYTitle("纵标题");
setXFontSize(10);
setYFontSize(10);
setWidth(800);
setHeight(600);
setMargin(0.2);
setIsV(true);
setBgcolor(255, 255, 255);
setFileName("temp.jpg");
}

/**
* 添加要进行画柱状图的数据
*
* @param value
* 单元值
* @param name
* 单元项名称
* @param group
* 该单元项所属的组
*/
public void addData(String name, int value, String group) {
if (dataset != null) {
dataset.addValue(value, group, name);
} else {
dataset = new DefaultCategoryDataset();
dataset.addValue(value, group, name);
}
}


/**
* 使用servlet生成图片
* @param request
* @param servletName /servlet/DisplayChart?filename=
* @return 图片路径
*/
public String getUrl(HttpServletRequest request,String servletName){
if("".equals(servletName)||servletName == null){
servletName = "/servlet/DisplayChart?filename=" ;
}
if (dataset == null) {
return "";
} else {
JFreeChart chart = null;
if (isV == true) {
chart = ChartFactory.createBarChart3D(this.title, this.XTitle,
this.YTitle, dataset, PlotOrientation.VERTICAL, false,
true, false);
// ChartFactory.createBarChart3D( 图表标题, 目录轴的显示标签, 数值轴的显示标签,数据集, 图表方向:水平、垂直, 是否显示图例(对于简单的柱状图必须是false), 是否生成工具, 是否生成URL链接);
} else {
chart = ChartFactory.createBarChart3D(this.title, this.XTitle,
this.YTitle, dataset, PlotOrientation.HORIZONTAL, true,
false, false);
}

CategoryPlot categoryplot = chart.getCategoryPlot();
categoryplot.setRangeGridlinePaint(this.rangeGridline);// 设置网格横线颜色
categoryplot.setForegroundAlpha(this.diaphaneity);// 柱体透明度
//categoryplot.setDomainGridlinesVisible(true);//垂直网格线
chart.setBackgroundPaint(this.bgcolor); // 设置外围背景颜色

//categoryplot.setNoDataMessage("当前无数据可供显示!");
//取得横轴
CategoryAxis categoryaxis = categoryplot.getDomainAxis();//取得横轴
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(this.xCircumgyratePercent)); //横坐标旋转
categoryaxis.setLowerMargin(0.01);// 设置标签距离图片左端距离比为2%
categoryaxis.setUpperMargin(0.01);
categoryaxis.setCategoryMargin(this.margin);
categoryaxis.setTickLabelFont(this.xFont); //-设置X轴坐标上的文字
categoryaxis.setLabelFont(this.xTitleFont); //设置X轴的标题文字
categoryaxis.setLabelPaint(this.xPaint);//设置X轴的标题文字颜色
categoryaxis.setTickLabelPaint(this.xPaint);//-设置X轴坐标上的文字颜色

//取得纵轴
NumberAxis verticalAxis = (NumberAxis) categoryplot.getRangeAxis();//取得纵轴
verticalAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
verticalAxis.setVisible(true);
verticalAxis.setAutoRange(true);// 自动设置数据轴数据范围

categoryplot.setBackgroundPaint(this.rangeGridInside);//设置网格背景色
categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);// 底部组标签(靠近x轴)

categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);// 左部标签(y轴)
categoryplot.setDomainGridlinePosition(CategoryAnchor.MIDDLE);// 设置格线位置


ValueAxis rangeAxis = categoryplot.getRangeAxis();
rangeAxis.setUpperMargin(0.15);// 标签距离图片顶端距离
rangeAxis.setLowerMargin(0.15);
rangeAxis.setTickLabelFont(this.yFont);//设置Y轴坐标上的文字
rangeAxis.setLabelFont(this.yTitleFont);//设置Y轴坐标上的标题
rangeAxis.setTickLabelPaint(this.yPaint);//设置Y轴坐标上的文字
rangeAxis.setLabelPaint(this.yPaint);//设置Y轴坐标上的标题
rangeAxis.setLabelAngle(this.yTitleCircumgyratePercent);//纵坐标 旋转
categoryplot.setRangeAxis(rangeAxis);

CustomBarRenderer custombarrenderer = new CustomBarRenderer();
custombarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
custombarrenderer.setBaseItemLabelsVisible(true);
// 按组设置柱体颜色
custombarrenderer.setWallPaint(this.wallPaintColor);// 3d墙的颜色
custombarrenderer.setItemMargin(this.poleSpaceBetween);// 组内柱间距
custombarrenderer.setMaximumBarWidth(0.2); // 設定最大寬度
custombarrenderer.setMinimumBarLength(0.1); // 設定最小長度
// 显示柱体数字
custombarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
custombarrenderer.setBaseItemLabelsVisible(true);
// 默认的数字显示在柱子中,通过如下两句可调整数字的显示
custombarrenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
custombarrenderer.setItemLabelPaint(this.viewNumberColor);//设置柱体数字颜色
custombarrenderer.setItemLabelAnchorOffset(18D);

//custombarrenderer.setItemURLGenerator(new StandardCategoryURLGenerator());//MAP中钻取链接格式
categoryplot.setRenderer(custombarrenderer);

String url = "" ;
try {
String filename = ServletUtilities.saveChartAsPNG(chart, width, height, request.getSession());
url = request.getContextPath() + servletName+ filename ;
} catch (Exception e) {
e.printStackTrace();
} finally {
this.dataset.clear();
}
return url;
}
}
/**
* @return 要显示的文件的名称(包括文件路径)
*/
public String show() {
return fileName;
}

/**
* 恢复成员变量为初试状态
*/
public void reset() {
dataset.clear();
init();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JFreeChart生成柱,需要进行以下步骤: 1. 创建一个数据集对象,用于存储需要绘制的数据 2. 创建一个表对象,并设置标题、x轴和y轴标签 3. 创建一个柱渲染器对象,设置颜色、边框等属性 4. 将数据集对象和渲染器对象关联到表对象中 5. 将表对象绘制到Swing组件中,如JPanel或JFrame中 下面是一个示例代码,可以生成一个简单的柱: ```java import java.awt.Color; import java.awt.Dimension; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.data.category.DefaultCategoryDataset; public class BarChartExample { public static void main(String[] args) { // 生成随机数据 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); Random random = new Random(); for (int i = 0; i < 5; i++) { dataset.addValue(random.nextInt(100), "Series 1", "Category " + i); } // 创建柱对象 JFreeChart chart = ChartFactory.createBarChart( "Bar Chart Example", // 表标题 "Category", // x轴标签 "Value", // y轴标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 表方向 true, // 是否显示例 true, // 是否生成工具提示 false // 是否生成URL链接 ); // 设置柱渲染器 CategoryPlot plot = (CategoryPlot) chart.getPlot(); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setSeriesPaint(0, Color.BLUE); renderer.setDrawBarOutline(false); // 设置x轴标签位置 CategoryAxis xAxis = plot.getDomainAxis(); xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 设置y轴范围 NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); yAxis.setRange(0, 100); // 将表绘制到Swing组件中 JPanel panel = new ChartPanel(chart); panel.setPreferredSize(new Dimension(500, 300)); JFrame frame = new JFrame("Bar Chart Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } ``` 运行以上代码,即可看到生成的柱
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值