JFreeChart学习笔记

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.TextAnchor;

public class ReportServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("image/png;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		String p = request.getParameter("type");
		if ("pie".equals(p))
			pieChart(response);
		else
			barChart(response);

	}

	//柱状图
	@SuppressWarnings("deprecation")
	private void barChart(HttpServletResponse response) throws IOException {
		DefaultCategoryDataset defaultPieDataset = new DefaultCategoryDataset();
		// 数据  名字  组名
		defaultPieDataset.addValue(62, "文思", "");
		defaultPieDataset.addValue(33, "联想中望", "");
		defaultPieDataset.addValue(26, "创科", "");
		defaultPieDataset.addValue(18, "中软", "");
		defaultPieDataset.addValue(48, "法本", "");
		defaultPieDataset.addValue(38, "华为", "");

		// createBarChart3D
		JFreeChart jFreeChart = ChartFactory.createBarChart("中国移动(深圳)厂商人力报表", "各厂商概况", "人数",
				defaultPieDataset, PlotOrientation.VERTICAL, true, false, false);
		jFreeChart.getTitle().setFont(new Font("黑体", Font.BOLD, 15));
		jFreeChart.getLegend().setItemFont(new Font("SansSerif", Font.BOLD, 12));

		// 获取图形对象,并设置参数
		CategoryPlot plot = jFreeChart.getCategoryPlot();
		plot.setNoDataMessage("没有销售数据");
		plot.setBackgroundPaint(Color.white); 									// 背景
		plot.setOutlinePaint(Color.white); 										// 设置绘图面板外边的填充颜色

		// 设置"组"间间隔
		CategoryAxis axis = plot.getDomainAxis();
		axis.setCategoryMargin(0.15);											//设置组间间隔
		//设置标签轴第一个图例距数值轴的距离
		axis.setLowerMargin(0.03);
		axis.setUpperMargin(0.03);
		axis.setLabelFont(new Font("SansSerif", Font.BOLD, 13));				//设置Y标签的字体
		axis.setTickLabelFont(new Font("SansSerif", Font.TRUETYPE_FONT, 18)); 	//设置组名称的字体

		// 设置"数值"轴最大值和最小值
		NumberAxis naxis = (NumberAxis) plot.getRangeAxis();
		naxis.setUpperBound(150);
		naxis.setLowerBound(0);
		naxis.setLabelFont(new Font("SansSerif", Font.BOLD, 11)); 				//设置X标签的字体

		// 设置"柱子"
		BarRenderer render = new BarRenderer();
		render.setBaseOutlinePaint(Color.BLACK);
		render.setSeriesPaint(0, new Color(241, 9, 14));
		render.setSeriesPaint(1, new Color(243, 92, 0));
		render.setSeriesPaint(2, new Color(100, 0, 220));
		render.setSeriesPaint(3, new Color(2, 93, 220));
		render.setSeriesPaint(4, new Color(142, 204, 241));
		render.setSeriesPaint(5, new Color(15, 160, 0));
		render.setSeriesPaint(6, new Color(173, 220, 0));
		// 设置每个地区所包含的平行柱的之间距离及是否显示数值
		render.setItemMargin(0.15);
		render.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{0}({2})",
				new DecimalFormat("0")));
		render.setItemLabelsVisible(true);
		render.setPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
				TextAnchor.BOTTOM_CENTER, TextAnchor.HALF_ASCENT_LEFT, -0.6));
		render.setItemLabelFont(new Font("宋体", Font.PLAIN, 11));
		render.setItemLabelPaint(Color.red);
		
		plot.setRenderer(render);

		ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, 500, 300);
	}

	//饼状图
	private void pieChart(HttpServletResponse response) throws IOException {
		DefaultPieDataset defaultPieDataset = new DefaultPieDataset();
		defaultPieDataset.setValue("文思", 10.6);
		defaultPieDataset.setValue("中软", 20);
		defaultPieDataset.setValue("创科", 30);
		defaultPieDataset.setValue("联想中望", 40);
		defaultPieDataset.setValue("法本", 40);
		defaultPieDataset.setValue("华为", 32);

		PieDataset dataset = defaultPieDataset;
		JFreeChart jFreeChart = ChartFactory
				.createPieChart("中国移动(深圳)厂商人力报表", dataset, true, true, false);
		jFreeChart.getTitle().setFont(new Font("黑体", Font.BOLD, 15));
		jFreeChart.getLegend().setItemFont(new Font("SansSerif", Font.BOLD, 12));
		// 获取图形对象,并设置参数
		PiePlot piePlot = (PiePlot) jFreeChart.getPlot();
		piePlot.setLabelFont(new Font("黑体", 0, 12));
		piePlot.setNoDataMessage("没有销售数据");
		piePlot.setBaseSectionOutlinePaint(Color.white); // 饼图分割线
		piePlot.setLabelBackgroundPaint(Color.white); // lable背景
		piePlot.setLabelPaint(Color.red); // lable颜色
		piePlot.setCircular(true); // 是否为圆型
		piePlot.setLabelGap(0.01D); // 间距
		piePlot.setBackgroundPaint(Color.white); // 背景
		piePlot.setOutlinePaint(Color.WHITE); // 设置绘图面板外边的填充颜色
		piePlot.setShadowPaint(Color.WHITE); // 设置绘图面板阴影的填充颜色
		// piePlot.setForegroundAlpha(0.65f); // 指定图片的透明度(0.0-1.0)
		// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
		piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({2})", NumberFormat
				.getNumberInstance(), new DecimalFormat("0.00%")));
		// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
		/*
		 * piePlot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( "{0}={1}({2})"));
		 */

		ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, 500, 300);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
			IOException {
		doGet(req, resp);
	}

}

你可能感兴趣的:(java)