Java 报表之JFreeChart(第二讲)
Posted 啊远
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 报表之JFreeChart(第二讲)相关的知识,希望对你有一定的参考价值。
1、利用 JFreeChart 创建按颜色分类的水果销售报表
1 package com.wcy.chart.bar; 2 3 import javax.servlet.http.HttpSession; 4 5 import org.jfree.chart.ChartFactory; 6 import org.jfree.chart.JFreeChart; 7 import org.jfree.chart.plot.PlotOrientation; 8 import org.jfree.chart.servlet.ServletUtilities; 9 import org.jfree.data.category.CategoryDataset; 10 import org.jfree.data.general.DatasetUtilities; 11 12 public class BarChart3 { 13 14 public static String genBarChart(HttpSession session)throws Exception{ 15 double[][] data = new double[][]{{1320},{720},{830},{400}}; 16 String[] rowKeys = {"苹果","香蕉","橘子","梨子"}; 17 String[] columnKeys = {"深圳"}; 18 CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys,data); 19 JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", 20 dataset, PlotOrientation.VERTICAL, true, true, true); 21 String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session); 22 return fileName; 23 } 24 }
1 <%@page import="com.wcy.chart.bar.BarChart3"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 String fileName = BarChart3.genBarChart(session); 13 %> 14 <img src="DisplayChart?filename=<%=fileName %>" width="700" height="500"> 15 </body> 16 </html>
2、利用 JFreeChart 创建按颜色分类并且按地区分类水果销售报表
1 package com.wcy.chart.bar; 2 3 import javax.servlet.http.HttpSession; 4 5 import org.jfree.chart.ChartFactory; 6 import org.jfree.chart.JFreeChart; 7 import org.jfree.chart.plot.PlotOrientation; 8 import org.jfree.chart.servlet.ServletUtilities; 9 import org.jfree.data.category.CategoryDataset; 10 import org.jfree.data.general.DatasetUtilities; 11 12 public class BarChart4 { 13 14 public static String genBarChart(HttpSession session)throws Exception{ 15 double[][] data = new double[][]{{1390,690,542,630},{529,650,1200,690},{780,450,1300,1120},{890,640,750,500}}; 16 String[] rowKeys = {"苹果","香蕉","橘子","梨子"}; 17 String[] columnKeys = {"上海","北京","贵州","广州"}; 18 CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data); 19 JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, true); 20 String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session); 21 return fileName; 22 } 23 }
1 <%@page import="com.wcy.chart.bar.BarChart4"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 String fileName = BarChart4.genBarChart(session); 13 %> 14 <img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0"> 15 </body> 16 </html>
3、利用 JFreeChart 创建自定义 3D 柱状报表
1 package com.wcy.chart.bar; 2 3 import java.awt.Color; 4 5 import javax.servlet.http.HttpSession; 6 7 import org.jfree.chart.ChartFactory; 8 import org.jfree.chart.JFreeChart; 9 import org.jfree.chart.labels.ItemLabelAnchor; 10 import org.jfree.chart.labels.ItemLabelPosition; 11 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 12 import org.jfree.chart.plot.CategoryPlot; 13 import org.jfree.chart.plot.PlotOrientation; 14 import org.jfree.chart.renderer.category.BarRenderer3D; 15 import org.jfree.chart.servlet.ServletUtilities; 16 import org.jfree.data.category.CategoryDataset; 17 import org.jfree.data.general.DatasetUtilities; 18 import org.jfree.ui.TextAnchor; 19 20 public class BarChart5 { 21 22 public static String genBarChart(HttpSession session)throws Exception{ 23 double[][] data = new double[][]{{1299,459,980,470},{590,564,300,1130},{1200,357,910,380},{509,1300,650,810}}; 24 String[] rowKeys = {"苹果","香蕉","橘子","梨子"}; 25 String[] columnKeys = {"上海","北京","厦门","贵州"}; 26 CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys,data); 27 JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, true); 28 29 CategoryPlot plot = chart.getCategoryPlot(); 30 31 // 设置网格背景颜色 32 plot.setBackgroundPaint(Color.WHITE); 33 // 设置网格竖线颜色 34 plot.setDomainGridlinePaint(Color.pink); 35 // 设置网格横线颜色 36 plot.setRangeGridlinePaint(Color.pink); 37 38 // 显示每个柱的数值,并修改该数值的字体属性 39 BarRenderer3D renderer=new BarRenderer3D(); 40 renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 41 renderer.setBaseItemLabelsVisible(true); 42 43 renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); 44 renderer.setItemLabelAnchorOffset(10D); 45 46 // 设置平行柱的之间距离 47 renderer.setItemMargin(0.4); 48 49 plot.setRenderer(renderer); 50 51 String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session); 52 return fileName; 53 } 54 }
1 <%@page import="com.wcy.chart.bar.BarChart5"%> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 String fileName = BarChart5.genBarChart(session); 13 %> 14 <img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0"> 15 </body> 16 </html>
声明:此程序代码本人只是用于学习总结,非原创,如有侵权,联系本人。
以上是关于Java 报表之JFreeChart(第二讲)的主要内容,如果未能解决你的问题,请参考以下文章