JFreeChart 用法与示例

JFreeChart 用法与示例

(学习留存,如有侵权,请告知,立刻删除)

1.创建JFreeChart步骤大致为以下三步:

(1).创建数据集对象

DefaultCategoryDataset dataSet = new DefaultCategoryDataset();  

(2).创建JFreeChart对象

// 通过ChartFactory创建JFreeChart

JFreeChart chart = ChartFactory.createBarChart3D();或者JFreeChart chart = new JFreeChart();  

(3).创建呈现媒介,并将chart装入媒介

ChartFrame cf = new ChartFrame();

或者:

String fileName = ServletUtilities.saveChartAsJPEG(ChartUtil.createChart(), 450, 300, session);  

String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;  


2.1 示例1:

2.1.1 ChartUtil.java代码清单:

  1. package com.xqh.util.jfree;  
  2.   
  3. import java.awt.Font;  
  4.   
  5. import org.jfree.chart.ChartFactory;  
  6. import org.jfree.chart.ChartFrame;  
  7. import org.jfree.chart.JFreeChart;  
  8. import org.jfree.chart.StandardChartTheme;  
  9. import org.jfree.chart.plot.PlotOrientation;  
  10. import org.jfree.data.category.CategoryDataset;  
  11. import org.jfree.data.category.DefaultCategoryDataset;  
  12.   
  13. public class ChartUtil {  
  14.     /** 
  15.      * 创建数据集合 
  16.      * @return dataSet 
  17.      */  
  18.     public static CategoryDataset createDataSet() {  
  19.         // 实例化DefaultCategoryDataset对象  
  20.         DefaultCategoryDataset dataSet = new DefaultCategoryDataset();  
  21.         // 向数据集合中添加数据  
  22.         dataSet.addValue(500, "Java图书", "J2SE类");  
  23.         dataSet.addValue(100, "Java图书", "J2ME类");  
  24.         dataSet.addValue(900, "Java图书", "J2EE类");  
  25.         return dataSet;  
  26.     }  
  27.     /** 
  28.      * 创建JFreeChart对象 
  29.      * @return chart 
  30.      */  
  31.     public static JFreeChart createChart() {  
  32.         StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); //创建主题样式  
  33.         standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); //设置标题字体  
  34.         standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15)); //设置图例的字体  
  35.         standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15)); //设置轴向的字体  
  36.         ChartFactory.setChartTheme(standardChartTheme);//设置主题样式  
  37.         // 通过ChartFactory创建JFreeChart  
  38.         JFreeChart chart = ChartFactory.createBarChart3D(  
  39.                 "Java图书销量统计", //图表标题  
  40.                 "Java图书", //横轴标题  
  41.                 "销量(本)",//纵轴标题  
  42.                 createDataSet(),//数据集合  
  43.                 PlotOrientation.VERTICAL, //图表方向  
  44.                 true,//是否显示图例标识  
  45.                 false,//是否显示tooltips  
  46.                 false);//是否支持超链接  
  47.         return chart;  
  48.     }  
  49.     // 本地测试  
  50.     public static void main(String[] args) {  
  51.         ChartFrame cf = new ChartFrame("Test", createChart());  
  52.         cf.pack();  
  53.         cf.setVisible(true);  
  54.     }  
  55. }  


2.1.2 index.jsp 代码清单:

  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
  2. <%@ page import="org.jfree.chart.servlet.ServletUtilities,   
  3.                             com.xqh.util.jfree.ChartUtil" %>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.       
  14.     <title>Java 图书销量统计</title>  
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!--  
  21.     <link rel="stylesheet" type="text/css" href="styles.css">  
  22.     -->  
  23.   </head>  
  24.     
  25.   <body>  
  26.     <%  
  27.         String fileName = ServletUtilities.saveChartAsJPEG(ChartUtil.createChart(), 450, 300, session);  
  28.         String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;  
  29.     %>  
  30.     <img src="<%=graphURL %>" border="1"/>  
  31.   </body>  
  32. </html>  


