package com.potevio.rnd; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DatasetUtilities; public class CreateJFreeChartBar { /** * 创建JFreeChart Bar Chart(柱状图) */ public static void main(String[] args) { //步骤1:创建CategoryDataset对象(准备数据) CategoryDataset dataset = createDataset(); //步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置 JFreeChart freeChart = createChart(dataset); //步骤3:将JFreeChart对象输出到文件,Servlet输出流等 saveAsFile(freeChart, "F:\\jfreechart\\bar.png", 500, 400); } //保存为文件 public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) { FileOutputStream out = null; try { File outFile = new File(outputPath); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } out = new FileOutputStream(outputPath); //保存为PNG文件 ChartUtilities.writeChartAsPNG(out, chart, weight, height); //保存为JPEG文件 //ChartUtilities.writeChartAsJPEG(out, chart, weight, height); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { //do nothing } } } } //根据CategoryDataset生成JFreeChart对象 public static JFreeChart createChart(CategoryDataset categoryDataset) { JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo", //标题 "产品", //categoryAxisLabel (category轴,横轴,X轴的标签) "数量", //valueAxisLabel(value轴,纵轴,Y轴的标签) categoryDataset, // dataset PlotOrientation.VERTICAL, true, // legend false, // tooltips false); // URLs //以下的设置可以由用户定制,也可以省略 CategoryPlot plot = (CategoryPlot) jfreechart.getPlot(); //背景色 透明度 plot.setBackgroundAlpha(0.5f); //前景色 透明度 plot.setForegroundAlpha(0.5f); //其它设置可以参考 CategoryPlot return jfreechart; } /** * 创建CategoryDataset对象 * */ public static CategoryDataset createDataset() { String []rowKeys = {"One", "Two", "Three"}; String []colKeys = {"1987", "1997", "2007"}; double [][] data = { {50, 20, 30}, {20, 10D, 40D}, {40, 30.0008D, 38.24D}, }; //也可以使用以下代码 //DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset(); //categoryDataset.addValue(10, "rowKey", "colKey"); return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data); } }
运行效果图:
附上另外一份代码:
package com; import java.awt.Color; import java.awt.Font; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.ItemLabelAnchor; import org.jfree.chart.labels.ItemLabelPosition; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DatasetUtilities; import org.jfree.ui.TextAnchor; public class CreateJFreeChartBar3 { public static void main(String[] args) { //步骤1:创建CategoryDataset对象(准备数据) CategoryDataset dataset = createDataset(); //步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置 JFreeChart freeChart = createChart(dataset); //步骤3:将JFreeChart对象输出到文件,Servlet输出流等 saveAsFile(freeChart, "E:\\jfreechart\\batpic.png", 600, 500); } //保存为文件 public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) { FileOutputStream out = null; try { File outFile = new File(outputPath); if (!outFile.getParentFile().exists()) { outFile.getParentFile().mkdirs(); } out = new FileOutputStream(outputPath); //保存为PNG文件 ChartUtilities.writeChartAsPNG(out, chart, weight, height); //保存为JPEG文件 //ChartUtilities.writeChartAsJPEG(out, chart, weight, height); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { //do nothing } } } } //根据CategoryDataset生成JFreeChart对象 public static JFreeChart createChart(CategoryDataset categoryDataset) { JFreeChart jfreechart = ChartFactory.createBarChart("", //标题 "人员", //categoryAxisLabel (category轴,横轴,X轴的标签) "次数", //valueAxisLabel(value轴,纵轴,Y轴的标签) categoryDataset, // dataset PlotOrientation.VERTICAL, true, // legend false, // tooltips false); // URLs TextTitle title = jfreechart.getTitle(); title.setFont(new Font("宋体", Font.BOLD, 12)); //以下的设置可以由用户定制,也可以省略 // 取得状图plot对象 CategoryPlot plot = (CategoryPlot) jfreechart.getPlot(); plot.setBackgroundPaint(Color.lightGray); // plot.setBackgroundPaint(new Color(255, 255, 204));// 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确) plot.setRangeGridlinesVisible(true);// 设置横虚线可见 plot.setRangeGridlinePaint(Color.gray);// 虚线色彩 // x轴设置 CategoryAxis domainAxis = plot.getDomainAxis(); //domainAxis.setLabelFont(labelFont);// 轴标题 //domainAxis.setTickLabelFont(labelFont);// 轴数值 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 3.0));//度倾斜 domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 横轴上的 Lable 是否完整显示 domainAxis.setLowerMargin(0.05);// 设置距离图片左端距离 domainAxis.setUpperMargin(0.05);// 设置距离图片右端距离 plot.setDomainAxis(domainAxis); domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 12));// X轴的标题文字字体 隶书 domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// X轴坐标上数值字体 // domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); // X轴上的Lable让其45度倾斜 .UP_45 // y轴设置 ValueAxis rangeAxis = plot.getRangeAxis(); //rangeAxis.setLabelFont(labelFont); //rangeAxis.setTickLabelFont(labelFont); // rangeAxis.setRange(85, 100.5);//纵轴显示范围 rangeAxis.setAutoTickUnitSelection(true); rangeAxis.setUpperMargin(0.15);// 设置最高的一个 Item 与图片顶端的距离 rangeAxis.setLowerMargin(0.15);// 设置最低的一个 Item 与图片底端的距离 //设置 柱子的提示 中文处理 jfreechart.getLegend().setItemFont(new Font("宋体", Font.BOLD,10)); //plot.setRangeAxis(rangeAxis); // Y轴精度 NumberAxis vn = (NumberAxis) plot.getRangeAxis(); DecimalFormat df = new DecimalFormat("#0"); vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式 rangeAxis.setAutoTickUnitSelection(true);//步长自动设置 rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());//纵轴单元为整型 rangeAxis.setLabelFont(new Font("宋体", Font.BOLD, 12));// y轴的标题文字字体 rangeAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// y轴坐标上数值字体 //柱子属性 BarRenderer renderer = new BarRenderer(); renderer.setMaximumBarWidth(0.1);// 设置柱子宽度//TODO 0.05 renderer.setMinimumBarLength(0.2);// 设置柱子高度 renderer.setBaseOutlinePaint(Color.BLACK);// 设置柱子边框颜色 renderer.setDrawBarOutline(true);// 设置柱子边框可见 //renderer.setSeriesPaint(0, new Color(51, 204, 204));// 设置柱的颜色 //renderer.setSeriesPaint(1, new Color(253, 204, 255)); for(int i = 0 ; i < 10; i++){ renderer.setSeriesPaint(i, Color.decode("#2EDE1E")); renderer.setSeriesOutlinePaint(i, Color.BLACK); } renderer.setItemMargin(0.0);// 设置每个地区所包含的平行柱的之间距离 // 显示每个柱的数值,并修改该数值的字体属性 renderer.setIncludeBaseInRange(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); renderer.setBaseItemLabelFont(new Font("宋体", Font.BOLD, 12));// 数值字体TRUETYPE_FONT renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_CENTER));//数值显示位置 plot.setRenderer(renderer); plot.setForegroundAlpha(1.0f);// 设置柱的透明度 // //背景色 透明度 // plot.setBackgroundAlpha(0.5f); // //前景色 透明度 // plot.setForegroundAlpha(0.5f); return jfreechart; } /** * 创建CategoryDataset对象 * */ public static CategoryDataset createDataset() { String []rowKeys = {"次数"}; String []colKeys = {"一二", "二二", "三二","三一", "二三二", "二三三","二三五", "二三四", "二三六","一二三"}; double [][] data = { {50,41,30,20,29,45,21,32,40,38} }; return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data); } }
运行效果图: