jfreechart統計図

18980 ワード

ネット上で多くのスペルを探して、自分もいくつかのものを追加したので、ここでは転載したリンク先をコピーしません.
まず、プロジェクトがstrutsを使用すると、webで使用されます.xmlもjspも加算.servlet、追加しないとそう見えます.Actionの形式なので、その時になると画像が表示されず、コードにこの点が明記されます.
以下のコードは自分でテストに合格しました.
1.バックグラウンドBarChartDemo、getDataSet 2メソッドには元の作者のコードが注記されています.ここで私が追加したコードとインポートしたパッケージを削除すればいいです.ここでDディスクとEディスクを直接実行すると画像が生成され、ganarateFruitChart()に2つの方法が書かれています

package com.jw.magicbpm.action.officebusinessmanage;

import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import com.jw.magicbpm.basecommon.db.DbSessionFactory;
import com.jw.magicbpm.model.officebusinessmanage.B_V_Ufp_PeopleAgeStatistics;

public class BarChartDemo {

	/**
	 *    
	 */
	public static String generatePieChart(HttpSession session, PrintWriter pw,
			int w, int h) {
		String filename = null;
		handleGarbledAndFont();//       
		PieDataset dataset = getDataSet();
		JFreeChart chart = ChartFactory.createPieChart3D("     ", //     
				dataset, //    
				true, //       
				false, //       
				false //     URL  
				);
		chart.setBackgroundPaint(Color.pink);
		try {
			/*------  chart     ----*/
			ChartRenderingInfo info = new ChartRenderingInfo(
					new StandardEntityCollection());
			filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,
					session);
			/*------  printWriter     ----*/
			ChartUtilities.writeImageMap(pw, filename, info, true);
			pw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return filename;
	}

	/**
	 *    
	 * @throws Exception 
	 */
	public static String generateBarChart(HttpSession session, PrintWriter pw,
			int w, int h) throws Exception {
		String filename = null;
		handleGarbledAndFont();//       
		CategoryDataset dataset = getDataSet2();
		JFreeChart chart = ChartFactory.createBarChart3D("       ", //     
				"  ", //         
				"  ", //         
				dataset, //    
				PlotOrientation.VERTICAL, //     :  、  
				true, //       (           false)
				false, //       
				false //     URL  
				);
		Font labelFont = new Font("SansSerif", Font.BOLD, 9);
		CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); 
		CategoryAxis domainAxis = categoryplot.getDomainAxis();
		domainAxis.setLabelFont(labelFont);// X        
	    domainAxis.setTickLabelFont(labelFont);//X         
	    //            
	    domainAxis.setLowerMargin(0.01); 
	    //            
	    domainAxis.setUpperMargin(0.01);
		try {
			/*------  chart     ----*/
			ChartRenderingInfo info = new ChartRenderingInfo(
					new StandardEntityCollection());
			filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,
					session);
			/*------  printWriter     ----*/
			ChartUtilities.writeImageMap(pw, filename, info, true);
			pw.flush();
		} catch (IOException e) {
			// TODO      catch  
			e.printStackTrace();
		}
		return filename;
	}

	/**
	 *    
	 */
	public static String generateLineChart(HttpSession session, PrintWriter pw,
			int w, int h) {
		String filename = null;
//		session.removeAttribute("JFreeChart_Deleter");// tomcat temp       
		handleGarbledAndFont();//       
		CategoryDataset dataset = getDataSet3();
		JFreeChart chart = ChartFactory.createLineChart("     ", //     
				"  ", //         
				"  ", //         
				dataset, //    
				PlotOrientation.VERTICAL, //     :  、  
				true, //       (           false)
				false, //       
				false //     URL  
				);

		/*----------           (      )--------------*/
		chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

		/*------------      --------------*/
		// 1,          
		chart.setBackgroundPaint(Color.pink);

		/*------------  Plot  -------------*/
		CategoryPlot plot = chart.getCategoryPlot();
		// 2,                  
		// plot.setBackgroundPaint(Color.PINK);
		// 3,         
		plot.setDomainGridlinePaint(Color.black);
		// 4,           
		plot.setDomainGridlinesVisible(true);
		// 5,         
		plot.setRangeGridlinePaint(Color.blue);
		// 6,           
		plot.setRangeGridlinesVisible(true);

		try {
			/*------  chart     ----*/
			ChartRenderingInfo info = new ChartRenderingInfo(
					new StandardEntityCollection());
			filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,
					session);
			/*------  printWriter     ----*/
			ChartUtilities.writeImageMap(pw, filename, info, true);
			pw.flush();
		} catch (IOException e) {
			// TODO      catch  
			e.printStackTrace();
		}
		return filename;
	}

	

	/**
	 *                
	 * 
	 * @return
	 */
	private static PieDataset getDataSet() {
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("  ", 100);
		dataset.setValue("  ", 200);
		dataset.setValue("  ", 300);
		dataset.setValue("  ", 400);
		dataset.setValue("  ", 500);
		return dataset;
	}

	/**
	 *             
	 * 
	 * @return
	 * @throws Exception 
	 */
	private static CategoryDataset getDataSet2() throws Exception {
		List<B_V_Ufp_PeopleAgeStatistics> list_B_V_Ufp_PeopleAgeStatistics1=PeopleAgeStatisticsAction.searchGovernmentLevelList(DbSessionFactory.getOracleSession());
		List<B_V_Ufp_PeopleAgeStatistics> list_B_V_Ufp_PeopleAgeStatistics2=PeopleAgeStatisticsAction.searchTownLevelList(DbSessionFactory.getOracleSession());
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(0).getUnderthirty(), "  ", "30     ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(0).getThirtyonetothirtyfive(), "  ", "31-35  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(0).getThirtysixtoforty(), "  ", "36-40   ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(0).getFortyonetofortyfive(), "  ", "41-45  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(0).getFortysixtofifty(), "  ", "46-50  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(0).getFiftyonetofiftyfive(), "  ", "51-55  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(0).getFiftysixtosixty(), "  ", "56-60  ");
		
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(1).getUnderthirty(), "   ", "30     ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(1).getThirtyonetothirtyfive(), "   ", "31-35  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(1).getThirtysixtoforty(), "   ", "36-40   ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(1).getFortyonetofortyfive(), "   ", "41-45  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(1).getFortysixtofifty(), "   ", "46-50  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(1).getFiftyonetofiftyfive(), "   ", "51-55  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics1.get(1).getFiftysixtosixty(), "   ", "56-60  ");
		
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(0).getUnderthirty(), "   ", "30     ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(0).getThirtyonetothirtyfive(), "   ", "31-35  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(0).getThirtysixtoforty(), "   ", "36-40   ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(0).getFortyonetofortyfive(), "   ", "41-45  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(0).getFortysixtofifty(), "   ", "46-50  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(0).getFiftyonetofiftyfive(), "   ", "51-55  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(0).getFiftysixtosixty(), "   ", "56-60  ");
		
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(1).getUnderthirty(), "  ", "30     ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(1).getThirtyonetothirtyfive(), "  ", "31-35  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(1).getThirtysixtoforty(), "  ", "36-40   ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(1).getFortyonetofortyfive(), "  ", "41-45  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(1).getFortysixtofifty(), "  ", "46-50  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(1).getFiftyonetofiftyfive(), "  ", "51-55  ");
		dataset.addValue(list_B_V_Ufp_PeopleAgeStatistics2.get(1).getFiftysixtosixty(), "  ", "56-60  ");
		
		
