前一篇文章说明了JFreeChar生成柱状图以及解决中文只能显示问方框的问题,本文主要记录柱状图的显示案例,可供初学者参考:
先看一个简单的柱状图生成案例:
- package org.ml.servlet;
- import java.awt.Font;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServlet;
- 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.chart.title.TextTitle;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- import org.jfree.data.general.DefaultPieDataset;
- public class ChartDemoServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public ChartDemoServlet() {
- super();
- }
- /**
- * 创建一个数据集合
- *
- * @return
- */
- private static DefaultPieDataset getDataSet() {
- DefaultPieDataset dataset = new DefaultPieDataset();
- dataset.setValue("苹果", 100);
- dataset.setValue("梨子", 200);
- dataset.setValue("葡萄", 300);
- dataset.setValue("香蕉", 400);
- dataset.setValue("荔枝", 500);
- return dataset;
- }
- @Override
- public void service(ServletRequest req, ServletResponse res)
- throws ServletException, IOException {
- res.setContentType("image/jpeg");// 将输出设置为image/jepg格式
- CategoryDataset dataset = getDataSet2();// 取得数据集合
- JFreeChart chart2 = ChartFactory.createBarChart3D("水果产量图", // 图表标题
- "水果", // 目录轴的显示标签
- "产量", // 数值轴的显示标签
- dataset, // 数据集
- PlotOrientation.VERTICAL, // HORIZONTAL,// 图表方向:水平、垂直
- false, // 是否显示图例(对于简单的柱状图是false,因为只有一种,没有必要显示)
- false, // 是否生成工具
- false // 是否生成URL链接
- );
- Font titleFont = new Font("黑体", Font.BOLD, 20);
- Font plotFont = new Font("宋体", Font.PLAIN, 16);
- TextTitle textTitle2 = chart2.getTitle();
- textTitle2.setFont(titleFont);// 为标题设置上字体
- CategoryPlot categoryPlot = chart2.getCategoryPlot();
- categoryPlot.getRangeAxis().setLabelFont(plotFont);// 设置Y轴标识字体
- categoryPlot.getDomainAxis().setLabelFont(plotFont);// 设置X轴标识字体
- categoryPlot.getDomainAxis().setTickLabelFont(plotFont);// 设置轴标记的坐标的标记字体
- ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1.0f, chart2,
- 800, 450, null);// 输出图表
- }
- private static CategoryDataset getDataSet2() {
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- dataset.addValue(100, "", "苹果");
- dataset.addValue(200, "", "梨子");
- dataset.addValue(300, "", "葡萄");
- dataset.addValue(400, "", "香蕉");
- dataset.addValue(500, "", "荔枝");
- return dataset;
- }
- }
![](https://img-my.csdn.net/uploads/201210/26/1351241357_9740.png)
这是一个简单的柱状图实例,只有各种水果产量之间的简单对比,下面看一个对各个城市之间的水果产量进行对比的例子:
- package org.ml.servlet;
- import java.awt.Font;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServlet;
- 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.chart.title.LegendTitle;
- import org.jfree.chart.title.TextTitle;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.category.DefaultCategoryDataset;
- public class ChartDemoServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public ChartDemoServlet() {
- super();
- }
- /**
- * 创建一个数据集合
- *
- * @return
- */
- // private static DefaultPieDataset getDataSet() {
- // DefaultPieDataset dataset = new DefaultPieDataset();
- // dataset.setValue("苹果", 100);
- // dataset.setValue("梨子", 200);
- // dataset.setValue("葡萄", 300);
- // dataset.setValue("香蕉", 400);
- // dataset.setValue("荔枝", 500);
- // return dataset;
- // }
- @Override
- public void service(ServletRequest req, ServletResponse res)
- throws ServletException, IOException {
- res.setContentType("image/jpeg");// 将输出设置为image/jepg格式
- CategoryDataset dataset = getDataSet3();// 取得数据集合
- JFreeChart chart2 = ChartFactory.createBarChart3D("水果产量图", // 图表标题
- "水果", // 目录轴的显示标签
- "产量", // 数值轴的显示标签
- dataset, // 数据集
- PlotOrientation.VERTICAL, // HORIZONTAL,// 图表方向:水平、垂直
- true, // 是否显示图例(对于简单的柱状图是false,因为只有一种,没有必要显示)
- false, // 是否生成工具
- false // 是否生成URL链接
- );
- Font titleFont = new Font("黑体", Font.BOLD, 20);
- Font plotFont = new Font("宋体", Font.PLAIN, 16);
- Font LegendFont = new Font("楷体", Font.PLAIN, 18);
- TextTitle textTitle2 = chart2.getTitle();
- textTitle2.setFont(titleFont);// 为标题设置上字体
- LegendTitle legend2 = chart2.getLegend(0);
- legend2.setItemFont(LegendFont);// 为图例说明设置字体
- CategoryPlot categoryPlot = chart2.getCategoryPlot();
- categoryPlot.getRangeAxis().setLabelFont(plotFont);// 设置Y轴标识字体
- categoryPlot.getDomainAxis().setLabelFont(plotFont);// 设置X轴标识字体
- categoryPlot.getDomainAxis().setTickLabelFont(plotFont);// 设置轴标记的坐标的标记字体
- ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1.0f, chart2,
- 800, 450, null);// 输出图表
- }
- private CategoryDataset getDataSet3() {
- DefaultCategoryDataset dataset = new DefaultCategoryDataset();
- dataset.addValue(100, "北京", "苹果");
- dataset.addValue(100, "上海", "苹果");
- dataset.addValue(100, "广州", "苹果");
- dataset.addValue(200, "北京", "梨子");
- dataset.addValue(200, "上海", "梨子");
- dataset.addValue(200, "广州", "梨子");
- dataset.addValue(300, "北京", "葡萄");
- dataset.addValue(300, "上海", "葡萄");
- dataset.addValue(300, "广州", "葡萄");
- dataset.addValue(400, "北京", "香蕉");
- dataset.addValue(400, "上海", "香蕉");
- dataset.addValue(400, "广州", "香蕉");
- dataset.addValue(500, "北京", "荔枝");
- dataset.addValue(500, "上海", "荔枝");
- dataset.addValue(500, "广州", "荔枝");
- return dataset;
- }
- }
JFreeChar生成柱状图的方式和饼状图的大体一致,如果不清楚可以参考这里(点击进入).