JFreeChart主要用来各种各样的图表,这些图表包括:饼图、柱状图(普通柱状图以及堆栈柱状图)、线图、区域图、分布图、混合图、甘特图以及一些仪表盘等等
(源代码下载)
示例程序运用的jar包:
- jcommon-1.0.17.jar
- jfreechart-1.0.14.jar
1:普通柱状图
这是程序调用CategoryDataset dataset = getDataSet2(); 后所生产的图片
BarChartDemo.java柱状图代码
1 /** 2 * 该类用于演示最简单的柱状图生成 3 * @author Winter Lau 4 */ 5 public class BarChartDemo { 6 public static void main(String[] args) throws IOException{ 7 8 CategoryDataset dataset = getDataSet2(); 9 JFreeChart chart = ChartFactory.createBarChart3D( 10 "水果产量图", // 图表标题 11 "水果", // 目录轴的显示标签 12 "产量", // 数值轴的显示标签 13 dataset, // 数据集 14 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 15 true, // 是否显示图例(对于简单的柱状图必须是 false) 16 false, // 是否生成工具 17 false // 是否生成 URL 链接 18 ); 19 //中文乱码 20 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); 21 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); 22 CategoryAxis domainAxis = categoryplot.getDomainAxis(); 23 TextTitle textTitle = chart.getTitle(); 24 textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); 25 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 26 domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); 27 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 28 numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); 29 chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); 30 31 FileOutputStream fos_jpg = null; 32 try { 33 fos_jpg = new FileOutputStream("D:\BarChart.jpg"); 34 ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f,chart,400,300,null); 35 } finally { 36 try { 37 fos_jpg.close(); 38 } catch (Exception e) {} 39 } 40 } 41 /** 42 * 获取一个演示用的简单数据集对象 43 * @return 44 */ 45 private static CategoryDataset getDataSet() { 46 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 47 dataset.addValue(100, "", "苹果"); 48 dataset.addValue(200, "", "梨子"); 49 dataset.addValue(300, "", "葡萄"); 50 dataset.addValue(400, "", "香蕉"); 51 dataset.addValue(500, "", "荔枝"); 52 return dataset; 53 } 54 /** 55 * 获取一个演示用的组合数据集对象 56 * @return 57 */ 58 private static CategoryDataset getDataSet2() { 59 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 60 dataset.addValue(100, "北京", "苹果"); 61 dataset.addValue(100, "上海", "苹果"); 62 dataset.addValue(100, "广州", "苹果"); 63 dataset.addValue(200, "北京", "梨子"); 64 dataset.addValue(200, "上海", "梨子"); 65 dataset.addValue(200, "广州", "梨子"); 66 dataset.addValue(300, "北京", "葡萄"); 67 dataset.addValue(300, "上海", "葡萄"); 68 dataset.addValue(300, "广州", "葡萄"); 69 dataset.addValue(400, "北京", "香蕉"); 70 dataset.addValue(400, "上海", "香蕉"); 71 dataset.addValue(400, "广州", "香蕉"); 72 dataset.addValue(500, "北京", "荔枝"); 73 dataset.addValue(500, "上海", "荔枝"); 74 dataset.addValue(500, "广州", "荔枝"); 75 return dataset; 76 } 77 }
2:饼状图
PieChartDemo.java饼状图源代码
1 /** 2 * 用于演示饼图的生成 3 */ 4 public class PieChartDemo { 5 public static void main(String[] args) throws IOException { 6 DefaultPieDataset data = getDataSet(); 7 JFreeChart chart = ChartFactory.createPieChart3D("水果产量图", // 图表标题 8 data, // 数据集 9 true, // 是否显示图例(对于简单的柱状图必须是 false) 10 false, // 是否生成工具 11 false // 是否生成 URL 链接 12 ); 13 14 //中文乱码 15 PiePlot3D plot = (PiePlot3D) chart.getPlot(); 16 plot.setLabelFont(new Font("黑体", Font.PLAIN, 20)); 17 TextTitle textTitle = chart.getTitle(); 18 textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); 19 chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); 20 21 // 写图表对象到文件,参照柱状图生成源码 22 FileOutputStream fos_jpg = null; 23 try { 24 fos_jpg = new FileOutputStream("D:\Pie3DChart.jpg"); 25 ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, 400, 300, 26 null); 27 } finally { 28 try { 29 fos_jpg.close(); 30 } catch (Exception e) { 31 } 32 } 33 } 34 35 /** 36 * 获取一个演示用的简单数据集对象 37 * 38 * @return 39 */ 40 private static DefaultPieDataset getDataSet() { 41 DefaultPieDataset dataset = new DefaultPieDataset(); 42 dataset.setValue("苹果", 100); 43 dataset.setValue("梨子", 200); 44 dataset.setValue("葡萄", 300); 45 dataset.setValue("香蕉", 400); 46 dataset.setValue("荔枝", 500); 47 return dataset; 48 } 49 }
其他的图表类型暂时不写了,其实写法也差不多。这里注意两种图形的中文乱码解决方法是不一样的,具体查看源代码中的红色部分
3:将数据库的内容用图形表示出来
index.jsp
1 <body> 2 <form action="servlet/ShowChartServlet" method="get" > 3 <input type="submit" value="jfreechart访问数据库生成图表"> 4 </form> 5 </body>
showJfreeChart.jsp
1 <body> 2 <center> 3 <img alt="" src="<%=path %>/reports/productSales.jpg"> 4 </center> 5 </body>
ShowChartServlet.java
1 public class ShowChartServlet extends HttpServlet { 2 3 /** 4 * 获取一个演示用的组合数据集对象 5 * 6 * @return 7 */ 8 private static CategoryDataset getDataSet() { 9 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 10 // 从数据库中获取 11 Connection conn = null; 12 PreparedStatement ps = null; 13 ResultSet rs = null; 14 try { 15 Class.forName("com.mysql.jdbc.Driver"); // 加载驱动程序 16 conn = DriverManager 17 .getConnection( 18 "jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=utf8", 19 "root", "root"); 20 ps = conn.prepareStatement("select p.name, count(pcount) from product p join salesitem si on (p.id = si.productid) group by p.id"); 21 rs = ps.executeQuery(); 22 while (rs.next()) { 23 dataset.addValue(rs.getInt(2), "", rs.getString(1)); 24 } 25 rs.close(); 26 rs = null; 27 } catch (SQLException e) { 28 e.printStackTrace(); 29 }catch (ClassNotFoundException e) { 30 e.printStackTrace(); 31 } 32 finally { 33 if(ps != null){ 34 try { 35 ps.close(); 36 } catch (SQLException e) { 37 e.printStackTrace(); 38 } 39 } 40 if(conn != null){ 41 try { 42 conn.close(); 43 } catch (SQLException e) { 44 e.printStackTrace(); 45 } 46 } 47 48 } 49 50 return dataset; 51 } 52 53 public void doGet(HttpServletRequest request, HttpServletResponse response) 54 throws ServletException, IOException { 55 56 CategoryDataset dataset = getDataSet(); 57 JFreeChart chart = ChartFactory.createBarChart3D( 58 "产品销量图", // 图表标题 59 "产品", // 目录轴的显示标签 60 "销量", // 数值轴的显示标签 61 dataset, // 数据集 62 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 63 true, // 是否显示图例(对于简单的柱状图必须是 false) 64 false, // 是否生成工具 65 false // 是否生成 URL 链接 66 ); 67 //中文乱码 68 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); 69 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); 70 CategoryAxis domainAxis = categoryplot.getDomainAxis(); 71 TextTitle textTitle = chart.getTitle(); 72 textTitle.setFont(new Font("黑体", Font.PLAIN, 20)); 73 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 74 domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); 75 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 76 numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); 77 chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); 78 79 FileOutputStream fos_jpg = null; 80 try { 81 fos_jpg = new FileOutputStream("E:\Workspaces\MyEclipse 9\WebProjects\JFreeChart\WebRoot\reports\productSales.jpg"); 82 ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f,chart,400,300,null); 83 String path = this.getServletContext().getContextPath(); 84 System.out.println(path+"mmmmmmmmmmm"); 85 this.getServletContext().getRequestDispatcher("/showJfreeChart.jsp").forward(request, response); 86 } finally { 87 try { 88 fos_jpg.close(); 89 } catch (Exception e) {} 90 } 91 } 92 93 }