【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“DrawPictureWithJfreechart.java”一文,供大家参考学习
package mrgf;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.axis.CategoryAxis3D;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.entity.*;
import org.jfree.data.category.DefaultCategoryDataset;
public class DrawPictureWithJfreechart {
int chartWidth = 500; //图片宽度
int chartHeight = 375; //图片高度
String chartTitle = "JFreeChart生成柱状图实例"; //图表标题
String bookTitle[] = {"A", "B", "C", "D", "E"}; //图例标题
String category[] = {"春", "夏", "秋", "冬"}; //类别
String xTitle = "销售时间:2006"; //x轴标题
String yTitle = "销售数量"; //y轴标题
double[][] datas = null; //绘图数据
public DrawPictureWithJfreechart() {
}
public DrawPictureWithJfreechart(int chartWidth, int chartHeight,
String chartTitle, String[] bookTitle,
String[] category, String xTitle,
String yTitle, double[][] datas) {
this.chartWidth = chartWidth;
this.chartHeight = chartHeight;
this.chartTitle = chartTitle;
this.bookTitle = bookTitle;
this.category = category;
this.xTitle = xTitle;
this.yTitle = yTitle;
this.datas = new double[bookTitle.length][category.length];
this.datas = datas;
}
public String drawHistogram(HttpServletRequest request, HttpSession session) {
// 创建默认数据集
if (datas == null) {
datas = new double[bookTitle.length][category.length];
for (int m = 0; m < bookTitle.length; m++) {
for (int n = 0; n < category.length; n++) {
datas[m][n] = 1 + (int) (Math.random() * 100);
}
}
}
// 往数据集中添加绘制图表所需的数据
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int m = 0; m < bookTitle.length; m++) {
for (int n = 0; n < category.length; n++) {
dataset.addValue(datas[m][n], bookTitle[m], category[n]);
}
}
// 创建3D效果图
JFreeChart chart = ChartFactory.createBarChart3D
(
chartTitle, // 图表标题
xTitle, // 坐标标题
yTitle, // 坐标标题
dataset, // 绘制数据
PlotOrientation.VERTICAL, // 直方图的方向,竖向
// PlotOrientation.HORIZONTAL, // 直方图的方向,横向
true, // 定义图表是否包含图例
true, // 定义图表是否包含提示
false // 定义图表是否包含URL
);
// 定义图框颜色
chart.setBackgroundPaint(new Color(168, 219, 219));
// 获得图表对象引用,自行设置绘制属性
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(new Color(219, 219, 127)); // 设置绘图区域背景色
plot.setDomainGridlinePaint(Color.BLACK); // 设置垂直方向标准线的颜色
plot.setDomainGridlinesVisible(false); // 设置垂直方向标准线是否显示,false为默认值
plot.setRangeGridlinePaint(Color.RED); // 设置水平方向标准线的颜色
plot.setRangeGridlinesVisible(true); // 设置水平方向标准线是否显示,true为默认值
// 设置是否显示柱的轮廓,true为默认值
BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer(); //p404z
renderer.setDrawBarOutline(true);
// 自定义柱的颜色,并定义渐变
Color color[] = new Color[bookTitle.length];
color[0] = new Color(28, 255, 66);
color[1] = new Color(28, 6, 255);
for (int i = 0; i < color.length; i++) {
GradientPaint gradient = new GradientPaint(0, 0, color[i].brighter(),
0, chartHeight, color[i].darker());
renderer.setSeriesPaint(i, gradient);
}
// 设置横轴标题文字的旋转方向
CategoryAxis3D domainAxis = (CategoryAxis3D) plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(
CategoryLabelPositions.createDownRotationLabelPositions(Math.PI / // 文字顺时针旋转
// CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / // 文字逆时针旋转
16.0) // 文字旋转弧度,接受双精度参数
);
// 结束自定义图表绘制的相关属性
ChartRenderingInfo info =
new ChartRenderingInfo(new StandardEntityCollection());
// 生成指定格式的图表并获得文件名
String fileName = null;
try {
fileName =
ServletUtilities.saveChartAsPNG(chart, chartWidth,
chartHeight, info,
session);
} catch (IOException ex) {
}
// 设置图表浏览路径
String graphURL =
request.getContextPath() + "/servlet/DisplayChart?filename=" +
fileName;
return graphURL;
}
// 绘图属性的赋值方法
public void setChartWidth(int chartWidth) {
this.chartWidth = chartWidth;
}
public void setChartHeight(int chartHeight) {
this.chartHeight = chartHeight;
}
public void setChartTitle(String chartTitle) {
this.chartTitle = chartTitle;
}
public void setBookTitle(String[] bookTitle) {
this.bookTitle = bookTitle;
}
public void setCategory(String[] category) {
this.category = category;
}
public void setXTitle(String xTitle) {
this.xTitle = xTitle;
}
public void setYTitle(String yTitle) {
this.yTitle = yTitle;
}
public void setDatas(double[][] datas) {
this.datas = datas;
}
}
上一篇:
DrawPanelTest.java
下一篇:
让我掉下眼泪的