生成饼状图
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 }