JFreeChart的柱状图实现

来源:互联网 发布:聚划算和淘宝关系 编辑:程序博客网 时间:2024/05/01 09:10

JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。

本文根据网上的资料和其他人的例子整理了使用JFreeChart实现简单柱状图的方法。图形效果如下:

实现代码如下:

//JFreeChart Bar Chart(柱状图)   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, "E:\\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, 500, 400);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, // datasetPlotOrientation.VERTICAL, false, // legendfalse, // tooltipsfalse); // URLsFont labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);jfreechart.setTextAntiAlias(false);jfreechart.setBackgroundPaint(Color.white);CategoryPlot plot = jfreechart.getCategoryPlot();// 获得图表区域对象// 设置横虚线可见plot.setRangeGridlinesVisible(true);// 虚线色彩plot.setRangeGridlinePaint(Color.gray);// 数据轴精度NumberAxis vn = (NumberAxis) plot.getRangeAxis();// vn.setAutoRangeIncludesZero(true);DecimalFormat df = new DecimalFormat("#0.0");vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式// x轴设置CategoryAxis domainAxis = plot.getDomainAxis();domainAxis.setLabelFont(labelFont);// 轴标题domainAxis.setTickLabelFont(labelFont);// 轴数值// Lable(Math.PI/3.0)度倾斜// domainAxis.setCategoryLabelPositions(CategoryLabelPositions// .createUpRotationLabelPositions(Math.PI / 3.0));domainAxis.setMaximumCategoryLabelWidthRatio(6.00f);// 横轴上的 Lable// 是否完整显示// 设置距离图片左端距离domainAxis.setLowerMargin(0.1);// 设置距离图片右端距离domainAxis.setUpperMargin(0.1);// 设置 columnKey 是否间隔显示// domainAxis.setSkipCategoryLabelsToFit(true);plot.setDomainAxis(domainAxis);// 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)plot.setBackgroundPaint(new Color(255, 255, 204));// y轴设置ValueAxis rangeAxis = plot.getRangeAxis();rangeAxis.setLabelFont(labelFont);rangeAxis.setTickLabelFont(labelFont);// 设置最高的一个 Item 与图片顶端的距离rangeAxis.setUpperMargin(0.15);// 设置最低的一个 Item 与图片底端的距离rangeAxis.setLowerMargin(0.15);plot.setRangeAxis(rangeAxis);// 解决中文乱码问题(关键)TextTitle textTitle = jfreechart.getTitle();textTitle.setFont(new Font("黑体", Font.PLAIN, 20));domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));vn.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));vn.setLabelFont(new Font("黑体", Font.PLAIN, 12));// jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));BarRenderer renderer = new BarRenderer();// 设置柱子宽度renderer.setMaximumBarWidth(0.2);// 设置柱子高度renderer.setMinimumBarLength(0.2);// 设置柱子边框颜色renderer.setBaseOutlinePaint(Color.BLACK);// 设置柱子边框可见renderer.setDrawBarOutline(true);// // 设置柱的颜色renderer.setSeriesPaint(0, Color.decode("#8BBA00"));// 设置每个地区所包含的平行柱的之间距离renderer.setItemMargin(0.5);jfreechart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);// 显示每个柱的数值,并修改该数值的字体属性renderer.setIncludeBaseInRange(true);renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());renderer.setBaseItemLabelsVisible(true);plot.setRenderer(renderer);// 设置柱的透明度plot.setForegroundAlpha(1.0f);// 背景色 透明度plot.setBackgroundAlpha(0.5f);return jfreechart;}// 创建CategoryDataset对象public static CategoryDataset createDataset() {double[][] data = new double[][] { { 25, 24, 40, 12, 33, 33 } };String[] rowKeys = { "" };String[] columnKeys = { "张三", "李四", "王五", "马六", "陈七", "赵八" };CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);return dataset;}}