⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdgenimage.java

📁 一个使用JFreeChart的例子
💻 JAVA
字号:

import java.lang.*;
import java.io.*;
import java.util.*;


import java.awt.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart; 
//import org.jfree.chart.StandardLegend; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.plot.CategoryPlot; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.renderer.category.LineAndShapeRenderer; 
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.util.*;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset; 

import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;


//import org.jdom.Attribute;
/*
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
*/

public class RDGenImage {

	/**
	 * @param args
	 */
	public String[] arrSeries;
	public int[] arrAxis_X;
	public Number[] arryPieVal;
	public Number[][] arrAxis_Y;
	public String strXName="";
	public String strYName="";
	public String strImgTitle="";
	public String strImgFolder="";
	public String strImgName="";
	
	public RDGenImage()
	{
		
	}
	/*
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.printf("dddd");
		GenLineImage();
	}
	*/
	public void TestIO()
	{
	}
	public void SetSeriesNumber(int nSeriesNum)
	{
		this.arrSeries = new String[nSeriesNum];
		arryPieVal= new Number[nSeriesNum];
		System.out.println("Set Series Number");
		System.out.println(nSeriesNum);
	}
	public void SetSeries(int i, String strSeries)
	{
			System.out.println("Set Series:");
			System.out.println(i);
			System.out.println(strSeries);
		  this.arrSeries[i] = strSeries;
		  System.out.println(arrSeries[i]);

		
	}
	public void SetAxisX(int[] _arrAxis_X)
	{
		this.arrAxis_X = _arrAxis_X;
		for(int i=0; i<arrAxis_X.length;i++)
			System.out.println(arrAxis_X[i]);
	}
	
	public void SetPieValue(int[] _arrPieValue)
	{
		for(int i=0; i<_arrPieValue.length;i++)
			arryPieVal[i] = Float.valueOf(Integer.toString(_arrPieValue[i])).floatValue();
	}
	
	public void SetAxisYRange(int x, int y)
	{
		arrAxis_Y = new Number[x][y];	
		System.out.println(x);
		System.out.println(y);
			
	}
	public void SetAxisY(int x, int[] _arrAxis_Y)
	{
		/*
		System.out.println("Print original data");
		for(int k=0; k<_arrAxis_Y.length;k++)
			System.out.println(_arrAxis_Y[k]);
		*/	
		//System.out.println("Print original datasdddd");
		for(int i=0;i<_arrAxis_Y.length;i++)
		{
			
				this.arrAxis_Y[x][i] = Float.valueOf(Integer.toString(_arrAxis_Y[i])).floatValue();
				//System.out.println(arrAxis_Y[x][i]);
		}
	}
	public void SetImgTitle(String strImgTitle)
	{
		this.strImgTitle = strImgTitle;
	}
	public void SetXName(String strXName)
	{
		this.strXName=strXName;
	}
	public void SetYName(String strYName)
	{
		this.strYName=strYName;
	}
	public void SetImgFolder(String strImgFolder)
	{
		this.strImgFolder = strImgFolder;
	}
	public void SetImgName(String strImgName)
	{
		this.strImgName = strImgName;
		System.out.println("Image name:" + strImgName);
	}

