jfreechart

生成餅狀圖java

 1 package com.kite.jfreechart;  2 
 3 import java.awt.Font;  4 import java.awt.Image;  5 import java.io.File;  6 import java.io.IOException;  7 
 8 import javax.imageio.ImageIO;  9 
10 import org.jfree.chart.ChartFactory; 11 import org.jfree.chart.ChartUtilities; 12 import org.jfree.chart.JFreeChart; 13 import org.jfree.chart.labels.StandardPieSectionLabelGenerator; 14 import org.jfree.chart.plot.PiePlot; 15 import org.jfree.chart.plot.Plot; 16 import org.jfree.data.general.DefaultPieDataset; 17 import org.junit.Test; 18 
19 /**
20  * 餅狀圖 21  */
22 public class App 23 { 24     
25     public static void main(String[] args) throws Exception 26  { 27         String title = "各大公司的J2EE AS市場佔有率統計"; 28         DefaultPieDataset ds = new DefaultPieDataset(); 29         ds.setValue("IBM", 2000); 30         ds.setValue("ORACLE", 3000); 31         ds.setValue("JBOOS", 4000); 32         ds.setValue("用友", 5000); 33         
34         //建立jfreechart
35         JFreeChart chart = ChartFactory.createPieChart3D(title, ds, true, false, false); 36         
37         //處理中文問題 大標題
38         chart.getTitle().setFont(new Font("宋體", Font.BOLD, 25)); 39         chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 20)); 40         
41         //繪圖區
42         PiePlot plot = (PiePlot) chart.getPlot(); 43         //設置繪圖區中標籤的字體
44         plot.setLabelFont(new Font("宋體", Font.BOLD, 18)); 45         
46         //設置背景 47         //chart.setBackgroundImage(ImageIO.read(new File("D:\\系統文檔\\Desktop\\model.png"))); 48         //plot.setBackgroundImage(ImageIO.read(new File("D:\\系統文檔\\Desktop\\model.png"))); 49         //設置分裂效果 Explode 爆炸 percent 按照百分比
50         plot.setExplodePercent("IBM", 0.2f); 51         plot.setExplodePercent("用友", 0.1f); 52         
53         //設置前景色透明度
54         plot.setForegroundAlpha(0.5f); 55         //設置標籤生成器 56         //{0}:公司名稱 57         //{1}:銷量 58         //{2}:百分比 59         //{3}:總量 60         //{4}:
61         plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}/{3}-{2})")); 62         //保存圖片
63         ChartUtilities.saveChartAsJPEG(new File("f:\\pie.jpg"), chart, 800, 600); 64  } 65     
66 }

生成柱狀圖
 1 package com.kite.jfreechart;  2 
 3 import java.awt.Font;  4 import java.io.File;  5 
 6 import org.jfree.chart.ChartFactory;  7 import org.jfree.chart.ChartUtilities;  8 import org.jfree.chart.JFreeChart;  9 import org.jfree.chart.plot.CategoryPlot; 10 import org.jfree.chart.plot.Plot; 11 import org.jfree.chart.plot.PlotOrientation; 12 import org.jfree.data.category.DefaultCategoryDataset; 13 
