[整理]JSP利用jfreechart畫餅圖

一、首先需要jfreechart的包:jfreechart-1.0.15.jar、jfreechart-1.0.15-experimental.jar、jfreechart-1.0.15-swt.jar、jcommon-1.0.18.jar

       搜索“jfreechart-1.0.15.zip下載”,解壓後在lib文件夾裏可以找到

二、配置站點的web.xml文件,增加以下節點:

  <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>


三、JSP文件代碼:

bing1.jsp 一個基本的餅圖

 

 <%@ page contentType="text/html;charset=GBK"%>
 
 <%@ page
     import="org.jfree.chart.*,org.jfree.chart.plot.PiePlot,
     org.jfree.data.general.DefaultPieDataset,
     org.jfree.chart.servlet.ServletUtilities,
     java.awt.*,org.jfree.chart.title.TextTitle"%>
 <%
     //設置數據集
     DefaultPieDataset dataset = new DefaultPieDataset();
     dataset.setValue("初中高級程序員", 0.55);
     dataset.setValue("項目經理", 0.1);
     dataset.setValue("系統分析師", 0.1);
     dataset.setValue("軟件架構師", 0.1);
     dataset.setValue("其他", 0.2);
 
     //通過工廠類生成JFreeChart對象
     JFreeChart chart = ChartFactory.createPieChart3D("IT行業職業分佈圖",
             dataset, true, false, false);
     PiePlot pieplot = (PiePlot) chart.getPlot();
 
     //沒有數據的時候顯示的內容
     pieplot.setNoDataMessage("無數據顯示");
     pieplot.setCircular(false);
     pieplot.setLabelGap(0.02D);
 
     //標題文字亂碼
     TextTitle textTitle = chart.getTitle();
     textTitle.setFont(new Font("宋體", Font.PLAIN, 20));
     //餅上的文字亂碼
     PiePlot plot = (PiePlot) chart.getPlot();
     plot.setLabelFont(new Font("宋體", Font.BOLD, 20));
     //圖例文字亂碼
     chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));
 
     String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
             null, session);
     String graphURL = request.getContextPath()
             + "/DisplayChart?filename=" + filename;
 %>
 
 <html>
     <head>
         <title>餅狀圖1(基本的餅圖)</title>
     </head>
     <body>
         <img src="<%=graphURL%>" width=500 height=300 border=0
             usemap="#<%= filename %>">
     </body>
 </html>


bing2.jsp 一個顯示百分比的餅圖

<%@ page contentType="text/html;charset=GBK"%>
 
 <%@ page
     import="org.jfree.chart.*,org.jfree.chart.plot.PiePlot,
     org.jfree.data.general.DefaultPieDataset,
     org.jfree.chart.servlet.ServletUtilities,
     java.awt.*,org.jfree.chart.title.TextTitle"%>
 <%@ page
     import="org.jfree.chart.labels.StandardPieSectionLabelGenerator"%>
 <%@ page import="java.text.NumberFormat"%>
 <%@page import="java.text.DecimalFormat"%>
 
 <%
     //設置數據集
     DefaultPieDataset dataset = new DefaultPieDataset();
     dataset.setValue("初中高級程序員", 0.52);
     dataset.setValue("項目經理", 0.1);
     dataset.setValue("系統分析師", 0.1);
     dataset.setValue("軟件架構師", 0.1);
     dataset.setValue("其他", 0.18);
 
     //通過工廠類生成JFreeChart對象
     JFreeChart chart = ChartFactory.createPieChart3D("IT行業職業分佈圖",
             dataset, true, false, false);
 
     PiePlot pieplot = (PiePlot) chart.getPlot();
     //一塊突出的餅圖,在網上搜了好久也沒找到可行的實現方式,歡迎大俠指導 <?後期如果找到解決方法再來做修改?>
 //pieplot.setExplodePercent("A",0.3D);//炸開的餅圖,目前實現還有問題
 
 //DecimalFormat:
 //NumberFormat:
 //StandardPieSectionLabelGenerator:
 //setLabelGenerator():    
     DecimalFormat df = new DecimalFormat("0.00%");//獲得一個DecimalFormat對象,主要是設置小數問題
     NumberFormat nf = NumberFormat.getNumberInstance();//獲得一個NumberFormat對象
     StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator(
             "{0}{2}", nf, df);//獲得StandardPieSectionLabelGenerator對象
     pieplot.setLabelGenerator(sp);//設置餅圖顯示百分比
 
 //沒有數據的時候顯示的內容
     pieplot.setNoDataMessage("無數據顯示");
     pieplot.setCircular(false);
     pieplot.setLabelGap(0.02D);
 
     pieplot.setIgnoreNullValues(true);//設置不顯示空值
     pieplot.setIgnoreZeroValues(true);//設置不顯示負值
 
 //標題文字亂碼  IT行業職業分佈圖
     TextTitle textTitle = chart.getTitle();
     textTitle.setFont(new Font("宋體", Font.PLAIN, 20));
 
     //餅上的文字亂碼
     PiePlot plot = (PiePlot) chart.getPlot();
     plot.setLabelFont(new Font("宋體", Font.PLAIN, 20));
 
     //圖例文字亂碼 餅圖下面的5個說明
     chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 20));
 
     String filename = ServletUtilities.saveChartAsPNG(chart, 1000, 600,
             null, session);
     String graphURL = request.getContextPath()
             + "/DisplayChart?filename=" + filename;
 %>
 
 <html>
     <head>
         <title>餅狀圖2(加上百分比並突出顯示某塊)</title>
     </head>
     <body>
         <img src="<%=graphURL%>" width=1000 height=600 border=0
             usemap="#<%= filename %>">
     </body>
 </html>


