JFreeChart学习笔记
饼图: 1 package com.xjz; 2 3 import java.awt.Font; 4 5 import org.jfree.chart.ChartFactory; 6 import org.jfree.chart.ChartFrame; 7 import org.jfree.chart.JFreeChart; 8 import org.jfree.chart.axis.CategoryAxis3D; 9 import org.jfree.chart.axis.NumberAxis3D; 10 import org.jfree.chart.axis.NumberTickUnit; 11 import org.jfree.chart.labels.CategoryItemLabelGenerator; 12 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 13 import org.jfree.chart.plot.CategoryPlot; 14 import org.jfree.chart.plot.PlotOrientation; 15 import org.jfree.chart.renderer.category.BarRenderer3D; 16 import org.jfree.data.category.CategoryDataset; 17 import org.jfree.data.category.DefaultCategoryDataset; 18 19 public class PieDemo { 20 21 public static void main(String[] args) { 22 CategoryDataset dataset = getDataSet(); 23 JFreeChart chart = ChartFactory.createBarChart3D("客户分类统计", 24 "客户等级", //X轴 25 "数量", //Y轴 26 dataset ,//图表需要的数据 27 PlotOrientation.VERTICAL, //图表的方向 28 true, //是否显示图例 29 true, //是否显示工具提示 30 true //是否生成URL链接地址 31 ); 32 33 //处理乱码 34 //1.主标题 35 chart.getTitle().setFont(new Font("宋体",Font.BOLD,18)); 36 //2.子标题 37 chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,18)); 38 //获取图表区对象 39 CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot(); 40 //3.X轴 41 CategoryAxis3D categoryAxis3D = (CategoryAxis3D) categoryPlot.getDomainAxis(); 42 //4.Y轴 43 NumberAxis3D numberAxis3D = (NumberAxis3D) categoryPlot.getRangeAxis(); 44 //处理X轴乱码 45 categoryAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15)); 46 categoryAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15)); 47 //处理Y轴乱码 48 numberAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15)); 49 numberAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15)); 50 51 //处理Y轴的刻度 52 //设置自动刻度为false 53 numberAxis3D.setAutoTickUnitSelection(false); 54 numberAxis3D.setTickUnit(new NumberTickUnit(1)); 55 56 //处理图形的宽度 57 //1.获取绘图区对象 58 BarRenderer3D barRenderer3D =(BarRenderer3D) categoryPlot.getRenderer(); 59 barRenderer3D.setMaximumBarWidth(0.08); 60 61 CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator(); 62 // 处理柱体上的数字 63 barRenderer3D.setBaseItemLabelGenerator(generator); 64 barRenderer3D.setBaseItemLabelsVisible(true); 65 66 //设置柱体上的数字的字体 67 barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,15)); 68 69 ChartFrame frame = new ChartFrame("chart", chart); 70 frame.setVisible(true); 71 frame.pack(); 72 } 73 74 private static CategoryDataset getDataSet() { 75 DefaultCategoryDataset dataSet = new DefaultCategoryDataset(); 76 dataSet.addValue(3, "客户等级", "机分客户"); 77 dataSet.addValue(2, "客户等级", "签约客户"); 78 dataSet.addValue(1, "客户等级", "重要客户"); 79 80 return dataSet; 81 } 82 83 } |
柱状图: 1 package com.xjz; 2 3 import java.awt.Font; 4 5 import org.jfree.chart.ChartFactory; 6 import org.jfree.chart.ChartFrame; 7 import org.jfree.chart.JFreeChart; 8 import org.jfree.chart.axis.CategoryAxis3D; 9 import org.jfree.chart.axis.NumberAxis3D; 10 import org.jfree.chart.axis.NumberTickUnit; 11 import org.jfree.chart.labels.CategoryItemLabelGenerator; 12 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 13 import org.jfree.chart.plot.CategoryPlot; 14 import org.jfree.chart.plot.PlotOrientation; 15 import org.jfree.chart.renderer.category.BarRenderer3D; 16 import org.jfree.data.category.CategoryDataset; 17 import org.jfree.data.category.DefaultCategoryDataset; 18 19 public class BarDemo { 20 21 public static void main(String[] args) { 22 CategoryDataset dataset = getDataSet(); 23 JFreeChart chart = ChartFactory.createBarChart3D("客户分类统计", 24 "客户等级", //X轴 25 "数量", //Y轴 26 dataset ,//图表需要的数据 27 PlotOrientation.VERTICAL, //图表的方向 28 true, //是否显示图例 29 true, //是否显示工具提示 30 true //是否生成URL链接地址 31 ); 32 33 //处理乱码 34 //1.主标题 35 chart.getTitle().setFont(new Font("宋体",Font.BOLD,18)); 36 //2.子标题 37 chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,18)); 38 //获取图表区对象 39 CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot(); 40 //3.X轴 41 CategoryAxis3D categoryAxis3D = (CategoryAxis3D) categoryPlot.getDomainAxis(); 42 //4.Y轴 43 NumberAxis3D numberAxis3D = (NumberAxis3D) categoryPlot.getRangeAxis(); 44 //处理X轴乱码 45 categoryAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15)); 46 categoryAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15)); 47 //处理Y轴乱码 48 numberAxis3D.setTickLabelFont(new Font("宋体",Font.BOLD,15)); 49 numberAxis3D.setLabelFont(new Font("宋体",Font.BOLD,15)); 50 51 //处理Y轴的刻度 52 //设置自动刻度为false 53 numberAxis3D.setAutoTickUnitSelection(false); 54 numberAxis3D.setTickUnit(new NumberTickUnit(1)); 55 56 //处理图形的宽度 57 //1.获取绘图区对象 58 BarRenderer3D barRenderer3D =(BarRenderer3D) categoryPlot.getRenderer(); 59 barRenderer3D.setMaximumBarWidth(0.08); 60 61 CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator(); 62 // 处理柱体上的数字 63 barRenderer3D.setBaseItemLabelGenerator(generator); 64 barRenderer3D.setBaseItemLabelsVisible(true); 65 66 //设置柱体上的数字的字体 67 barRenderer3D.setBaseItemLabelFont(new Font("宋体",Font.BOLD,15)); 68 69 ChartFrame frame = new ChartFrame("chart", chart); 70 frame.setVisible(true); 71 frame.pack(); 72 } 73 74 private static CategoryDataset getDataSet() { 75 DefaultCategoryDataset dataSet = new DefaultCategoryDataset(); 76 dataSet.addValue(3, "客户等级", "机分客户"); 77 dataSet.addValue(2, "客户等级", "签约客户"); 78 dataSet.addValue(1, "客户等级", "重要客户"); 79 80 return dataSet; 81 } 82 83 } |
折线图: 1 package com.xjz; 2 3 import java.awt.Font; 4 import java.io.File; 5 import java.io.IOException; 6 7 import org.jfree.chart.ChartFactory; 8 import org.jfree.chart.ChartFrame; 9 import org.jfree.chart.ChartUtilities; 10 import org.jfree.chart.JFreeChart; 11 import org.jfree.chart.labels.PieSectionLabelGenerator; 12 import org.jfree.chart.labels.StandardPieSectionLabelGenerator; 13 import org.jfree.chart.plot.PiePlot3D; 14 import org.jfree.data.general.DefaultPieDataset; 15 import org.jfree.data.general.PieDataset; 16 17 public class LineDemo { 18 19 public static void main(String[] args) throws IOException { 20 PieDataset dataset = getDataSet(); 21 JFreeChart chart = ChartFactory.createPieChart3D("客户分类统计", 22 dataset ,//图表需要的数据 23 true, //是否显示图例 24 true, //是否显示工具提示 25 true //是否生成URL链接地址 26 ); 27 Font font = new Font("宋体",Font.BOLD,18); 28 chart.getTitle().setFont(font);//主标题 29 chart.getLegend().setItemFont(font);//子标题 30 31 PiePlot3D piePlot3D = (PiePlot3D) chart.getPlot();//图表区对象 32 piePlot3D.setLabelFont(font);//处理饼图标题乱码 33 34 //处理饼图显示的信息 35 String labelFormat = "{0}:{1}({2})";//0--key,1--value,2--百分比 36 PieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(labelFormat); 37 piePlot3D.setLabelGenerator(generator); 38 39 //生成图片 40 File file = new File("D:/chart.jpeg"); 41 ChartUtilities.saveChartAsJPEG(file, chart, 600, 400); 42 43 ChartFrame frame = new ChartFrame("chart", chart); 44 frame.setVisible(true); 45 frame.pack(); 46 } 47 48 private static DefaultPieDataset getDataSet() { 49 DefaultPieDataset dataSet = new DefaultPieDataset(); 50 dataSet.setValue("机分客户", 4); 51 dataSet.setValue("签约客户", 3); 52 dataSet.setValue("重要客户", 6); 53 return dataSet; 54 } 55 56 } |