2.1.3 web.xml 配置信息:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <servlet>  
  11.     <servlet-name>DisplayChart</servlet-name>  
  12.     <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>  
  13.   </servlet>  
  14.   <servlet-mapping>  
  15.     <servlet-name>DisplayChart</servlet-name>  
  16.     <url-pattern>/servlet/DisplayChart</url-pattern>  
  17.   </servlet-mapping>  
  18. </web-app>  


2.1.4 运行结果:

2.2 示例2:

在示例1的基础上,对制图对象、绘图区对象、坐标轴对象。图片渲染对象等属性进行设置,并添加一个子标题

2.2.1 ChartUtil.java程序清单:

  1. package com.xqh.util.jfree;  
  2.   
  3. import java.awt.Font;  
  4. import java.awt.Image;  
  5. import java.io.IOException;  
  6.   
  7. import javax.imageio.ImageIO;  
  8.   
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartFrame;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.StandardChartTheme;  
  13. import org.jfree.chart.axis.CategoryAxis;  
  14. import org.jfree.chart.axis.CategoryLabelPositions;  
  15. import org.jfree.chart.axis.ValueAxis;  
  16. import org.jfree.chart.plot.CategoryPlot;  
  17. import org.jfree.chart.plot.PlotOrientation;  
  18. import org.jfree.chart.renderer.category.BarRenderer3D;  
  19. import org.jfree.chart.title.TextTitle;  
  20. import org.jfree.data.category.CategoryDataset;  
  21. import org.jfree.data.category.DefaultCategoryDataset;  
  22. import org.jfree.ui.VerticalAlignment;  
  23.   
  24. public class ChartUtil {  
  25.     /** 
  26.      * 创建数据集合 
  27.      * @return dataSet 
  28.      */  
  29.     public static CategoryDataset createDataSet() {  
  30.         // 实例化DefaultCategoryDataset对象  
  31.         DefaultCategoryDataset dataSet = new DefaultCategoryDataset();  
  32.         // 添加第一季度数据  
  33.         dataSet.addValue(6000, "第一季度", "J2SE类");  
  34.         dataSet.addValue(3000, "第一季度", "J2ME类");  
  35.         dataSet.addValue(12000, "第一季度", "J2EE类");  
  36.         // 添加第二季度数据  
  37.         dataSet.addValue(8000, "第二季度", "J2SE类");  
  38.         dataSet.addValue(4000, "第二季度", "J2ME类");  
  39.         dataSet.addValue(6000, "第二季度", "J2EE类");  
  40.         // 添加第三季度数据  
  41.         dataSet.addValue(5000, "第三季度", "J2SE类");  
  42.         dataSet.addValue(4000, "第三季度", "J2ME类");  
  43.         dataSet.addValue(8000, "第三季度", "J2EE类");  
  44.         // 添加第四季度数据  
  45.         dataSet.addValue(8000, "第四季度", "J2SE类");  
  46.         dataSet.addValue(2000, "第四季度", "J2ME类");  
  47.         dataSet.addValue(9000, "第四季度", "J2EE类");  
  48.         return dataSet;  
  49.     }  
  50.     /** 
  51.      * 创建JFreeChart对象 
  52.      * @return chart 
  53.      */  
  54.     public static JFreeChart createChart() {  
  55.         StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); //创建主题样式  
  56.         standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); //设置标题字体  
  57.         standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15)); //设置图例的字体  
  58.         standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15)); //设置轴向的字体  
  59.         ChartFactory.setChartTheme(standardChartTheme);//设置主题样式  
  60.         // 通过ChartFactory创建JFreeChart  
  61.         JFreeChart chart = ChartFactory.createBarChart3D(  
  62.                 "Java图书销量统计", //图表标题  
  63.                 "Java图书", //横轴标题  
  64.                 "销量(本)",//纵轴标题  
  65.                 createDataSet(),//数据集合  
  66.                 PlotOrientation.VERTICAL, //图表方向  
  67.                 true,//是否显示图例标识  
  68.                 false,//是否显示tooltips  
  69.                 false);//是否支持超链接  
  70.         // 背景图片  
  71.         Image image = null;  
  72.         try {  
  73.             // 创建背景图片  
  74.             image = ImageIO.read(ChartUtil.class.getResource("test.jpg"));  
  75.         } catch (IOException e){  
  76.             e.printStackTrace();  
  77.         }  
  78.         chart.getTitle().setFont(new Font("隶书", Font.BOLD, 25)); // 设置标题字体  
  79.         chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); // 设置图例类别字体  
  80.         chart.setBorderVisible(true); // 设置显示边框  
  81.         TextTitle subTitle = new TextTitle("2012年Java类图书全国销量统计(J2SE, J2ME, J2EE)");//实例化TextTitle对象  
  82.         subTitle.setVerticalAlignment(VerticalAlignment.BOTTOM); //设置居中显示  
  83.         chart.addSubtitle(subTitle);//添加子标题  
  84.         CategoryPlot plot = chart.getCategoryPlot(); // 获取绘图区对象  
  85.         plot.setForegroundAlpha(0.8F);//设置绘图区前景色透明度  
  86.         plot.setBackgroundAlpha(0.5F);//设置绘图区背景色透明度  
  87.         plot.setBackgroundImage(image);//设置绘图区背景图片  
  88.         CategoryAxis categoryAxis = plot.getDomainAxis();//获取坐标轴对象  
  89.         categoryAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));//设置坐标轴标题字体  
  90.         categoryAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//设置坐标轴尺值字体  
  91.         categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 设置坐标轴标题旋转角度  
  92.         ValueAxis valueAxis = plot.getRangeAxis();// 设置数据轴对象  
  93.         valueAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));  
  94.         BarRenderer3D renderer = new BarRenderer3D();  
  95.         renderer.setItemMargin(0.32); // 设置柱间的间距  
  96.         plot.setRenderer(renderer);// 设置图片渲染对象    
  97.         return chart;  
  98.     }  
  99.     // 本地测试  
  100.     public static void main(String[] args) {  
  101.         ChartFrame cf = new ChartFrame("Test", createChart());  
  102.         cf.pack();  
  103.         cf.setVisible(true);  
  104.     }  
  105. }  