bing2.jsp 一個水晶效果的拼圖

 <%@ page contentType="text/html;charset=GBK"%>
 
 <%@ page
     import="org.jfree.chart.*,org.jfree.chart.servlet.ServletUtilities,
     org.jfree.util.Rotation,org.jfree.data.general.DefaultPieDataset,
     org.jfree.chart.plot.PiePlot3D,org.jfree.chart.title.TextTitle,
     java.awt.Font,org.jfree.chart.plot.PiePlot"%>
 <%@ page
     import="org.jfree.chart.labels.StandardPieSectionLabelGenerator"%>
 <%@ page import="java.text.NumberFormat"%>
 <%@page import="java.text.DecimalFormat"%>
 <%
     //設置數據集
     DefaultPieDataset dataset = new DefaultPieDataset();
     dataset.setValue("初中高級程序員", 0.52);
     dataset.setValue("項目經理", 0.1);
     dataset.setValue("系統分析師", 0.1);
     dataset.setValue("軟件架構師", 0.1);
     dataset.setValue("其他", 0.18);
 
     //通過工廠類生成JFreeChart對象
     JFreeChart chart = ChartFactory.createPieChart3D("IT行業職業分佈圖",
             dataset, true, true, false);
     //獲得3D的水晶圖對象
     PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();
     PiePlot pieplot = (PiePlot) chart.getPlot();
     DecimalFormat df = new DecimalFormat("0.00%");//獲得一個DecimalFormat對象,主要是設置小數問題
     NumberFormat nf = NumberFormat.getNumberInstance();//獲得一個NumberFormat對象
     StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator(
             "{0}{2}", nf, df);//獲得StandardPieSectionLabelGenerator對象
     pieplot.setLabelGenerator(sp);//設置餅圖顯示百分比
    //設置開始角度
    pieplot3d.setStartAngle(150D);
    //設置方向爲“順時針方向”
    pieplot3d.setDirection(Rotation.CLOCKWISE);
 
    //設置透明度,0.5F爲半透明,1爲不透明,0爲全透明
    pieplot3d.setForegroundAlpha(0.5F);
    
     //沒有數據的時候顯示的內容
      pieplot3d.setNoDataMessage("無數據顯示");
     
      //標題文字亂碼  IT行業職業分佈圖
      TextTitle textTitle = chart.getTitle();
      textTitle.setFont(new Font("宋體", Font.PLAIN, 20));
      
      //餅上的文字亂碼
      PiePlot plot = (PiePlot) chart.getPlot();
     plot.setLabelFont(new Font("宋體", Font.PLAIN, 20));
     
      //圖例文字亂碼 餅圖下面的5個說明
      chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 20));
      
     String filename = ServletUtilities.saveChartAsPNG(chart, 1000, 600,
             null, session);
     String graphURL = request.getContextPath()
             + "/DisplayChart?filename=" + filename;
 %>
 
 <html>
     <head>
         <title>餅狀圖3(水晶餅圖)</title>
     </head>
     <body>
         <img src="<%=graphURL%>" width=1000 height=600 border=0
             usemap="#<%= filename %>">
     </body>
 </html>


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章