1. jfreechar简介
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。
2. JfreeChart的下载及环境搭建
第一步:下载网址为:http://www.jfree.org/jfreechart/download
下载的版本为:jfreechart-1.0.19
第二步:导入的jar包地址:jfreechart-1.0.19/lib如图所示
第三步:在web.xml中配置jfreechart
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
其中DisplayChart是用来显示图表的类
1. JfreeChat图表数据
Jfreechart组件可以生成生成各种各样的图形报表,如区域图、饼状图、折线图、时序图等,常用的柱形图。
1. 创建图表的操作步骤
使用 JFreeChart 创建图表共有三个步骤。如下:
创建一个 dataset。该 dataset 包含图表要显示的数据。
创建一个 JFreeChart 对象。该对象负责画这个图表。
创建一个输出目标(如:一个 panel,显示在屏幕上)。该输出目标画这个图表。
2. 利用JfreeChart创建垂直柱状报表
package com.xp.chartbar;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PieLabelLinkStyle;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;
public class BarChart1 {
private static Font FONT = new Font("宋体", Font.PLAIN, 12);
public static Color[] CHART_COLORS = {new Color(31, 129,188),
new Color(241, 92, 128),new Color(124, 181,236),
new Color(102, 172, 204),new Color(102, 102,0),
new Color(204, 153, 102),new Color(0, 153,255),
new Color(204, 255, 255),new Color(51, 153,153),
new Color(255, 204, 102),new Color(102, 102,0),
new Color(204, 204, 204),new Color(204, 255,255),
new Color(255, 204, 204),new Color(255, 255,204),
new Color(255, 153, 204),new Color(51, 0,0),
new Color(0, 51, 102),new Color(0, 153,102),
new Color(153, 102, 153),new Color(102, 153,204),
new Color(153, 204, 153),new Color(204, 204,153),
new Color(255, 255, 153),new Color(255, 204,153),
new Color(255, 153, 204),new Color(204, 153,153),
new Color(204, 204, 255),new Color(204, 255,204),
new Color(153, 204, 153),new Color(255, 204,102)};//颜色
public static void setChartTheme()
{
// 设置中文主题样式解决乱码
StandardChartTheme chartTheme =newStandardChartTheme("CN");
// 设置标题字体
chartTheme.setExtraLargeFont(FONT);
// 设置图例的字体
chartTheme.setRegularFont(FONT);
// 设置轴向的字体
chartTheme.setLargeFont(FONT);
chartTheme.setSmallFont(FONT);
chartTheme.setTitlePaint(new Color(51, 51,51));
chartTheme.setSubtitlePaint(new Color(85, 85,85));
chartTheme.setLegendBackgroundPaint(Color.WHITE);//设置标注
chartTheme.setLegendItemPaint(Color.BLACK);//
chartTheme.setChartBackgroundPaint(Color.WHITE);
// 绘制颜色绘制颜色.轮廓供应商
//paintSequence,outlinePaintSequence,strokeSequence,outlineStrokeSequence,shapeSequence
Paint[] OUTLINE_PAINT_SEQUENCE =new Paint[]{Color.WHITE};
// 绘制器颜色源
DefaultDrawingSupplier drawingSupplier =newDefaultDrawingSupplier(
CHART_COLORS,CHART_COLORS, OUTLINE_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE);
chartTheme.setDrawingSupplier(drawingSupplier);
chartTheme.setPlotBackgroundPaint(Color.WHITE);//绘制区域
chartTheme.setPlotOutlinePaint(Color.WHITE);//绘制区域外边框
chartTheme.setLabelLinkPaint(new Color(8, 55,114));//链接标签颜色
chartTheme.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
chartTheme.setAxisOffset(new RectangleInsets(5,12, 5, 12));
chartTheme.setDomainGridlinePaint(new Color(192, 208,224));// X坐标轴垂直网格颜色
chartTheme.setRangeGridlinePaint(new Color(192, 192,192));// Y坐标轴水平网格颜色
chartTheme.setBaselinePaint(Color.WHITE);
chartTheme.setCrosshairPaint(Color.BLUE);//不确定含义
chartTheme.setAxisLabelPaint(new Color(51, 51,51));//坐标轴标题文字颜色
chartTheme.setTickLabelPaint(new Color(67, 67,72));//刻度数字
chartTheme.setBarPainter(new StandardBarPainter());//设置柱状图渲染
chartTheme.setXYBarPainter(newStandardXYBarPainter());// XYBar渲染
chartTheme.setItemLabelPaint(Color.black);
chartTheme.setThermometerPaint(Color.white);//温度计
ChartFactory.setChartTheme(chartTheme);
}
public static StringgenBarChart(HttpSessionsession) throws Exception {
setChartTheme();
DefaultCategoryDataset dataset =new DefaultCategoryDataset();
dataset.addValue(510, "深圳","苹果");
dataset.addValue(320, "深圳","香蕉");
dataset.addValue(580, "深圳","橘子");
dataset.addValue(390, "深圳","梨子");
JFreeChart chart=ChartFactory.createBarChart3D("水果销售统计图","水果","销售",dataset,
PlotOrientation.VERTICAL,true, true,true);
String fileName=ServletUtilities.saveChartAsPNG(chart, 700, 500,null,session);
returnfileName;
}
}
BarChart1.jsp
<%@page import="com.xp.chartbar.BarChart1"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>柱状图演示</title>
</head>
<body>
<%
String fileName=BarChart1.genBarChart(session);
System.out.println(fileName);
%>
<img alt="柱状图" src="DisplayChart?filename=<%=fileName%>" width="700" height="500" border="0"/>
</body>
</html>
7.利用JfreeChart创建横向柱状报表
package com.xp.chartbar;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChart2 {
public static String genBarChart(HttpSession session) throws Exception {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(510, "深圳", "苹果");
dataset.addValue(320, "深圳", "香蕉");
dataset.addValue(580, "深圳", "橘子");
dataset.addValue(390, "深圳", "梨子");
JFreeChart chart=ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset,
PlotOrientation.HORIZONTAL, true, true, true);
String fileName=ServletUtilities.saveChartAsPNG(chart, 700, 500, null,session);
return fileName;
}
}
<%@page import="com.xp.chartbar.BarChart2"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
String filename=BarChart2.genBarChart(session);
System.out.println(filename);
%>
<img src="DisplayChart?filename=<%=filename %>" width="700" height="500" border="0"/>
</body>
</html>