一、餅圖
代碼
/**<br /> * ClassName: PieChartTest.java<br /> * created on 2008-12-21<br /> * Copyrights 2008 qjyong All rights reserved.<br /> * EMail: qjyong@gmail.com<br /> */<br />package test;</p><p>import java.awt.BasicStroke;<br />import java.awt.Color;<br />import java.awt.Font;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;<br />import java.io.IOException;<br />import java.text.DecimalFormat;<br />import java.text.NumberFormat;</p><p>import org.jfree.chart.ChartFactory;<br />import org.jfree.chart.ChartFrame;<br />import org.jfree.chart.ChartUtilities;<br />import org.jfree.chart.JFreeChart;<br />import org.jfree.chart.labels.StandardPieSectionLabelGenerator;<br />import org.jfree.chart.labels.StandardPieToolTipGenerator;<br />import org.jfree.chart.plot.PiePlot;<br />import org.jfree.chart.title.TextTitle;<br />import org.jfree.chart.urls.StandardPieURLGenerator;<br />import org.jfree.data.general.DefaultPieDataset;<br />import org.jfree.data.general.PieDataset;<br />import org.jfree.util.Rotation;</p><p>/**<br /> * 使用JFreeChart繪製餅圖<br /> * @author qiujy<br /> */<br />public class PieChartTest {</p><p>/**<br /> * step1:建立資料集對象<br /> * @return<br /> */<br />public static PieDataset createDataSet() {<br />DefaultPieDataset dataset = new DefaultPieDataset();<br />dataset.setValue("java程式設計語言", 10000);<br />dataset.setValue("JSP基礎與案例開發詳解", 20000);<br />dataset.setValue("struts基礎與案例開發詳解", 30000);<br />dataset.setValue("精通JSF", 40000);</p><p>return dataset;<br />}</p><p>/**<br /> * step2:建立圖表<br /> * @param dataset<br /> * @return<br /> */<br />public static JFreeChart createChart(PieDataset dataset) {<br /> JFreeChart chart = ChartFactory.createPieChart3D(<br />//JFreeChart chart = ChartFactory.createPieChart(<br />"原創圖書銷量統計", // 圖表標題<br />dataset, // 資料集<br />true, // 是否顯示圖例<br />true, // 是否顯示工具提示<br />true // 是否產生URL<br />);</p><p>//設定標題字型==為了防止中文亂碼:必須設定字型<br />chart.setTitle(new TextTitle("原創圖書銷量統計", new Font("黑體", Font.ITALIC, 22)));<br />//設定圖例的字型==為了防止中文亂碼:必須設定字型<br />chart.getLegend().setItemFont(new Font("黑體", Font.BOLD, 12));<br />// 擷取餅圖的Plot對象(實際圖表)<br />PiePlot plot = (PiePlot) chart.getPlot();<br />//圖形邊框顏色<br />plot.setBaseSectionOutlinePaint(Color.GRAY);<br />//圖形邊框粗細<br />plot.setBaseSectionOutlineStroke(new BasicStroke(0.0f));<br />//設定餅狀圖的繪製方向,可以按順時針方向繪製,也可以按逆時針方向繪製<br />plot.setDirection(Rotation.ANTICLOCKWISE);<br />//設定繪製角度(圖形旋轉角度)<br />plot.setStartAngle(70);<br />//設定反白的資料區塊<br />plot.setExplodePercent("One", 0.1D);<br />//設定背景色透明度<br />plot.setBackgroundAlpha(0.7F);<br />// 設定前景色彩透明度<br />plot.setForegroundAlpha(0.65F);<br />//設定區塊標籤的字型==為了防止中文亂碼:必須設定字型<br />plot.setLabelFont(new Font("隸書", Font.PLAIN, 12));<br />// 扇區分離顯示,對3D圖不起效<br />plot.setExplodePercent(dataset.getKey(3), 0.1D);<br />// 圖例顯示百分比:自訂方式,{0} 表示選項, {1} 表示數值, {2} 表示所佔比例 ,小數點後兩位<br />plot.setLabelGenerator(new StandardPieSectionLabelGenerator(<br />"{0}:{1}/r/n({2})", NumberFormat.getNumberInstance(),<br />new DecimalFormat("0.00%")));<br />// 圖例顯示百分比<br />// plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));<br />// 指定顯示的餅圖為:圓形(true) 還是橢圓形(false)<br />plot.setCircular(false);<br />// 沒有資料的時候顯示的內容<br />plot.setNoDataMessage("找不到可用資料..."); </p><p>//設定滑鼠移至上方提示<br />plot.setToolTipGenerator(new StandardPieToolTipGenerator());<br />//設定熱點連結<br />plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));</p><p>return chart;<br />}</p><p>/**<br /> * step3: 輸出圖表到Swing Frame<br /> * @param chart<br /> */<br />public static void drawToFrame(JFreeChart chart){<br />//輸出圖表到Swing Frame<br />ChartFrame frame = new ChartFrame("原創圖書銷量統計", chart);<br />frame.pack();<br />frame.setVisible(true);<br />}</p><p>/**<br /> * step3: 輸出圖表到指定的磁碟<br /> * @param destPath<br /> * @param chart<br /> */<br />public static void drawToOutputStream(String destPath, JFreeChart chart) {<br />FileOutputStream fos = null;<br />try {<br />fos = new FileOutputStream(destPath);<br />// ChartUtilities.writeChartAsJPEG(<br />ChartUtilities.writeChartAsPNG(fos, // 指定目標輸出資料流<br />chart, // 圖表對象<br />600, // 寬<br />400, // 高<br />null); // ChartRenderingInfo資訊<br />} catch (IOException e) {<br />e.printStackTrace();<br />} finally {<br />try {fos.close();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}<br />}<br />}</p><p>public static void main(String[] args) throws FileNotFoundException {<br />// step1:建立資料集對象<br />PieDataset dataset = createDataSet();</p><p>// step2:建立圖表<br />JFreeChart chart = createChart(dataset);</p><p>// step3: 輸出圖表到Swing視窗<br />//drawToFrame(chart);</p><p>// step3: 輸出圖表到磁碟<br />drawToOutputStream("D://mybook-pie.png", chart);<br />}<br />}<br />
產生的圖
二、餅圖
代碼
/**<br /> * ClassName: BarChartTest.java<br /> * created on 2008-12-21<br /> * Copyrights 2008 qjyong All rights reserved.<br /> * EMail: qjyong@gmail.com<br /> */<br />package test;</p><p>import java.awt.Color;<br />import java.awt.Font;<br />import java.io.FileNotFoundException;<br />import java.io.FileOutputStream;<br />import java.io.IOException;</p><p>import org.jfree.chart.ChartFactory;<br />import org.jfree.chart.ChartFrame;<br />import org.jfree.chart.ChartUtilities;<br />import org.jfree.chart.JFreeChart;<br />import org.jfree.chart.axis.CategoryAxis;<br />import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;<br />import org.jfree.chart.plot.CategoryPlot;<br />import org.jfree.chart.plot.PlotOrientation;<br />import org.jfree.chart.renderer.category.BarRenderer;<br />import org.jfree.chart.title.LegendTitle;<br />import org.jfree.chart.title.TextTitle;<br />import org.jfree.data.category.CategoryDataset;<br />import org.jfree.data.category.DefaultCategoryDataset;</p><p>/**<br /> * 柱狀圖和折線圖<br /> * @author qiujy<br /> */<br />public class BarChartTest {<br />/**<br /> * step1:建立 簡單資料集對象<br /> * @return<br /> */<br />public static CategoryDataset createDataSet() {<br />DefaultCategoryDataset dataset = new DefaultCategoryDataset();<br />dataset.setValue(10000, "","Corejava");<br />dataset.setValue(20000, "","JavaWeb");<br />dataset.setValue(30000, "","易用struts");<br />dataset.setValue(40000, "","精通JSF");</p><p>return dataset;<br />}</p><p>/**<br /> * 組合資料集對象<br /> * @return<br /> */<br />public static CategoryDataset createDataSet2() {<br />DefaultCategoryDataset dataset = new DefaultCategoryDataset();<br />dataset.setValue(5000, "北京","Corejava");<br />dataset.setValue(3000, "上海","Corejava");<br />dataset.setValue(2000, "廣州","Corejava");</p><p>dataset.setValue(10000, "北京","JavaWeb");<br />dataset.setValue(6000, "上海","JavaWeb");<br />dataset.setValue(4000, "廣州","JavaWeb");</p><p>dataset.setValue(15000, "北京","易用struts");<br />dataset.setValue(5000, "上海","易用struts");<br />dataset.setValue(10000, "廣州","易用struts");</p><p>dataset.setValue(20000, "北京","精通JSF");<br />dataset.setValue(10000, "上海","精通JSF");<br />dataset.setValue(10000, "廣州","精通JSF");</p><p>return dataset;<br />}</p><p>/**<br /> * step2:建立圖表<br /> * @param dataset<br /> * @return<br /> */<br />public static JFreeChart createChart(CategoryDataset dataset) {<br />JFreeChart chart = ChartFactory.createBarChart3D(//3D柱狀圖<br />//JFreeChart chart = ChartFactory.createLineChart3D( //3D折線圖<br />"原創圖書銷量統計", //圖表的標題<br />"圖書名", //目錄軸的顯示標籤<br />"銷量", //數值軸的顯示標籤<br />dataset, //資料集<br />PlotOrientation.VERTICAL, //圖表方式:V垂直;H水平<br />true, // 是否顯示圖例<br />false, // 是否顯示工具提示<br />false // 是否產生URL<br />);</p><p>//===============為了防止中文亂碼:必須設定字型<br />chart.setTitle(new TextTitle("原創圖書銷量統計", new Font("黑體", Font.ITALIC, 22)));</p><p>LegendTitle legend = chart.getLegend(); // 擷取圖例<br />legend.setItemFont(new Font("宋體", Font.BOLD, 12)); //設定圖例的字型,防止中文亂碼</p><p>CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 擷取柱圖的Plot對象(實際圖表)<br />// 設定柱圖背景色(注意,系統取色的時候要使用16位的模式來查看顏色編碼,這樣比較準確)<br />plot.setBackgroundPaint(new Color(255, 255, 204));<br />plot.setForegroundAlpha(0.65F); //設定前景色彩透明度</p><p>// 設定橫虛線可見<br />plot.setRangeGridlinesVisible(true);<br />// 虛線色彩<br />plot.setRangeGridlinePaint(Color.gray);</p><p>CategoryAxis h = plot.getDomainAxis(); //擷取x軸<br />h.setMaximumCategoryLabelWidthRatio(1.0f);// 橫軸上的 Lable 是否完整顯示<br />h.setLabelFont(new Font("宋體", Font.BOLD, 12));//設定字型,防止中文亂碼<br />h.setTickLabelFont(new Font("宋體", Font.BOLD, 12));// 軸數值<br />//h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度傾斜</p><p>plot.getRangeAxis().setLabelFont(new Font("宋體", Font.BOLD, 12)); //Y軸設定字型,防止中文亂碼</p><p>//柱圖的轉譯器<br />BarRenderer renderer = new BarRenderer();<br />// 設定柱子寬度<br />//renderer.setMaximumBarWidth(0.05);<br />// 設定柱子高度<br />//renderer.setMinimumBarLength(0.2);<br />// 設定柱子邊框顏色<br />renderer.setBaseOutlinePaint(Color.BLACK);<br />// 設定柱子邊框可見<br />renderer.setDrawBarOutline(true);<br />//設定每個柱的顏色<br />renderer.setSeriesPaint(0, Color.BLUE);<br />renderer.setSeriesPaint(1, Color.GREEN);<br />renderer.setSeriesPaint(2, Color.RED);<br />//設定每個地區所包含的平行柱的之間距離<br />renderer.setItemMargin(0.05);<br />// 顯示每個柱的數值,並修改該數值的字型屬性<br />renderer.setIncludeBaseInRange(true);<br />renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());<br />renderer.setBaseItemLabelsVisible(true);<br />// 設定柱的透明度<br />plot.setForegroundAlpha(1.0f);<br />//給柱圖添加轉譯器<br />plot.setRenderer(renderer); </p><p>// 沒有資料的時候顯示的內容<br />plot.setNoDataMessage("找不到可用資料...");</p><p>return chart;<br />}</p><p>/**<br /> * step3: 輸出圖表到Swing Frame<br /> * @param chart<br /> */<br />public static void drawToFrame(JFreeChart chart){<br />//輸出圖表到Swing Frame<br />ChartFrame frame = new ChartFrame("原創圖書銷量統計", chart);<br />frame.pack();<br />frame.setVisible(true);<br />}</p><p>/**<br /> * step3: 輸出圖表到指定的磁碟<br /> * @param destPath<br /> * @param chart<br /> */<br />public static void drawToOutputStream(String destPath, JFreeChart chart) {<br />FileOutputStream fos = null;<br />try {<br />fos = new FileOutputStream(destPath);<br />// ChartUtilities.writeChartAsJPEG(<br />ChartUtilities.writeChartAsPNG(fos, // 指定目標輸出資料流<br />chart, // 圖表對象<br />600, // 寬<br />500, // 高<br />null); // ChartRenderingInfo資訊<br />} catch (IOException e) {<br />e.printStackTrace();<br />} finally {<br />try {<br />fos.close();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}<br />}<br />}</p><p>public static void main(String[] args) throws FileNotFoundException {<br />// step1:建立資料集對象<br />CategoryDataset dataset = createDataSet2();</p><p>// step2:建立餅圖<br />JFreeChart chart = createChart(dataset);</p><p>// step3: 輸出圖表到Swing視窗<br />//drawToFrame(chart);</p><p>// step3: 輸出圖表到磁碟<br />drawToOutputStream("D://mybook-bar.png", chart);<br />}</p><p>}<br />
產生的圖
三、一些沒有保密要求的圖表,可以直接使用Google圖表API(http://code.google.com/intl/zh-CN/apis/chart/)來產生,更簡單更直接:
如:請求連結為http://chart.apis.google.com/chart?cht=p3&chco=0000ff&chd=t:40,30,20,10&chs=500x250&chl=java|jsp|ssh|ejb
產生的圖片為
其它圖表可參看Google提供的API