1 package barchartdemo1; 2 3 import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.awt.Font; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import org.jfree.chart.ChartFactory; 7 import org.jfree.chart.ChartUtilities; 8 import org.jfree.chart.JFreeChart; 9 import org.jfree.chart.axis.CategoryAxis; 10 import org.jfree.chart.axis.NumberAxis; 11 import org.jfree.chart.plot.CategoryPlot; 12 import org.jfree.chart.plot.PlotOrientation; 13 import org.jfree.data.category.CategoryDataset; 14 import org.jfree.data.category.DefaultCategoryDataset; 15 16 /** 17 * 18 * @author Administrator 19 */ 20 public class Main { 21 22 /** 23 * @param args the command line arguments 24 */ 25 public static void main(String[] args) throws IOException { 26 CategoryDataset ds = getDataSet(); 27 JFreeChart chart = ChartFactory.createBarChart3D( 28 "水果產量圖", //圖表標題 29 "水果", //目錄軸的顯示標簽 30 "產量", //數值軸的顯示標簽 31 ds, //數據集 32 PlotOrientation.VERTICAL, //圖表方向 33 true, //是否顯示圖例,對於簡單的柱狀圖必須為false 34 false, //是否生成提示工具 35 false); //是否生成url鏈接 36 37 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); 38 39 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); 40 41 CategoryAxis domainAxis = categoryplot.getDomainAxis(); 42 43 /*------設置X軸坐標上的文字-----------*/ 44 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 45 46 /*------設置X軸的標題文字------------*/ 47 domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); 48 49 /*------設置Y軸坐標上的文字-----------*/ 50 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 51 52 /*------設置Y軸的標題文字------------*/ 53 numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12)); 54 55 /*------這句代碼解決了底部漢字亂碼的問題-----------*/ 56 chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12)); 57 58 /*******這句代碼解決了標題漢字亂碼的問題********/ 59 chart.getTitle().setFont(new Font("宋體", Font.PLAIN, 12)); 60 61 FileOutputStream out = null; 62 try { 63 out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg"); 64 ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null); 65 } finally { 66 try { 67 out.close(); 68 } catch (Exception ex) { 69 ex.printStackTrace(); 70 } 71 } 72 } 73 74 private static CategoryDataset getDataSet() { 75 DefaultCategoryDataset ds = new DefaultCategoryDataset(); 76 ds.addValue(100, "北京", "蘋果"); 77 ds.addValue(100, "上海", "蘋果"); 78 ds.addValue(100, "廣州", "蘋果"); 79 ds.addValue(200, "北京", "梨子"); 80 ds.addValue(200, "上海", "梨子"); 81 ds.addValue(200, "廣州", "梨子"); 82 ds.addValue(300, "北京", "葡萄"); 83 ds.addValue(300, "上海", "葡萄"); 84 ds.addValue(300, "廣州", "葡萄"); 85 ds.addValue(400, "北京", "橘子"); 86 ds.addValue(400, "上海", "橘子"); 87 ds.addValue(400, "廣州", "橘子"); 88 ds.addValue(500, "北京", "香蕉"); 89 ds.addValue(500, "上海", "香蕉"); 90 ds.addValue(500, "廣州", "香蕉"); 91 return ds; 92 } 93 }
1 package barchartdemo1; 2 3 import <a href="http://lib.csdn.net/base/javaee" class='replace_word' title="Java EE知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.awt.Font; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import org.jfree.chart.ChartFactory; 7 import org.jfree.chart.ChartUtilities; 8 import org.jfree.chart.JFreeChart; 9 import org.jfree.chart.axis.CategoryAxis; 10 import org.jfree.chart.axis.NumberAxis; 11 import org.jfree.chart.plot.CategoryPlot; 12 import org.jfree.chart.plot.PlotOrientation; 13 import org.jfree.data.category.CategoryDataset; 14 import org.jfree.data.category.DefaultCategoryDataset; 15 16 /** 17 * 18 * @author Administrator 19 */ 20 public class Main { 21 22 /** 23 * @param args the command line arguments 24 */ 25 public static void main(String[] args) throws IOException { 26 CategoryDataset ds = getDataSet(); 27 JFreeChart chart = ChartFactory.createBarChart3D( 28 "水果產量圖", //圖表標題 29 "水果", //目錄軸的顯示標簽 30 "產量", //數值軸的顯示標簽 31 ds, //數據集 32 PlotOrientation.VERTICAL, //圖表方向 33 true, //是否顯示圖例,對於簡單的柱狀圖必須為false 34 false, //是否生成提示工具 35 false); //是否生成url鏈接 36 37 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); 38 39 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); 40 41 CategoryAxis domainAxis = categoryplot.getDomainAxis(); 42 43 /*------設置X軸坐標上的文字-----------*/ 44 domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 45 46 /*------設置X軸的標題文字------------*/ 47 domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); 48 49 /*------設置Y軸坐標上的文字-----------*/ 50 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 51 52 /*------設置Y軸的標題文字------------*/ 53 numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12)); 54 55 /*------這句代碼解決了底部漢字亂碼的問題-----------*/ 56 chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12)); 57 58 /*******這句代碼解決了標題漢字亂碼的問題********/ 59 chart.getTitle().setFont(new Font("宋體", Font.PLAIN, 12)); 60 61 FileOutputStream out = null; 62 try { 63 out = new FileOutputStream("E://Items//BarChartDemo1//image//1.jpg"); 64 ChartUtilities.writeChartAsJPEG(out, 0.5f, chart, 400, 300, null); 65 } finally { 66 try { 67 out.close(); 68 } catch (Exception ex) { 69 ex.printStackTrace(); 70 } 71 } 72 } 73 74 private static CategoryDataset getDataSet() { 75 DefaultCategoryDataset ds = new DefaultCategoryDataset(); 76 ds.addValue(100, "北京", "蘋果"); 77 ds.addValue(100, "上海", "蘋果"); 78 ds.addValue(100, "廣州", "蘋果"); 79 ds.addValue(200, "北京", "梨子"); 80 ds.addValue(200, "上海", "梨子"); 81 ds.addValue(200, "廣州", "梨子"); 82 ds.addValue(300, "北京", "葡萄"); 83 ds.addValue(300, "上海", "葡萄"); 84 ds.addValue(300, "廣州", "葡萄"); 85 ds.addValue(400, "北京", "橘子"); 86 ds.addValue(400, "上海", "橘子"); 87 ds.addValue(400, "廣州", "橘子"); 88 ds.addValue(500, "北京", "香蕉"); 89 ds.addValue(500, "上海", "香蕉"); 90 ds.addValue(500, "廣州", "香蕉"); 91 return ds; 92 } 93 }
圖片效果:

