JFreeChart自我总结
1、饼图、柱状图、折线图生成的工具类
1
package
com.text.util;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Font;
6 import java.awt.GradientPaint;
7 import java.io.IOException;
8 import java.text.DecimalFormat;
9 import java.text.NumberFormat;
10 import java.text.SimpleDateFormat;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpSession;
14
15 import org.jfree.chart.ChartFactory;
16 import org.jfree.chart.JFreeChart;
17 import org.jfree.chart.axis.CategoryAxis;
18 import org.jfree.chart.axis.DateAxis;
19 import org.jfree.chart.axis.ValueAxis;
20 import org.jfree.chart.labels.ItemLabelAnchor;
21 import org.jfree.chart.labels.ItemLabelPosition;
22 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
23 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
24 import org.jfree.chart.labels.StandardPieToolTipGenerator;
25 import org.jfree.chart.labels.StandardXYItemLabelGenerator;
26 import org.jfree.chart.labels.StandardXYToolTipGenerator;
27 import org.jfree.chart.plot.CategoryPlot;
28 import org.jfree.chart.plot.IntervalMarker;
29 import org.jfree.chart.plot.PiePlot;
30 import org.jfree.chart.plot.PlotOrientation;
31 import org.jfree.chart.plot.XYPlot;
32 import org.jfree.chart.renderer.category.BarRenderer;
33 import org.jfree.chart.renderer.category.BarRenderer3D;
34 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
35 import org.jfree.chart.servlet.ServletUtilities;
36 import org.jfree.chart.title.LegendTitle;
37 import org.jfree.chart.title.TextTitle;
38 import org.jfree.data.category.CategoryDataset;
39 import org.jfree.data.general.DefaultPieDataset;
40 import org.jfree.data.time.TimeSeriesCollection;
41 import org.jfree.ui.Layer;
42 import org.jfree.ui.TextAnchor;
43 import org.jfree.util.Rotation;
44
45 /**
46 *<br> 文 件 名:JFreeChartServlet.java
47 *<br> 包 名:com.report.servlet
48 *<br> 创 建 人:
49 *<br> 创建日期: Mar 30, 2010 1:00:23 PM
50 *<br> 描 述:生成jFreeChart
51 */
52 public class JFreeChartCreater {
53
54 /**
55 *<br> 方法名称:createPieChart
56 *<br> 功能描述:创建PieChart(饼图)图表
57 *<br> 返 回 值:JFreeChart
58 *<br> 创 建 人:
59 *<br> 创建日期:Mar 30, 2010 12:59:07 PM
60 * @param dataset
61 */
62 public static JFreeChart createPieChart(DefaultPieDataset dataset,
63 String title, boolean is3D) {
64 JFreeChart chart = null ;
65 if (is3D){
66 chart = ChartFactory.createPieChart3D(
67 title, // 图表标题
68 dataset, // 数据集
69 true , // 是否显示图例
70 true , // 是否显示工具提示
71 true // 是否生成URL
72 );
73 }
74 else { chart = ChartFactory.createPieChart(
75 title, // 图表标题
76 dataset, // 数据集
77 true , // 是否显示图例
78 true , // 是否显示工具提示
79 true // 是否生成URL
80 );
81 }
82 // 设置标题字体,为了防止中文乱码:必须设置字体
83 chart.setTitle( new TextTitle(title, new Font( " 黑体 " , Font.ITALIC, 22 )));
84 // 设置图例的字体,为了防止中文乱码:必须设置字体
85 chart.getLegend().setItemFont( new Font( " 黑体 " , Font.BOLD, 12 ));
86 // 获取饼图的Plot对象(实际图表)
87 PiePlot plot = (PiePlot) chart.getPlot();
88 // 图形边框颜色
89 plot.setBaseSectionOutlinePaint(Color.GRAY);
90 // 图形边框粗细
91 plot.setBaseSectionOutlineStroke( new BasicStroke( 0.0f ));
92 // 设置饼状图的绘制方向,可以按顺时针方向绘制,也可以按逆时针方向绘制
93 plot.setDirection(Rotation.ANTICLOCKWISE);
94 // 设置绘制角度(图形旋转角度)
95 plot.setStartAngle( 70 );
96 // 设置突出显示的数据块
97 // plot.setExplodePercent("One", 0.1D);
98 // 设置背景色透明度
99 plot.setBackgroundAlpha( 0.7F );
100 // 设置前景色透明度
101 plot.setForegroundAlpha( 0.65F );
102 // 设置区块标签的字体==为了防止中文乱码:必须设置字体
103 plot.setLabelFont( new Font( " 宋体 " , Font.PLAIN, 12 ));
104 // 扇区分离显示,对3D图不起效
105 if (is3D)
106 plot.setExplodePercent(dataset.getKey( 3 ), 0.1D );
107 // 图例显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
108 plot.setLabelGenerator( new StandardPieSectionLabelGenerator(
109 " {0}:{1}\r\n({2}) " , NumberFormat.getNumberInstance(),
110 new DecimalFormat( " 0.00% " )));
111 // 指定显示的饼图为:圆形(true) 还是椭圆形(false)
112 plot.setCircular( true );
113 // 没有数据的时候显示的内容
114 plot.setNoDataMessage( " 找不到可用数据
"
);
115
116 // 设置鼠标悬停提示
117 plot.setToolTipGenerator( new StandardPieToolTipGenerator());
118 // 设置热点链接
119 // plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));
120
121 return chart;
122 }
123 /**
124 *<br> 方法名称:createPieChart
125 *<br> 功能描述:创建BarChart(柱状图/条形图)图表
126 *<br> 返 回 值:JFreeChart
127 *<br> 创 建 人:
128 *<br> 创建日期:Mar 30, 2010 12:59:07 PM
129 * @param dataset
130 */
131 @SuppressWarnings( " deprecation " )
132 public static JFreeChart createBarChart(CategoryDataset dataset,
133 String title, String x, String y, boolean is3D) {
134 JFreeChart chart = null ;
135 BarRenderer renderer = null ;
136 if (is3D){
137 chart = ChartFactory.createBarChart3D( // 3D柱状图
138 // JFreeChart chart = ChartFactory.createLineChart3D( // 3D折线图
139 title, // 图表的标题
140 x, // 目录轴的显示标签
141 y, // 数值轴的显示标签
142 dataset, // 数据集
143 PlotOrientation.VERTICAL, // 图表方式:V垂直;H水平
144 true , // 是否显示图例
145 false , // 是否显示工具提示
146 false // 是否生成URL
147 );
148 // 柱图的呈现器
149 renderer = new BarRenderer3D();
150 renderer.setItemLabelGenerator( new StandardCategoryItemLabelGenerator());
151 renderer.setItemLabelFont( new Font( " 黑体 " ,Font.PLAIN, 12 ));
152 renderer.setItemLabelsVisible( true );
153 // 3D柱子上不能正常显示数字
154 // 注意:如果数值太大切前面的柱子低于后面的柱子,那么前面的那个数值将被挡住,所以将下面方法中的0该为-值
155 ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(
156 ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
157 TextAnchor.HALF_ASCENT_LEFT, - 1.3D );
158 // 设置不能正常显示的柱子label的position
159 renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
160 renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
161 } else {
162 chart = ChartFactory.createBarChart( // 柱状图
163 // JFreeChart chart = ChartFactory.createLineChart3D( // 3D折线图
164 title, // 图表的标题
165 x, // 目录轴的显示标签
166 y, // 数值轴的显示标签
167 dataset, // 数据集
168 PlotOrientation.VERTICAL, // 图表方式:V垂直;H水平
169 true , // 是否显示图例
170 false , // 是否显示工具提示
171 false // 是否生成URL
172 );
173 // 柱图的呈现器
174 renderer = new BarRenderer();
175 renderer.setIncludeBaseInRange( true ); // 显示每个柱的数值,并修改该数值的字体属性
176 renderer.setBaseItemLabelGenerator( new StandardCategoryItemLabelGenerator());
177 renderer.setBaseItemLabelsVisible( true );
178 }
179
180 // 设置图片背景
181 // chart.setBackgroundPaint(Color.PINK);
182 // 为了防止中文乱码:必须设置字体
183 chart.setTitle( new TextTitle(title, new Font( " 黑体 " , Font.PLAIN, 22 )));
184 LegendTitle legend = chart.getLegend(); // 获取图例
185 legend.setItemFont( new Font( " 宋体 " , Font.BOLD, 12 )); // 设置图例的字体,防止中文乱码
186 CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
187 // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
188 plot.setBackgroundPaint( new Color( 255 , 255 , 204 ));
189 plot.setForegroundAlpha( 0.65F ); // 设置前景色透明度
190 // 设置横虚线可见
191 plot.setRangeGridlinesVisible( true );
192 // 虚线色彩
193 plot.setRangeGridlinePaint(Color.gray);
194
195 ValueAxis rangeAxis = plot.getRangeAxis();
196 // 设置最高的一个Item与图片顶端的距离
197 rangeAxis.setUpperMargin( 0.2 );
198 // 设置最低的一个Item与图片底端的距离
199 rangeAxis.setLowerMargin( 0.3 );
200
201 CategoryAxis domainAxis = plot.getDomainAxis(); // 获取x轴
202 domainAxis.setMaximumCategoryLabelWidthRatio( 1.0f ); // 横轴上的 Lable 是否完整显示
203 domainAxis.setLabelFont( new Font( " 宋体 " , Font.TRUETYPE_FONT, 14 )); // 设置字体,防止中文乱码
204 domainAxis.setTickLabelFont( new Font( " 宋体 " , Font.BOLD, 12 )); // 轴数值
205 // h.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 45度倾斜
206 plot.getRangeAxis().setLabelFont( new Font( " 宋体 " , Font.TRUETYPE_FONT, 14 )); // Y轴设置字体,防止中文乱码
207
208 renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框颜色
209 renderer.setDrawBarOutline( true ); // 设置柱子边框可见
210 renderer.setSeriesPaint( 0 , Color.YELLOW); // 设置每个柱的颜色
211 renderer.setSeriesPaint( 1 , Color.green);
212 renderer.setSeriesPaint( 2 , Color.RED);
213 renderer.setSeriesPaint( 3 , Color.CYAN);
214 renderer.setSeriesPaint( 5 , Color.ORANGE);
215 renderer.setSeriesPaint( 4 , Color.MAGENTA);
216 renderer.setSeriesPaint( 6 , Color.DARK_GRAY);
217 renderer.setSeriesPaint( 7 , Color.PINK);
218 renderer.setSeriesPaint( 8 , Color.black);
219 renderer.setItemMargin( 0.1 ); // 设置每个地区所包含的平行柱的之间距离
220 plot.setRenderer(renderer); // 给柱图添加呈现器
221 plot.setForegroundAlpha( 0.7f ); // 设置柱的透明度
222 // renderer.setMaximumBarWidth(0.2); // 设置柱子宽度
223 // renderer.setMinimumBarLength(0.6); // 设置柱子高度
224 // 设置横坐标显示位置(默认是下方);
225 // plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
226 // 设置纵坐标显示位置(默认是左方)
227 // plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
228 // 没有数据的时候显示的内容
229 plot.setNoDataMessage( " 找不到可用数据
"
);
230
231 return chart;
232 }
233
234 /**
235 *<br> 方法名称:JFreeChartSeriesChart
236 *<br> 功能描述:创建曲线图(折线图)
237 *<br> 注意事项:一般曲线图不用加域所以后4个参数一般为(false,null,0,0)
238 *<br> 参 数:title-标题;subtitleStr-子标题;domain-x轴标志;range-y轴标志;dataset-设置数据;isAreaText-是否在图标中加域;
239 *<br> areaText-域中文字,lowpress-域的最低刻度;uperpress-域的最高刻度
240 *<br> 返 回 值:JFreeChart
241 *<br> 创 建 人:
242 *<br> 创建日期:Mar 30, 2010 1:11:24 PM
243 */
244 @SuppressWarnings( " deprecation " )
245 public static JFreeChart JFreeChartSeriesChart(String title,
246 String subtitleStr, String domain, String range,
247 TimeSeriesCollection dataset, boolean isAreaText,String areaText, double lowpress, double uperpress) { // 时间曲线元素
248 // JFreeChart chart = ChartFactory.createTimeSeriesChart("标题","x轴标志","y轴标志","设置数据",是否显示图形,是否进行提示,是否配置报表存放地址);
249 JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,range, dataset, true , true , false );
250
251 if (subtitleStr != null ){
252 TextTitle subtitle = new TextTitle(subtitleStr, new Font( " 黑体 " ,Font.BOLD, 12 ));
253 chart.addSubtitle(subtitle);
254 }
255 // 设置日期显示格式
256 XYPlot plot = chart.getXYPlot();
257 DateAxis axis = (DateAxis) plot.getDomainAxis();
258 axis.setDateFormatOverride( new SimpleDateFormat( " yyyy-MM-dd " ));
259 // 设置标题的颜色
260 chart.setTitle( new TextTitle(title, new Font( " 黑体 " , Font.ITALIC, 22 )));
261 chart.setBackgroundPaint( new GradientPaint( 0 , 0 , Color.white, 0 , 1000 , Color.blue));
262 plot.setOutlineStroke( new BasicStroke( 1.5f )); // 边框粗细
263 ValueAxis vaxis = plot.getDomainAxis();
264 vaxis.setAxisLineStroke( new BasicStroke( 1.5f )); // 坐标轴粗细
265 vaxis.setAxisLinePaint( new Color( 215 , 215 , 215 )); // 坐标轴颜色
266 vaxis.setLabelPaint( new Color( 10 , 10 , 10 )); // 坐标轴标题颜色
267 vaxis.setTickLabelPaint( new Color( 102 , 102 , 102 )); // 坐标轴标尺值颜色
268 vaxis.setLowerMargin( 0.06d ); // 分类轴下(左)边距
269 vaxis.setUpperMargin( 0.14d ); // 分类轴下(右)边距,防止最后边的一个数据靠近了坐标轴。
270 plot.setNoDataMessage( " 找不到可用数据
"
);
//
没有数据时显示的文字说明。
271 XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
272 // 第一条折线的颜色
273 xylineandshaperenderer.setBaseItemLabelsVisible( true );
274 xylineandshaperenderer.setSeriesFillPaint( 0 , new Color( 127 , 128 , 0 ));
275 xylineandshaperenderer.setSeriesPaint( 0 , new Color( 127 , 128 , 0 ));
276 xylineandshaperenderer.setSeriesShapesVisible( 0 , true );
277 xylineandshaperenderer.setSeriesShapesVisible( 1 , true );
278 xylineandshaperenderer.setSeriesShapesVisible( 2 , true );
279 xylineandshaperenderer.setSeriesShapesVisible( 3 , true );
280 xylineandshaperenderer.setSeriesShapesVisible( 4 , true );
281 // 折线的粗细调
282 StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();
283 xylineandshaperenderer.setToolTipGenerator(xytool);
284 xylineandshaperenderer.setStroke( new BasicStroke( 1.5f ));
285 // 显示节点的值
286 xylineandshaperenderer.setBaseItemLabelsVisible( true );
287 xylineandshaperenderer.setBasePositiveItemLabelPosition( new ItemLabelPosition(
288 ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
289 xylineandshaperenderer.setBaseItemLabelGenerator( new StandardXYItemLabelGenerator());
290 xylineandshaperenderer.setBaseItemLabelPaint( new Color( 102 , 102 , 102 )); // 显示折点数值字体的颜色
291
292 ValueAxis rangeAxis = plot.getRangeAxis();
293 // 设置最高的一个Item与图片顶端的距离
294 rangeAxis.setUpperMargin( 0.2 );
295 // 设置最低的一个Item与图片底端的距离
296 rangeAxis.setLowerMargin( 0.3 );
297 // 在图表中加区域加区域
298 if (isAreaText){
299 lowpress = 62 ;
300 uperpress = 400 ;
301 IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);
302 intermarker.setPaint(Color.decode( " #66FFCC " )); // 域顏色
303 intermarker.setLabelFont( new Font( " SansSerif " , 41 , 14 ));
304 intermarker.setLabelPaint(Color.RED);
305 intermarker.setLabel(areaText);
306 if (dataset != null ) {
307 plot.addRangeMarker(intermarker, Layer.BACKGROUND);
308 }
309 }
310 return chart;
311 }
312
313
314 /**
315 *<br> 方法名称:getGraphURL
316 *<br> 功能描述:取得session中图像的地址;
317 *<br> 返 回 值:String
318 *<br> 创 建 人:
319 *<br> 创建日期:Mar 30, 2010 1:09:51 PM
320 */
321 public static String getGraphURL(JFreeChart chart, HttpSession session,
322 HttpServletRequest request, int width, int height) throws IOException {
323 String filename = ServletUtilities.saveChartAsPNG(chart, width, height, null , session);
324 String graphURL = request.getContextPath() + " /servlet/DisplayChart?filename= " + filename;
325 return graphURL;
326 }
327
328 }
329
330
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Font;
6 import java.awt.GradientPaint;
7 import java.io.IOException;
8 import java.text.DecimalFormat;
9 import java.text.NumberFormat;
10 import java.text.SimpleDateFormat;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpSession;
14
15 import org.jfree.chart.ChartFactory;
16 import org.jfree.chart.JFreeChart;
17 import org.jfree.chart.axis.CategoryAxis;
18 import org.jfree.chart.axis.DateAxis;
19 import org.jfree.chart.axis.ValueAxis;
20 import org.jfree.chart.labels.ItemLabelAnchor;
21 import org.jfree.chart.labels.ItemLabelPosition;
22 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
23 import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
24 import org.jfree.chart.labels.StandardPieToolTipGenerator;
25 import org.jfree.chart.labels.StandardXYItemLabelGenerator;
26 import org.jfree.chart.labels.StandardXYToolTipGenerator;
27 import org.jfree.chart.plot.CategoryPlot;
28 import org.jfree.chart.plot.IntervalMarker;
29 import org.jfree.chart.plot.PiePlot;
30 import org.jfree.chart.plot.PlotOrientation;
31 import org.jfree.chart.plot.XYPlot;
32 import org.jfree.chart.renderer.category.BarRenderer;
33 import org.jfree.chart.renderer.category.BarRenderer3D;
34 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
35 import org.jfree.chart.servlet.ServletUtilities;
36 import org.jfree.chart.title.LegendTitle;
37 import org.jfree.chart.title.TextTitle;
38 import org.jfree.data.category.CategoryDataset;
39 import org.jfree.data.general.DefaultPieDataset;
40 import org.jfree.data.time.TimeSeriesCollection;
41 import org.jfree.ui.Layer;
42 import org.jfree.ui.TextAnchor;
43 import org.jfree.util.Rotation;
44
45 /**
46 *<br> 文 件 名:JFreeChartServlet.java
47 *<br> 包 名:com.report.servlet
48 *<br> 创 建 人:
49 *<br> 创建日期: Mar 30, 2010 1:00:23 PM
50 *<br> 描 述:生成jFreeChart
51 */
52 public class JFreeChartCreater {
53
54 /**
55 *<br> 方法名称:createPieChart
56 *<br> 功能描述:创建PieChart(饼图)图表
57 *<br> 返 回 值:JFreeChart
58 *<br> 创 建 人:
59 *<br> 创建日期:Mar 30, 2010 12:59:07 PM
60 * @param dataset
61 */
62 public static JFreeChart createPieChart(DefaultPieDataset dataset,
63 String title, boolean is3D) {
64 JFreeChart chart = null ;
65 if (is3D){
66 chart = ChartFactory.createPieChart3D(
67 title, // 图表标题
68 dataset, // 数据集
69 true , // 是否显示图例
70 true , // 是否显示工具提示
71 true // 是否生成URL
72 );
73 }
74 else { chart = ChartFactory.createPieChart(
75 title, // 图表标题
76 dataset, // 数据集
77 true , // 是否显示图例
78 true , // 是否显示工具提示
79 true // 是否生成URL
80 );
81 }
82 // 设置标题字体,为了防止中文乱码:必须设置字体
83 chart.setTitle( new TextTitle(title, new Font( " 黑体 " , Font.ITALIC, 22 )));
84 // 设置图例的字体,为了防止中文乱码:必须设置字体
85 chart.getLegend().setItemFont( new Font( " 黑体 " , Font.BOLD, 12 ));
86 // 获取饼图的Plot对象(实际图表)
87 PiePlot plot = (PiePlot) chart.getPlot();
88 // 图形边框颜色
89 plot.setBaseSectionOutlinePaint(Color.GRAY);
90 // 图形边框粗细
91 plot.setBaseSectionOutlineStroke( new BasicStroke( 0.0f ));
92 // 设置饼状图的绘制方向,可以按顺时针方向绘制,也可以按逆时针方向绘制
93 plot.setDirection(Rotation.ANTICLOCKWISE);
94 // 设置绘制角度(图形旋转角度)
95 plot.setStartAngle( 70 );
96 // 设置突出显示的数据块
97 // plot.setExplodePercent("One", 0.1D);
98 // 设置背景色透明度
99 plot.setBackgroundAlpha( 0.7F );
100 // 设置前景色透明度
101 plot.setForegroundAlpha( 0.65F );
102 // 设置区块标签的字体==为了防止中文乱码:必须设置字体
103 plot.setLabelFont( new Font( " 宋体 " , Font.PLAIN, 12 ));
104 // 扇区分离显示,对3D图不起效
105 if (is3D)
106 plot.setExplodePercent(dataset.getKey( 3 ), 0.1D );
107 // 图例显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
108 plot.setLabelGenerator( new StandardPieSectionLabelGenerator(
109 " {0}:{1}\r\n({2}) " , NumberFormat.getNumberInstance(),
110 new DecimalFormat( " 0.00% " )));
111 // 指定显示的饼图为:圆形(true) 还是椭圆形(false)
112 plot.setCircular( true );
113 // 没有数据的时候显示的内容
114 plot.setNoDataMessage( " 找不到可用数据

115
116 // 设置鼠标悬停提示
117 plot.setToolTipGenerator( new StandardPieToolTipGenerator());
118 // 设置热点链接
119 // plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));
120
121 return chart;
122 }
123 /**
124 *<br> 方法名称:createPieChart
125 *<br> 功能描述:创建BarChart(柱状图/条形图)图表
126 *<br> 返 回 值:JFreeChart
127 *<br> 创 建 人:
128 *<br> 创建日期:Mar 30, 2010 12:59:07 PM
129 * @param dataset
130 */
131 @SuppressWarnings( " deprecation " )
132 public static JFreeChart createBarChart(CategoryDataset dataset,
133 String title, String x, String y, boolean is3D) {
134 JFreeChart chart = null ;
135 BarRenderer renderer = null ;
136 if (is3D){
137 chart = ChartFactory.createBarChart3D( // 3D柱状图
138 // JFreeChart chart = ChartFactory.createLineChart3D( // 3D折线图
139 title, // 图表的标题
140 x, // 目录轴的显示标签
141 y, // 数值轴的显示标签
142 dataset, // 数据集
143 PlotOrientation.VERTICAL, // 图表方式:V垂直;H水平
144 true , // 是否显示图例
145 false , // 是否显示工具提示
146 false // 是否生成URL
147 );
148 // 柱图的呈现器
149 renderer = new BarRenderer3D();
150 renderer.setItemLabelGenerator( new StandardCategoryItemLabelGenerator());
151 renderer.setItemLabelFont( new Font( " 黑体 " ,Font.PLAIN, 12 ));
152 renderer.setItemLabelsVisible( true );
153 // 3D柱子上不能正常显示数字
154 // 注意:如果数值太大切前面的柱子低于后面的柱子,那么前面的那个数值将被挡住,所以将下面方法中的0该为-值
155 ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(
156 ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
157 TextAnchor.HALF_ASCENT_LEFT, - 1.3D );
158 // 设置不能正常显示的柱子label的position
159 renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
160 renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
161 } else {
162 chart = ChartFactory.createBarChart( // 柱状图
163 // JFreeChart chart = ChartFactory.createLineChart3D( // 3D折线图
164 title, // 图表的标题
165 x, // 目录轴的显示标签
166 y, // 数值轴的显示标签
167 dataset, // 数据集
168 PlotOrientation.VERTICAL, // 图表方式:V垂直;H水平
169 true , // 是否显示图例
170 false , // 是否显示工具提示
171 false // 是否生成URL
172 );
173 // 柱图的呈现器
174 renderer = new BarRenderer();
175 renderer.setIncludeBaseInRange( true ); // 显示每个柱的数值,并修改该数值的字体属性
176 renderer.setBaseItemLabelGenerator( new StandardCategoryItemLabelGenerator());
177 renderer.setBaseItemLabelsVisible( true );
178 }
179
180 // 设置图片背景
181 // chart.setBackgroundPaint(Color.PINK);
182 // 为了防止中文乱码:必须设置字体
183 chart.setTitle( new TextTitle(title, new Font( " 黑体 " , Font.PLAIN, 22 )));
184 LegendTitle legend = chart.getLegend(); // 获取图例
185 legend.setItemFont( new Font( " 宋体 " , Font.BOLD, 12 )); // 设置图例的字体,防止中文乱码
186 CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
187 // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
188 plot.setBackgroundPaint( new Color( 255 , 255 , 204 ));
189 plot.setForegroundAlpha( 0.65F ); // 设置前景色透明度
190 // 设置横虚线可见
191 plot.setRangeGridlinesVisible( true );
192 // 虚线色彩
193 plot.setRangeGridlinePaint(Color.gray);
194
195 ValueAxis rangeAxis = plot.getRangeAxis();
196 // 设置最高的一个Item与图片顶端的距离
197 rangeAxis.setUpperMargin( 0.2 );
198 // 设置最低的一个Item与图片底端的距离
199 rangeAxis.setLowerMargin( 0.3 );
200
201 CategoryAxis domainAxis = plot.getDomainAxis(); // 获取x轴
202 domainAxis.setMaximumCategoryLabelWidthRatio( 1.0f ); // 横轴上的 Lable 是否完整显示
203 domainAxis.setLabelFont( new Font( " 宋体 " , Font.TRUETYPE_FONT, 14 )); // 设置字体,防止中文乱码
204 domainAxis.setTickLabelFont( new Font( " 宋体 " , Font.BOLD, 12 )); // 轴数值
205 // h.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 45度倾斜
206 plot.getRangeAxis().setLabelFont( new Font( " 宋体 " , Font.TRUETYPE_FONT, 14 )); // Y轴设置字体,防止中文乱码
207
208 renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框颜色
209 renderer.setDrawBarOutline( true ); // 设置柱子边框可见
210 renderer.setSeriesPaint( 0 , Color.YELLOW); // 设置每个柱的颜色
211 renderer.setSeriesPaint( 1 , Color.green);
212 renderer.setSeriesPaint( 2 , Color.RED);
213 renderer.setSeriesPaint( 3 , Color.CYAN);
214 renderer.setSeriesPaint( 5 , Color.ORANGE);
215 renderer.setSeriesPaint( 4 , Color.MAGENTA);
216 renderer.setSeriesPaint( 6 , Color.DARK_GRAY);
217 renderer.setSeriesPaint( 7 , Color.PINK);
218 renderer.setSeriesPaint( 8 , Color.black);
219 renderer.setItemMargin( 0.1 ); // 设置每个地区所包含的平行柱的之间距离
220 plot.setRenderer(renderer); // 给柱图添加呈现器
221 plot.setForegroundAlpha( 0.7f ); // 设置柱的透明度
222 // renderer.setMaximumBarWidth(0.2); // 设置柱子宽度
223 // renderer.setMinimumBarLength(0.6); // 设置柱子高度
224 // 设置横坐标显示位置(默认是下方);
225 // plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
226 // 设置纵坐标显示位置(默认是左方)
227 // plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
228 // 没有数据的时候显示的内容
229 plot.setNoDataMessage( " 找不到可用数据

230
231 return chart;
232 }
233
234 /**
235 *<br> 方法名称:JFreeChartSeriesChart
236 *<br> 功能描述:创建曲线图(折线图)
237 *<br> 注意事项:一般曲线图不用加域所以后4个参数一般为(false,null,0,0)
238 *<br> 参 数:title-标题;subtitleStr-子标题;domain-x轴标志;range-y轴标志;dataset-设置数据;isAreaText-是否在图标中加域;
239 *<br> areaText-域中文字,lowpress-域的最低刻度;uperpress-域的最高刻度
240 *<br> 返 回 值:JFreeChart
241 *<br> 创 建 人:
242 *<br> 创建日期:Mar 30, 2010 1:11:24 PM
243 */
244 @SuppressWarnings( " deprecation " )
245 public static JFreeChart JFreeChartSeriesChart(String title,
246 String subtitleStr, String domain, String range,
247 TimeSeriesCollection dataset, boolean isAreaText,String areaText, double lowpress, double uperpress) { // 时间曲线元素
248 // JFreeChart chart = ChartFactory.createTimeSeriesChart("标题","x轴标志","y轴标志","设置数据",是否显示图形,是否进行提示,是否配置报表存放地址);
249 JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,range, dataset, true , true , false );
250
251 if (subtitleStr != null ){
252 TextTitle subtitle = new TextTitle(subtitleStr, new Font( " 黑体 " ,Font.BOLD, 12 ));
253 chart.addSubtitle(subtitle);
254 }
255 // 设置日期显示格式
256 XYPlot plot = chart.getXYPlot();
257 DateAxis axis = (DateAxis) plot.getDomainAxis();
258 axis.setDateFormatOverride( new SimpleDateFormat( " yyyy-MM-dd " ));
259 // 设置标题的颜色
260 chart.setTitle( new TextTitle(title, new Font( " 黑体 " , Font.ITALIC, 22 )));
261 chart.setBackgroundPaint( new GradientPaint( 0 , 0 , Color.white, 0 , 1000 , Color.blue));
262 plot.setOutlineStroke( new BasicStroke( 1.5f )); // 边框粗细
263 ValueAxis vaxis = plot.getDomainAxis();
264 vaxis.setAxisLineStroke( new BasicStroke( 1.5f )); // 坐标轴粗细
265 vaxis.setAxisLinePaint( new Color( 215 , 215 , 215 )); // 坐标轴颜色
266 vaxis.setLabelPaint( new Color( 10 , 10 , 10 )); // 坐标轴标题颜色
267 vaxis.setTickLabelPaint( new Color( 102 , 102 , 102 )); // 坐标轴标尺值颜色
268 vaxis.setLowerMargin( 0.06d ); // 分类轴下(左)边距
269 vaxis.setUpperMargin( 0.14d ); // 分类轴下(右)边距,防止最后边的一个数据靠近了坐标轴。
270 plot.setNoDataMessage( " 找不到可用数据

271 XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
272 // 第一条折线的颜色
273 xylineandshaperenderer.setBaseItemLabelsVisible( true );
274 xylineandshaperenderer.setSeriesFillPaint( 0 , new Color( 127 , 128 , 0 ));
275 xylineandshaperenderer.setSeriesPaint( 0 , new Color( 127 , 128 , 0 ));
276 xylineandshaperenderer.setSeriesShapesVisible( 0 , true );
277 xylineandshaperenderer.setSeriesShapesVisible( 1 , true );
278 xylineandshaperenderer.setSeriesShapesVisible( 2 , true );
279 xylineandshaperenderer.setSeriesShapesVisible( 3 , true );
280 xylineandshaperenderer.setSeriesShapesVisible( 4 , true );
281 // 折线的粗细调
282 StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();
283 xylineandshaperenderer.setToolTipGenerator(xytool);
284 xylineandshaperenderer.setStroke( new BasicStroke( 1.5f ));
285 // 显示节点的值
286 xylineandshaperenderer.setBaseItemLabelsVisible( true );
287 xylineandshaperenderer.setBasePositiveItemLabelPosition( new ItemLabelPosition(
288 ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
289 xylineandshaperenderer.setBaseItemLabelGenerator( new StandardXYItemLabelGenerator());
290 xylineandshaperenderer.setBaseItemLabelPaint( new Color( 102 , 102 , 102 )); // 显示折点数值字体的颜色
291
292 ValueAxis rangeAxis = plot.getRangeAxis();
293 // 设置最高的一个Item与图片顶端的距离
294 rangeAxis.setUpperMargin( 0.2 );
295 // 设置最低的一个Item与图片底端的距离
296 rangeAxis.setLowerMargin( 0.3 );
297 // 在图表中加区域加区域
298 if (isAreaText){
299 lowpress = 62 ;
300 uperpress = 400 ;
301 IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);
302 intermarker.setPaint(Color.decode( " #66FFCC " )); // 域顏色
303 intermarker.setLabelFont( new Font( " SansSerif " , 41 , 14 ));
304 intermarker.setLabelPaint(Color.RED);
305 intermarker.setLabel(areaText);
306 if (dataset != null ) {
307 plot.addRangeMarker(intermarker, Layer.BACKGROUND);
308 }
309 }
310 return chart;
311 }
312
313
314 /**
315 *<br> 方法名称:getGraphURL
316 *<br> 功能描述:取得session中图像的地址;
317 *<br> 返 回 值:String
318 *<br> 创 建 人:
319 *<br> 创建日期:Mar 30, 2010 1:09:51 PM
320 */
321 public static String getGraphURL(JFreeChart chart, HttpSession session,
322 HttpServletRequest request, int width, int height) throws IOException {
323 String filename = ServletUtilities.saveChartAsPNG(chart, width, height, null , session);
324 String graphURL = request.getContextPath() + " /servlet/DisplayChart?filename= " + filename;
325 return graphURL;
326 }
327
328 }
329
330
2、模拟数据
1
//
柱状图
2 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
3 dataset.setValue( 5003 , " 北京 " , " Corejava " );
4 dataset.setValue( 3333 , " 上海 " , " Corejava " );
5 dataset.setValue( 5343 , " 广州 " , " Corejava " );
6 dataset.setValue( 2000 , " 贵州 " , " Corejava " );
7
8 dataset.setValue( 1000 , " 北京 " , " JavaWeb " );
9 dataset.setValue( 6300 , " 上海 " , " JavaWeb " );
10 dataset.setValue( 4000 , " 广州 " , " JavaWeb " );
11
12 dataset.setValue( 2000 , " 北京 " , " jquery " );
13 dataset.setValue( 1000 , " 上海 " , " jquery " );
14 dataset.setValue( 1000 , " 广州 " , " jquery " );
15 JFreeChart jfreechart = JFreeChartCreater.createBarChart(dataset, " 课件使用情况条形图 " , " 课件制作者 " , " 可见使用次数 " , true );
16
17 // 折线图
18 // 创建第一条时序线
19 TimeSeries pop1 = new TimeSeries( " 流行趋势1 " , Day. class );
20 pop1.add( new Day( 10 , 1 , 2004 ), 100 );
21 pop1.add( new Day( 10 , 2 , 2004 ), 150 );
22 pop1.add( new Day( 10 , 3 , 2004 ), 250 );
23 pop1.add( new Day( 10 , 4 , 2004 ), 275 );
24 pop1.add( new Day( 10 , 5 , 2004 ), 325 );
25 pop1.add( new Day( 10 , 6 , 2004 ), 425 );
26 // 创建第二条时序线
27 TimeSeries pop2 = new TimeSeries( " 流行趋势2 " , Day. class );
28 pop2.add( new Day( 20 , 1 , 2004 ), 200 );
29 pop2.add( new Day( 20 , 2 , 2004 ), 250 );
30 pop2.add( new Day( 20 , 3 , 2004 ), 450 );
31 pop2.add( new Day( 20 , 4 , 2004 ), 475 );
32 pop2.add( new Day( 20 , 5 , 2004 ), 125 );
33 pop2.add( new Day( 20 , 6 , 2004 ), 150 );
34 JFreeChart jfreechart = JFreeChartCreater.JFreeChartSeriesChart( " 课件使用情况条形图 " , null , " X轴 " , " Y轴 " , dataset, false , null , 0 , 0 );
35
36 // 饼图
37 DefaultPieDataset dataset = new DefaultPieDataset();
38
39 dataset.setValue( " java程序设计语言 " , 10000 );
40 dataset.setValue( " JSP基础与案例开发详解 " , 20000 );
41 dataset.setValue( " struts基础与案例开发详解 " , 30000 );
42 dataset.setValue( " 精通JSF " , 4000 );
43 JFreeChart jfreechart = JFreeChartCreater.createPieChart(dataset,
44 " 课件使用情况条形图 " , false );
45
46 request.setAttribute( " jfreechart " , jfreechart);
2 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
3 dataset.setValue( 5003 , " 北京 " , " Corejava " );
4 dataset.setValue( 3333 , " 上海 " , " Corejava " );
5 dataset.setValue( 5343 , " 广州 " , " Corejava " );
6 dataset.setValue( 2000 , " 贵州 " , " Corejava " );
7
8 dataset.setValue( 1000 , " 北京 " , " JavaWeb " );
9 dataset.setValue( 6300 , " 上海 " , " JavaWeb " );
10 dataset.setValue( 4000 , " 广州 " , " JavaWeb " );
11
12 dataset.setValue( 2000 , " 北京 " , " jquery " );
13 dataset.setValue( 1000 , " 上海 " , " jquery " );
14 dataset.setValue( 1000 , " 广州 " , " jquery " );
15 JFreeChart jfreechart = JFreeChartCreater.createBarChart(dataset, " 课件使用情况条形图 " , " 课件制作者 " , " 可见使用次数 " , true );
16
17 // 折线图
18 // 创建第一条时序线
19 TimeSeries pop1 = new TimeSeries( " 流行趋势1 " , Day. class );
20 pop1.add( new Day( 10 , 1 , 2004 ), 100 );
21 pop1.add( new Day( 10 , 2 , 2004 ), 150 );
22 pop1.add( new Day( 10 , 3 , 2004 ), 250 );
23 pop1.add( new Day( 10 , 4 , 2004 ), 275 );
24 pop1.add( new Day( 10 , 5 , 2004 ), 325 );
25 pop1.add( new Day( 10 , 6 , 2004 ), 425 );
26 // 创建第二条时序线
27 TimeSeries pop2 = new TimeSeries( " 流行趋势2 " , Day. class );
28 pop2.add( new Day( 20 , 1 , 2004 ), 200 );
29 pop2.add( new Day( 20 , 2 , 2004 ), 250 );
30 pop2.add( new Day( 20 , 3 , 2004 ), 450 );
31 pop2.add( new Day( 20 , 4 , 2004 ), 475 );
32 pop2.add( new Day( 20 , 5 , 2004 ), 125 );
33 pop2.add( new Day( 20 , 6 , 2004 ), 150 );
34 JFreeChart jfreechart = JFreeChartCreater.JFreeChartSeriesChart( " 课件使用情况条形图 " , null , " X轴 " , " Y轴 " , dataset, false , null , 0 , 0 );
35
36 // 饼图
37 DefaultPieDataset dataset = new DefaultPieDataset();
38
39 dataset.setValue( " java程序设计语言 " , 10000 );
40 dataset.setValue( " JSP基础与案例开发详解 " , 20000 );
41 dataset.setValue( " struts基础与案例开发详解 " , 30000 );
42 dataset.setValue( " 精通JSF " , 4000 );
43 JFreeChart jfreechart = JFreeChartCreater.createPieChart(dataset,
44 " 课件使用情况条形图 " , false );
45
46 request.setAttribute( " jfreechart " , jfreechart);
3、jsp页面
1
<%
@ page language
=
"
java
"
pageEncoding
=
"
utf-8
"
%>
2 <% @ include file = " /includes/taglibs.jsp " %>
3 < jsp:directive .page import ="org.jfree.chart.*" />
4 < jsp:directive .page import ="org.jfree.chart.entity.*" />
5 < jsp:directive .page import ="org.jfree.chart.servlet.ServletUtilities" />
6 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
7 < html xmlns ="http://www.w3.org/1999/xhtml" >
8 < head >
9 < meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
10 < title > 课件使用情况柱状图 </ title >
11 </ head >
12 < body >
13 <%
14 JFreeChart jfreechart = (JFreeChart) request.getAttribute( " jfreechart " );
15 StandardEntityCollection sec = new StandardEntityCollection();
16 ChartRenderingInfo info = new ChartRenderingInfo(sec);
17 String filename = ServletUtilities.saveChartAsJPEG(jfreechart, 500 , 300 , info, session);
18 String graphURL = request.getContextPath() + " /servlet/DisplayChart?filename= " + filename;
19 %>
20
21 < P ALIGN ="CENTER" >
22 < img src ="<%=graphURL%>" width =500 height =300 border =0 usemap ="#map0" >
23 </ P >
24 < p ALIGN ="CENTER" >
25 < input type ="button" value ="关闭" onclick ="JavaScript:window.close()" />
26 </ p >
27 </ body >
28 </ html >
29
2 <% @ include file = " /includes/taglibs.jsp " %>
3 < jsp:directive .page import ="org.jfree.chart.*" />
4 < jsp:directive .page import ="org.jfree.chart.entity.*" />
5 < jsp:directive .page import ="org.jfree.chart.servlet.ServletUtilities" />
6 <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
7 < html xmlns ="http://www.w3.org/1999/xhtml" >
8 < head >
9 < meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
10 < title > 课件使用情况柱状图 </ title >
11 </ head >
12 < body >
13 <%
14 JFreeChart jfreechart = (JFreeChart) request.getAttribute( " jfreechart " );
15 StandardEntityCollection sec = new StandardEntityCollection();
16 ChartRenderingInfo info = new ChartRenderingInfo(sec);
17 String filename = ServletUtilities.saveChartAsJPEG(jfreechart, 500 , 300 , info, session);
18 String graphURL = request.getContextPath() + " /servlet/DisplayChart?filename= " + filename;
19 %>
20
21 < P ALIGN ="CENTER" >
22 < img src ="<%=graphURL%>" width =500 height =300 border =0 usemap ="#map0" >
23 </ P >
24 < p ALIGN ="CENTER" >
25 < input type ="button" value ="关闭" onclick ="JavaScript:window.close()" />
26 </ p >
27 </ body >
28 </ html >
29