2010.03.09——JfreeChart之柱状图

2010.03.09——JfreeChart之柱状图

package pojo;
import java.awt.Color;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;


/**
* 柱状图和折线图
* @author qiujy
*/
public class BarChartTest {
/**
* step1:创建 简单数据集对象
* @return
*/
public static CategoryDataset createDataSet() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(10000, "","Corejava");
dataset.setValue(20000, "","JavaWeb");
dataset.setValue(30000, "","易用struts");
dataset.setValue(40000, "","精通JSF");

return dataset;
}

/**
* 组合数据集对象
* @return
*/
public static CategoryDataset createDataSet2() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(5000, "北京","Corejava");
dataset.setValue(3000, "上海","Corejava");
dataset.setValue(2000, "广州","Corejava");

dataset.setValue(10000, "北京","JavaWeb");
dataset.setValue(6000, "上海","JavaWeb");
dataset.setValue(4000, "广州","JavaWeb");

dataset.setValue(15000, "北京","易用struts");
dataset.setValue(5000, "上海","易用struts");
dataset.setValue(10000, "广州","易用struts");

dataset.setValue(20000, "北京","精通JSF");
dataset.setValue(10000, "上海","精通JSF");
dataset.setValue(10000, "广州","精通JSF");

return dataset;
}

/**
* step2:创建图表
* @param dataset
* @return
*/
public static JFreeChart createChart(CategoryDataset dataset) {
JFreeChart chart = ChartFactory.createBarChart3D( //3D柱状图
//JFreeChart chart = ChartFactory.createLineChart3D( //3D折线图
"原创图书销量统计", //图表的标题
"图书名", //目录轴的显示标签
"销量", //数值轴的显示标签
dataset, //数据集
PlotOrientation.VERTICAL, //图表方式:V垂直;H水平
true, // 是否显示图例
false, // 是否显示工具提示
false // 是否生成URL
);

//===============为了防止中文乱码:必须设置字体
chart.setTitle(new TextTitle("原创图书销量统计", new Font("黑体", Font.ITALIC, 22)));

LegendTitle legend = chart.getLegend(); // 获取图例
legend.setItemFont(new Font("宋体", Font.BOLD, 12)); //设置图例的字体,防止中文乱码

CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
// 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
plot.setBackgroundPaint(new Color(255, 255, 204));
plot.setForegroundAlpha(0.65F); //设置前景色透明度

// 设置横虚线可见
plot.setRangeGridlinesVisible(true);
// 虚线色彩
plot.setRangeGridlinePaint(Color.gray);

CategoryAxis h = plot.getDomainAxis(); //获取x轴
h.setMaximumCategoryLabelWidthRatio(1.0f);// 横轴上的 Lable 是否完整显示
h.setLabelFont(new Font("宋体", Font.BOLD, 12));//设置字体,防止中文乱码
h.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// 轴数值
//h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度倾斜

plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 12)); //Y轴设置字体,防止中文乱码

//柱图的呈现器
BarRenderer3D renderer = new BarRenderer3D();
// 设置柱子宽度
//renderer.setMaximumBarWidth(0.05);
// 设置柱子高度
//renderer.setMinimumBarLength(0.2);
// 设置柱子边框颜色
renderer.setBaseOutlinePaint(Color.BLACK);
// 设置柱子边框可见
renderer.setDrawBarOutline(true);
//设置每个柱的颜色
renderer.setSeriesPaint(0, Color.BLUE);
renderer.setSeriesPaint(1, Color.GREEN);
renderer.setSeriesPaint(2, Color.RED);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.05);
// 显示每个柱的数值,并修改该数值的字体属性
renderer.setIncludeBaseInRange(true);
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
// 设置柱的透明度
plot.setForegroundAlpha(1.0f);
//给柱图添加呈现器
plot.setRenderer(renderer);

// 没有数据的时候显示的内容
plot.setNoDataMessage("找不到可用数据...");

return chart;
}

/**
* step3: 输出图表到Swing Frame
* @param chart
*/
public static void drawToFrame(JFreeChart chart){
//输出图表到Swing Frame
ChartFrame frame = new ChartFrame("原创图书销量统计", chart);
frame.pack();
frame.setVisible(true);
}

/**
* step3: 输出图表到指定的磁盘
* @param destPath
* @param chart
*/
public static void drawToOutputStream(String destPath, JFreeChart chart) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(destPath);
// ChartUtilities.writeChartAsJPEG(
ChartUtilities.writeChartAsPNG(fos, // 指定目标输出流
chart, // 图表对象
600, // 宽
500, // 高
null); // ChartRenderingInfo信息
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* step3: 输出图表到网页
* @param destPath
* @param chart
*/
public static String drawToHtml(JFreeChart chart,HttpSession session,PrintWriter out){
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
String fileName = "";
try
{
fileName = ServletUtilities.saveChartAsPNG(chart, 500, 300, info,
session);//生成图片
// Write the image map to the PrintWriter
ChartUtilities.writeImageMap(out, fileName, info, false);
}
catch (IOException e)
{
e.printStackTrace();
}
out.flush();
return fileName;//返回生成图片的文件名
}

// public static void main(String[] args) throws FileNotFoundException {
// // step1:创建数据集对象
// CategoryDataset dataset = createDataSet2();
//
// // step2:创建柱状图
// JFreeChart chart = createChart(dataset);
//
// // step3: 输出图表到Swing窗口
// drawToFrame(chart);
//
// // step3: 输出图表到磁盘
// //drawToOutputStream("D:\\mybook-bar.png", chart);
//
// }

}



jsp


<%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="pojo.BarChartTest"%>
<%@ page import = "java.io.PrintWriter" %>
<%
BarChartTest chart=new BarChartTest();
String fileName=chart.drawToHtml(chart.createChart(chart.createDataSet2()),session,new PrintWriter(out));
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;
%>
<html>
<head>
<title> JFreeChart使用例子</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<img src="<%= graphURL %>" width="500" height="300" border="0" >
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值