	public void GenLineImage()
	{
		
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(int i=0; i<arrSeries.length;i++)
		{
			for(int j=0; j<arrAxis_X.length;j++)
			{
				dataset.addValue(arrAxis_Y[i][j], arrSeries[i], arrAxis_X[j]);
			}
		}
	     
	   // create the chart... 
	   //JFreeChart chart;
	     
	   JFreeChart chart = ChartFactory.createLineChart( 
			   		strImgTitle,       // chart title 
				    strXName,                    // domain axis label 
				    strYName,                   // range axis label 
				   dataset,                   // data 
				   PlotOrientation.VERTICAL,  // orientation 
				   true,                      // include legend 
				   true,                      // tooltips 
				   false                      // urls 
				   ); 
   
    String sFont = "SimSun";
		chart.setBackgroundPaint(Color.white);
		chart.getTitle().setFont(new Font(sFont,Font.BOLD,14));
		
		CategoryPlot categoryplot = (CategoryPlot)chart.getPlot();
		categoryplot.setBackgroundPaint(Color.white);
		categoryplot.setRangeGridlinePaint(Color.red);
		
		
		NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		numberaxis.setAutoRangeIncludesZero(true);
		//获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
		LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
		//lineandshaperenderer.setShapesVisible(true);
		//lineandshaperenderer.setShapesVisible(true); //series 点(即数据点)可见
		//lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[]{10F, 6F}, 0.0F)); 
		//定义series为"First"的(即series1)点之间的连线 ,这里是虚线,默认是直线
		//lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[]{6F, 6F}, 0.0F)); 
		//定义series为"Second"的(即series2)点之间的连线
		//lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[]{2.0F, 6F}, 0.0F)); 
		//定义series为"Third"的(即series3)点之间的连线
		//return jfreechart;
        

	      // renderer.setItemLabelsVisible(true);           
	       
	       try {
				// Write the chart image to the temporary directory
				ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

		//		File pngDir = new File(PNG_DIR);
		//		File pngFile = File.createTempFile("rptPie",".png",pngDir);
				String fsep = System.getProperty("file.separator");
				File pngFile = new File(strImgFolder + strImgName);
//				String filename = pngFile.getName();
				ChartUtilities.saveChartAsPNG(pngFile, chart, 500, 300, info);
			} catch(Exception e) {
				System.out.println("PieChart::plotChart: " +
						"Problem occurred creating chart.");
				//errCode = -2;
			}
	  
	}
	
	public void GenPieImage()
	{
			String title = "2005 搜索引擎市场占有率";//数据为测试数据
 
			DefaultPieDataset piedata = new DefaultPieDataset();
			
			//第一个参数为名称,第二个参数是double数
			for(int i=0; i<arrSeries.length;i++)
				piedata.setValue(arrSeries[i],arryPieVal[i]);
			
			//3D饼图
			PiePlot3D plot = new PiePlot3D(piedata); 
			//PiePlot3D.setNoDataMessage("无数据显示");
			
			//设定链接 ("link.jsp","section"));//section为参数,不填写则默认为category
			//plot.setURLGenerator(new StandardPieURLGenerator("link.jsp","section"));
			
			//指定图片的透明度
			plot.setForegroundAlpha(1);//0.5f);
			//plot.setOutlinePaint(java.awt.Color.white);
			//plot.setBackgroundPaint(java.awt.Color.white);
			
			//指定显示的饼图上圆形(false)还椭圆形(true)
			plot.setCircular(false);
			
			//显示百分比
			//plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieSectionLabelGenerator.DEFAULT_TOOLTIP_FORMAT));
			plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}: ({1}M, {2})"));
			
			JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true); 
			//设置图片背景色
			chart.setBackgroundPaint(java.awt.Color.white); 
			
			//设置图片标题属性
			Font font = new Font("SimSun",Font.CENTER_BASELINE,20);
			TextTitle _title = new TextTitle(strImgTitle);
			_title.setFont(font);
			_title.setPaint(java.awt.Color.black);
			chart.setTitle(_title); 
			
			//plot.setToolTipGenerator(new StandardPieSectionLabelGenerator());
			 
 			try {
				// Write the chart image to the temporary directory
				ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

				String fsep = System.getProperty("file.separator");
				File pngFile = new File(strImgFolder + strImgName);
				
				//500是图片长度,300是图片高度,session 为HttpSession对象
				ChartUtilities.saveChartAsPNG(pngFile, chart, 500, 300, info);
			} catch(Exception e) {
				System.out.println("PieChart::plotChart: " + "Problem occurred creating chart.");
				//errCode = -2;
			}
	  
 
		
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -