JFreeChart 柱形图、折线图、饼图、条形图

  1. import java.awt.Font;
    import java.io.IOException;
    
    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.ValueAxis;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.category.DefaultCategoryDataset;
    
    public class CategoryServlet extends HttpServlet {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 6228999300370044372L;
    
    	/**
    	 * The doGet method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to get.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 */
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		response.setContentType("image/jpeg");
    		//创建3D柱形图
    		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    		dataset.addValue(610, "广州", "猪肉");
    		dataset.addValue(220, "广州", "牛肉");
    		dataset.addValue(530, "广州", "鸡肉");
    		dataset.addValue(340, "广州", "鱼肉");
    		
    		JFreeChart chart = ChartFactory.createBarChart3D("肉类销量统计图", "肉类",
    				"销量", dataset, PlotOrientation.VERTICAL, true, false,
    				false);
    		
    		Font font = new Font("宋体", 0, 12);//创建字体对象
    		chart.getLegend().setItemFont(font);//设置图例字体
    		chart.getTitle().setFont(font);//设置标题字体
    		CategoryPlot plot = chart.getCategoryPlot();
    
    		CategoryAxis domainAxis = plot.getDomainAxis();
    		domainAxis.setTickLabelFont(font);//设置X轴坐标上的文字
    		domainAxis.setLabelFont(font);//设置X轴标题上的文字
    		
    		ValueAxis rangeAxis = plot.getRangeAxis();
    		rangeAxis.setTickLabelFont(font);//设置Y轴坐标上的文字
    		rangeAxis.setLabelFont(font);//设置Y轴标题上的文字
    		
    		// 输出图片    
    		ChartUtilities.writeChartAsJPEG(response.getOutputStream(),0.5f,chart,800,600,null);
    	}
    
    }
    
     2.
    import java.awt.Font;
    import java.io.IOException;
    
    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.plot.PiePlot;
    import org.jfree.data.general.DefaultPieDataset;
    
    public class PieServlet extends HttpServlet {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 8827627228843788090L;
    
    	/**
    	 * The doGet method of the servlet. <br>
    	 * 
    	 * This method is called when a form has its tag value method equals to get.
    	 * 
    	 * @param request
    	 *            the request send by the client to the server
    	 * @param response
    	 *            the response send by the server to the client
    	 * @throws ServletException
    	 *             if an error occurred
    	 * @throws IOException
    	 *             if an error occurred
    	 */
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		response.setContentType("image/jpeg");
    		
    		// 设置数据集
    		DefaultPieDataset dataset = new DefaultPieDataset();
    		dataset.setValue("初中高级程序员", 0.55);
    		dataset.setValue("项目经理", 0.1);
    		dataset.setValue("系统分析师", 0.1);
    		dataset.setValue("软件架构师", 0.1);
    		dataset.setValue("其他", 0.2);
    		
    		//创建3D饼图
    		JFreeChart chart = ChartFactory.createPieChart3D("饼图", // chart title
    				dataset,// data
    				true, // include legend
    				true, 
    				false);
    		
    		// 设置图表属性
    		PiePlot piePlot = (PiePlot) chart.getPlot();
    		piePlot.setCircular(true);//饼图是否一定是正圆
    		Font font = new Font("宋体", 0, 12);//创建字体对象
    		piePlot.setLabelFont(font);//设置标签字体
    		chart.getTitle().setFont(font);//设置标题字体
    		
    		//没有数据的时候显示的内容
    		piePlot.setNoDataMessage("无数据显示");
    		piePlot.setCircular(false);
    		piePlot.setLabelGap(0.02D);
    		
    		// 输出图片    
    		ChartUtilities.writeChartAsJPEG(response.getOutputStream(),0.5f,chart,800,600,null);
    	}
    
    }
    
     3.
    import java.awt.Font;
    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.PlotOrientation;
    import org.jfree.data.category.DefaultCategoryDataset;
    
    /**
     * 
     * 折线图
     */
    
    public class PolyLine {
    
    	public static void createPolyLine() {
    
    		/*
    		 * 
    		 * 报表字体
    		 */
    
    		final Font CHART_FONT = new Font("宋体", 12, 12);
    
    		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    		dataset.addValue(100, "ten", "jfree");
    		dataset.addValue(150, "ten", "chart");
    		dataset.addValue(300, "ten", "struts");
    		dataset.addValue(100, "ten", "hibernate");
    
    		/*
    		 * 
    		 * 三维折线图把createLineChart换成createLineChart3D就可以实现三维折线图。
    		 */
    		JFreeChart chart = ChartFactory.createLineChart3D(
    				"chart", // 标题
    				"num", // 横坐标
    				"type", // 纵坐标
    				dataset, // 数据
    				PlotOrientation.VERTICAL, // 竖直图表
    				true, // 是否显示legend
    				false, // 是否显示tooltip
    				false // 是否使用url链接
    				);
    		// XYPlot plot = null;
    		// 设置字体,解决中文乱码问题
    		chart.getTitle().setFont(CHART_FONT);
    		chart.getLegend().setItemFont(CHART_FONT);
    		// plot.getRangeAxis().setLabelFont(CHART_FONT);
    		// plot.getDomainAxis().setLabelFont(new Font("宋体", 12, 24));
    		FileOutputStream fos = null;
    		try {
    			fos = new FileOutputStream("src/poly3d.png");
    			ChartUtilities.writeChartAsPNG(fos, chart, 400, 300);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				fos.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    
    		PolyLine.createPolyLine();
    
    	}
    }
    
     
  2. 饼图
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.text.NumberFormat;
    
    import javax.swing.JPanel;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.StandardChartTheme;
    import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    
    public class Test1 extends ApplicationFrame {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -8028194135638567202L;
    	
    	public static final Font DEFAULT_FONT = new Font("宋体", Font.PLAIN, 12);
    	
    	public Test1(String title) {
    		super(title);
    		JPanel localJPanel = createDemoPanel();
    	    localJPanel.setPreferredSize(new Dimension(500, 270));
    	    setContentPane(localJPanel);
    	}
    	
    	public static JPanel createDemoPanel() {
    		DefaultPieDataset defaultpiedataset = createDataset();
    		
    		ChartFactory.setChartTheme(createStandardChartTheme());
    		
    		JFreeChart jfreechart = ChartFactory.createPieChart("饼图",
    				defaultpiedataset, true, true, false);
    		
    		jfreechart.setBackgroundPaint(new Color(222, 222, 255));
    		//获取PiePlot对象,用于设置饼图的相关属性,如:颜色、形状……
    		PiePlot pieplot = (PiePlot) jfreechart.getPlot();
    		pieplot.setSectionPaint("壹", new Color(222, 222, 255));
    		pieplot.setBackgroundPaint(Color.white);
    		pieplot.setCircular(true);
    		//自定义显示百分比样式
    		//图片中显示百分比:自定义方式,{0} 表示选项,{1} 表示数值, {2} 表示所占百分比
    		pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
    		"{0} = {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()));
    		//设置图片突起部分
    //		pieplot.setExplodePercent("Section 1", 0.5D);
    		pieplot.setNoDataMessage("No data available");
    		ChartPanel chartpanel = new ChartPanel(jfreechart);
    		chartpanel.setPreferredSize(new Dimension(500, 270));
    		//旋转
    //		Rotator rotator = new Rotator(pieplot);
    //		rotator.start();
    		return chartpanel;
    	}
    	
    	private static DefaultPieDataset createDataset() {
    		DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
    		defaultpiedataset.setValue("壹", 23.3);
    		defaultpiedataset.setValue("贰", 56.5);
    		defaultpiedataset.setValue("参", 43.3);
    		defaultpiedataset.setValue("肆", 11.1);
    		return defaultpiedataset;
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Test1 test1 = new Test1("");
    	    test1.pack();
    	    RefineryUtilities.centerFrameOnScreen(test1);
    	    test1.setVisible(true);
    	}
    	
    	//设计饼图的一个标准模板
    	public static StandardChartTheme createStandardChartTheme() {
    		//创建主题样式
    		StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
    		//设置标题字体
    		standardChartTheme.setExtraLargeFont(DEFAULT_FONT);
    		//设置图例子图
    		standardChartTheme.setRegularFont(DEFAULT_FONT);
    		//设置轴向字体
    		standardChartTheme.setLargeFont(DEFAULT_FONT);
    		return standardChartTheme;
    	}
    
    }
    
     
  3. 条形图
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GradientPaint;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    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.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.BarRenderer;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    
    public class Test2 extends ApplicationFrame {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1107365396303352467L;
    	
    	public static final Font DEFAULT_FONT = new Font("宋体", Font.PLAIN, 12);
    	
    	public Test2(String title) {
    		super(title);
    		JFreeChart chart = createChart(createDataset());
    		ChartPanel chartPanel = new ChartPanel(chart, false);
    		chartPanel.setPreferredSize(new Dimension(500, 270));
    		setContentPane(chartPanel);
    	}
    	private CategoryDataset createDataset() {
    		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    		dataset.addValue(1.0, "Row 1", "Column 1");
    		dataset.addValue(5.0, "Row 1", "Column 2");
    		dataset.addValue(3.0, "Row 1", "Column 3");
    		dataset.addValue(2.0, "Row 2", "Column 1");
    		dataset.addValue(3.0, "Row 2", "Column 2");
    		dataset.addValue(2.0, "Row 2", "Column 3");
    		return dataset;
    	}
    	
    	private JFreeChart createChart(CategoryDataset dataset) {
    		JFreeChart chart = ChartFactory.createBarChart("BarChartDemo", // chart title
    		"Category", // domain axis label
    		"Value", // range axis label
    		dataset, // data
    		PlotOrientation.VERTICAL, // orientation
    		true, // include legend
    		true, // tooltips?
    		false // URLs?
    		);
    		chart.setBackgroundPaint(Color.white);
    		CategoryPlot plot = (CategoryPlot) chart.getPlot();
    		plot.setBackgroundPaint(Color.lightGray);
    		plot.setRangeGridlinePaint(Color.white);
    		
    		//刻度轴刻度设置
    		NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
    		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    		//设置颜色
    		BarRenderer renderer = (BarRenderer) plot.getRenderer();
    		renderer.setSeriesPaint(0, Color.gray);
    		renderer.setSeriesPaint(1, Color.orange);
    		renderer.setDrawBarOutline(false);
    		
    		renderer.setDrawBarOutline(false);//设置外廓线不可见
    		GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue,
    		0.0F, 0.0F, new Color(0, 0, 64));
    		GradientPaint gradientpaint_7_ = new GradientPaint(0.0F, 0.0F,
    		Color.green, 0.0F, 0.0F, new Color(0, 64, 0));
    		GradientPaint gradientpaint_8_ = new GradientPaint(0.0F, 0.0F,
    		Color.red, 0.0F, 0.0F, new Color(64, 0, 0));
    		renderer.setSeriesPaint(0, gradientpaint);
    		renderer.setSeriesPaint(1, gradientpaint_7_);
    		renderer.setSeriesPaint(2, gradientpaint_8_);
    		//设置种类里直方条形图之间的空间
    		renderer.setItemMargin(0.0);
    		//设置种类标签旋转的角度,逆时针旋转
    		CategoryAxis categoryaxis = plot.getDomainAxis();
    		categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
    		.createUpRotationLabelPositions(Math.PI / 6));
    		return chart;
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Test2 demo = new Test2("Bar Demo");
    		demo.pack();
    		RefineryUtilities.centerFrameOnScreen(demo);
    		demo.setVisible(true);
    	}
    
    }
    
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值