餅圖:java 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 } |
柱狀圖:ide 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 } |