2.2.2 index.jsp 程序清单:

  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
  2. <%@ page import="org.jfree.chart.servlet.ServletUtilities,   
  3.                             com.xqh.util.jfree.ChartUtil" %>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.       
  14.     <title>Java 图书销量统计</title>  
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.     <!--  
  21.     <link rel="stylesheet" type="text/css" href="styles.css">  
  22.     -->  
  23.   </head>  
  24.     
  25.   <body>  
  26.     <%  
  27.         String fileName = ServletUtilities.saveChartAsJPEG(ChartUtil.createChart(), 450, 300, session);  
  28.         String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName;  
  29.     %>  
  30.     <img src="<%=graphURL %>" border="1"/>  
  31.   </body>  
  32. </html>  


2.2.3 web.xml 配置信息:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <welcome-file-list>  
  8.     <welcome-file>index.jsp</welcome-file>  
  9.   </welcome-file-list>  
  10.   <servlet>  
  11.     <servlet-name>DisplayChart</servlet-name>  
  12.     <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>  
  13.   </servlet>  
  14.   <servlet-mapping>  
  15.     <servlet-name>DisplayChart</servlet-name>  
  16.     <url-pattern>/servlet/DisplayChart</url-pattern>  
  17.   </servlet-mapping>  
  18. </web-app>  


2.2.4运行结果:

猜你喜欢

转载自blog.csdn.net/zhaoyanmi/article/details/87625079