· 一:jfreechart介紹 jfreechart是一個免費創(chuàng)建圖片的java工具.可以創(chuàng)建如下圖形: 餅圖(pie charts;) 曲線圖(line charts ) 柱狀圖(horizontal/vertical bar charts) 甘特圖(Gantt charts; ) XY plots and scatter plots; time series, high/low/open/close charts and candle stick charts; combination charts; Pareto charts; bubble charts; wind plots, meter charts and symbol charts; 從以下地址可以看到j(luò)freechart可以創(chuàng)建的圖形類型 http://www./jfreechart/samples.html sourceforge有一個基于jfreechart的項目Cewolf可以很方便的在jsp/servlet中創(chuàng)建圖片 jfreechart目前(2003-05-08)版本為0.98 希望得到詳細的信息或下載jfreechart請訪問如下站點: http://www./jfreechart/ · 二:特別說明: jfreechart是一個開源項目,但是文檔是需要40美金去購買的。 還有一個很重要的問題,jfreechart如果使用中文,他使用的默認字體 顯示出來的中文會很模糊,你可能需要修改源代碼。 下面我就舉幾個簡單的例子說明一下如何使用jfreechart創(chuàng)建圖片 在開發(fā)中有可能會導(dǎo)入以下的類 import com.jrefinery.chart.ChartFactory; import com.jrefinery.chart.ChartUtilities; import com.jrefinery.chart.JFreeChart; import com.jrefinery.chart.TextTitle; import com.jrefinery.chart.axis.NumberAxis; import com.jrefinery.chart.plot.CategoryPlot; import com.jrefinery.chart.plot.PiePlot; import com.jrefinery.data.Day; import com.jrefinery.data.DefaultCategoryDataset; import com.jrefinery.data.DefaultPieDataset; import com.jrefinery.data.TimeSeries; import com.jrefinery.data.TimeSeriesCollection; import com.jrefinery.data.TimeSeriesDataPair; 在0.98以后包由com.jrefinery.*改變?yōu)?org.jfree · 三:創(chuàng)建餅圖 //圖片標題 String title = "空調(diào)2002年市場占有率"; //設(shè)定數(shù)據(jù)源 DefaultPieDataset piedata = new DefaultPieDataset(); //第一個參數(shù)為名稱,第二個參數(shù)是double數(shù) piedata.setValue("聯(lián)想", 27.3); piedata.setValue("長城", 12.2); piedata.setValue("海爾", 5.5); piedata.setValue("美的", 17.1); piedata.setValue("松下", 9.0); piedata.setValue("科龍", 19.0); //創(chuàng)建JFreeChart,都使用ChartFactory來創(chuàng)建JFreeChart,很標準的工廠設(shè)計模式 JFreeChart chart = ChartFactory.createPieChart(title, piedata, true, true, true); //設(shè)定圖片標題 chart.setTitle(new TextTitle(title, new Font("隸書", Font.99vALIC, 15))); //chart.addSubtitle(new TextTitle("2002財年分析", new Font("隸書", Font.99vALIC, 12))); //設(shè)定背景 chart.setBackgroundPaint(Color.white); //chart.s //餅圖使用一個PiePlot PiePlot pie = (PiePlot)chart.getPlot(); //pie.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS); pie.setSectionLabelType(PiePlot.NAME_AND_VALUE_LABELS); //設(shè)定顯示格式(名稱加百分比或數(shù)值) pie.setPercentFormatString("#,###0.0#%"); //設(shè)定百分比顯示格式 pie.setBackgroundPaint(Color.white); pie.setSectionLabelFont(new Font("黑體", Font.TRUETYPE_FONT, 12)); //設(shè)定背景透明度(0-1.0之間) pie.setBackgroundAlpha(0.6f); //設(shè)定前景透明度(0-1.0之間) pie.setForegroundAlpha(0.90f); //輸出文件到指定目錄 String rfname = MathUtil.getRoundCode(12) + ".jpeg"; String fileName = "d:/test/" + rfname; try { //可以保存文件為jpg或png格式。 ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600); //第一個參數(shù)為文件名 //第二個參數(shù)質(zhì)量 //第三個參數(shù)為哪個chart創(chuàng)建圖片 //第四個寬度 //第五個高度 } catch (IOException exz) { System.out.print("....Cant´t Create image File"); } 其實使用JFreeChart創(chuàng)建圖片很簡單,不同的的圖片類型區(qū)別在于設(shè)置數(shù)據(jù)集。 |
|