//		dataset.addValue(100, "  ", "  ");
//		dataset.addValue(500, "  ", "  ");
//		dataset.addValue(400, "  ", "  ");
//		dataset.addValue(200, "  ", "  ");
//		dataset.addValue(300, "  ", "  ");
//		dataset.addValue(500, "  ", "  ");
//		dataset.addValue(600, "  ", "  ");
//		dataset.addValue(400, "  ", "  ");
//		dataset.addValue(700, "  ", "  ");
//		dataset.addValue(300, "  ", "  ");
//		dataset.addValue(300, "  ", "  ");
//		dataset.addValue(200, "  ", "  ");
//		dataset.addValue(500, "  ", "  ");
//		dataset.addValue(100, "  ", "  ");
//		dataset.addValue(400, "  ", "  ");
		return dataset;
	}

	/**
	 *             
	 * 
	 * @return
	 */
	private static CategoryDataset getDataSet3() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(100, "  ", "  ");
		dataset.addValue(200, "  ", "  ");
		dataset.addValue(100, "  ", "  ");
		dataset.addValue(400, "  ", "  ");
		dataset.addValue(300, "  ", "  ");
		dataset.addValue(500, "  ", "  ");
		dataset.addValue(90, "  ", "  ");
		dataset.addValue(700, "  ", "  ");
		dataset.addValue(800, "  ", "  ");
		dataset.addValue(1000, "  ", "  ");
		dataset.addValue(300, "  ", "   ");
		dataset.addValue(700, "  ", "   ");
		dataset.addValue(1200, "  ", "  ");
		dataset.addValue(1100, "  ", "  ");
		dataset.addValue(1000, "  ", "  ");
		dataset.addValue(900, "  ", "  ");
		dataset.addValue(800, "  ", "  ");
		dataset.addValue(700, "  ", "  ");
		dataset.addValue(600, "  ", "  ");
		dataset.addValue(500, "  ", "  ");
		dataset.addValue(400, "  ", "  ");
		dataset.addValue(300, "  ", "  ");
		dataset.addValue(200, "  ", "   ");
		dataset.addValue(100, "  ", "   ");
		dataset.addValue(600, "  ", "  ");
		dataset.addValue(500, "  ", "  ");
		dataset.addValue(400, "  ", "  ");
		dataset.addValue(300, "  ", "  ");
		dataset.addValue(200, "  ", "  ");
		dataset.addValue(100, "  ", "  ");
		dataset.addValue(200, "  ", "  ");
		dataset.addValue(300, "  ", "  ");
		dataset.addValue(400, "  ", "  ");
		dataset.addValue(500, "  ", "  ");
		dataset.addValue(600, "  ", "   ");
		dataset.addValue(700, "  ", "   ");
		return dataset;
	}
	/**
	 *        
	 * @throws IOException 
	 */
	public static void handleGarbledAndFont(){
		//        
       StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  
       //        
       standardChartTheme.setExtraLargeFont(new Font("  ",Font.BOLD,20));  
       //         
       standardChartTheme.setRegularFont(new Font("  ",Font.PLAIN,15));  
       //         
       standardChartTheme.setLargeFont(new Font("  ",Font.PLAIN,15));  
       //        
       ChartFactory.setChartTheme(standardChartTheme);  
	}
	/**
	 *          
	 * @throws Exception 
	 * @throws IOException 
	 */
	public static void ganarateFruitChart() throws Exception{
		CategoryDataset dataset = getDataSet2();
		
	       JFreeChart chart = ChartFactory.createBarChart3D("     ", //     
				"  ", //         
				"  (  )", //         
				dataset, //    
				PlotOrientation.VERTICAL, //     :  、  
				true, //       (           false)
				false, //       
				false //     URL  
				);
		
		FileOutputStream fos_jpg = null;
		try {
			fos_jpg = new FileOutputStream("D:\\fruit.jpg");//      d 
			ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, 400, 300,
					null);
			//      E (      )
			ChartUtilities.saveChartAsPNG(new File("E:/BarChart3D.png"), chart, 800, 500);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos_jpg.close();
			} catch (Exception e) {
			}
		}
	}
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		ganarateFruitChart();
	}
}