14 /**
15  * 生成柱狀圖 16  */
17 public class AppBar 18 { 19     public static void main(String[] args) throws Exception 20  { 21         
22         DefaultCategoryDataset ds = new DefaultCategoryDataset(); 23         
24         ds.setValue(2000, "IBM", "第一季度"); 25         ds.setValue(3000, "Oracle", "第一季度"); 26         ds.setValue(4000, "JBOOS", "第一季度"); 27         ds.setValue(5000, "用友", "第一季度"); 28         
29         ds.setValue(5000, "IBM", "第二季度"); 30         ds.setValue(4000, "Oracle", "第二季度"); 31         ds.setValue(3000, "JBOOS", "第二季度"); 32         ds.setValue(2000, "用友", "第二季度"); 33         
34         ds.setValue(1000, "IBM", "第三季度"); 35         ds.setValue(2000, "Oracle", "第三季度"); 36         ds.setValue(2500, "JBOOS", "第三季度"); 37         ds.setValue(1500, "用友", "第三季度"); 38         
39         String title = "前三季度各大公司j2ee as銷量統計 "; 40         
41         JFreeChart chart = ChartFactory.createBarChart3D(title, "季度", "銷量(單位:臺)", ds, PlotOrientation.VERTICAL, true, true, false); 42         
43         //處理中文
44         chart.getTitle().setFont(new Font("宋體",Font.BOLD,25)); 45         
46         //提示條
47         chart.getLegend().setItemFont(new Font("宋體",Font.PLAIN,20)); 48         
49         CategoryPlot plot = (CategoryPlot) chart.getPlot(); 50         
51         //域軸字體
52         plot.getDomainAxis().setLabelFont(new Font("宋體",Font.PLAIN,18)); 53         plot.getDomainAxis().setTickLabelFont(new Font("宋體",Font.PLAIN,15));//小標籤字體 54         
55         //range
56         plot.getRangeAxis().setLabelFont(new Font("宋體",Font.PLAIN,18)); 57         ChartUtilities.saveChartAsJPEG(new File("f:\\bar.jpg"), chart, 800, 600); 58         
59  } 60 }

顯示折線圖字體

 1 package com.kite.jfreechart;  2 
 3 import java.awt.Font;  4 import java.io.File;  5 
 6 import org.jfree.chart.ChartFactory;  7 import org.jfree.chart.ChartUtilities;  8 import org.jfree.chart.JFreeChart;  9 import org.jfree.chart.plot.CategoryPlot; 10 import org.jfree.chart.plot.Plot; 11 import org.jfree.chart.plot.PlotOrientation; 12 import org.jfree.data.category.DefaultCategoryDataset; 13 
14 /**
15  * 現狀圖 16  */
17 public class AppLine 18 { 19     public static void main(String[] args) throws Exception 20  { 21         DefaultCategoryDataset ds = new DefaultCategoryDataset(); 22         ds.setValue(2000, "IBM", "第一季度"); 23         ds.setValue(3000, "Oracle", "第一季度"); 24         ds.setValue(4000, "JBOOS", "第一季度"); 25         ds.setValue(5000, "用友", "第一季度"); 26         
27         ds.setValue(5000, "IBM", "第二季度"); 28         ds.setValue(4000, "Oracle", "第二季度"); 29         ds.setValue(3000, "JBOOS", "第二季度"); 30         ds.setValue(2000, "用友", "第二季度"); 31         
32         ds.setValue(1000, "IBM", "第三季度"); 33         ds.setValue(2000, "Oracle", "第三季度"); 34         ds.setValue(2500, "JBOOS", "第三季度"); 35         ds.setValue(1500, "用友", "第三季度"); 36         
37         String title = "前三季度各大公司j2ee as銷量統計 "; 38         
39         JFreeChart chart = ChartFactory.createLineChart(title, "季度", "銷量(單位:臺)", ds, PlotOrientation.VERTICAL, true, true, false); 40         
41         //處理中文
42         chart.getTitle().setFont(new Font("宋體",Font.BOLD,25)); 43         
44         //提示條
45         chart.getLegend().setItemFont(new Font("宋體",Font.PLAIN,20)); 46         
47         CategoryPlot plot = (CategoryPlot) chart.getPlot(); 48         
49         //域軸字體
50         plot.getDomainAxis().setLabelFont(new Font("宋體",Font.PLAIN,18)); 51         plot.getDomainAxis().setTickLabelFont(new Font("宋體",Font.PLAIN,15));//小標籤字體 52         
53         //range
54         plot.getRangeAxis().setLabelFont(new Font("宋體",Font.PLAIN,18)); 55         ChartUtilities.saveChartAsJPEG(new File("f:\\line.jpg"), chart, 800, 600); 56         
57  } 58 }
相關文章
相關標籤/搜索