2.jspページ、jsp:directive.Pageインポートを自分のパスに変更

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.io.PrintWriter"%>
<jsp:directive.page import="com.jw.magicbpm.action.officebusinessmanage.BarChartDemo"/>
<html>
<head>
<title>
</title>
<%
//   
String fileNamePie=BarChartDemo.generatePieChart(session,new PrintWriter(out),580,250);
//    strut     DisplayChart.servlet,  DisplayChart,      
String graphURLPie = request.getContextPath() + "/servlet/DisplayChart.servlet?filename=" + fileNamePie;
//   
String fileNameBar=BarChartDemo.generateBarChart(session,new PrintWriter(out),580,250);
String graphURLBar = request.getContextPath() + "/servlet/DisplayChart.servlet?filename=" + fileNameBar;
//   
String fileNameLine=BarChartDemo.generateLineChart(session,new PrintWriter(out),580,250);
String graphURLLine = request.getContextPath() + "/servlet/DisplayChart.servlet?filename=" + fileNameLine;

%>
</head>
<body bgcolor="#ffffff">
<table align="center" width="580" border="0" cellspacing="0" cellpadding="0">
 <tr>
    <td>
      <img src="<%= graphURLPie %>"width=580 height=250 border=0 >
    </td>
 </tr>
 <tr>
    <td>
      <img src="<%= graphURLBar %>"width=580 height=250 border=0 >
    </td>
 </tr>
 <tr>
    <td>
      <img src="<%= graphURLLine %>"width=580 height=250 border=0 >
    </td>
 </tr>
</table>
</body>
</html>

3.web.xmlに以下を加えると、DisplayChartにもservletを付けなければなりません.jspの中と一致します.

<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>
org.jfree.chart.servlet.DisplayChart
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart.servlet</url-pattern>
</servlet-mapping>

tomcatを使用すると、生成された一時画像はtomcatディレクトリのtempファイルにあり、ページに入るたびに画像が生成され、時間が経つとtomcatには無駄な画像が多くなります.何とかして削除しなければなりません.114行私が注釈したこの言葉session.removeAttribute("JFreeChart_Deleter");//tomcatのtempの統計画像を削除する
これは生成された一時画像を削除することであるが、このjspに3つの統計がある場合、バックグラウンドを走るたびに分けられている場合、バックグラウンドでは毎回sessionが実行されるという限界がある.removeAttribute(「JFreeChart_Deleter」)では、前の生成したピクチャを削除するたびに、3回バックグラウンドを走って最後のピクチャしかないので、できるだけ1回バックグラウンドを歩くか、バックグラウンドを1つの方法に統合するか、フロントページはurlになります.もし確かに得点していくつかの統計を使うならば、batスクリプトを使ってtempの中の臨時のピクチャーを削除することができます(個人はこの方法を推薦します)http://liangjie5305579-126-com.iteye.com/admin/blogs/1743824.