JFreeChart

package com.xuzengqiang.jfreechart;

import java.awt.Color;
import java.awt.Font;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PieLabelLinkStyle;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("all")
public class PieAction extends ActionSupport
{
	public static DefaultPieDataset createDataSet()
	{
		DefaultPieDataset dataset = new DefaultPieDataset();
		dataset.setValue("老年人", 20);
		dataset.setValue("年轻人", 40);
		dataset.setValue("小孩子", 10);
		dataset.setValue("成年人", 30);
		return dataset;
	}

	public static JFreeChart createChart(DefaultPieDataset dataset)
	{
		JFreeChart chart = ChartFactory.createPieChart("简单的饼状图--岳阳人口比例", // 如果想要3D,可以createPieChart3D
				dataset, true, // 是否生成图例
				true, // 是否生成工具(提示)
				false // 是否生成URL链接
				);
		chart.setBackgroundPaint(null); // 设置背景颜色,为null表示透明
		chart.setBorderVisible(false); // 设置边框线不可见
		titleStyle(chart);
		plotStyle(chart);
		legendStyle(chart);
		return chart;
	}

	// 初始化标题样式
	public static void titleStyle(JFreeChart chart)
	{
		TextTitle title = chart.getTitle(); // 设置标题字体,防止中文乱码
		title.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		title.setPaint(Color.decode("#FF7700")); // 设置字体颜色
	}

	// 初始化数据区样式
	public static void plotStyle(JFreeChart chart)
	{
		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setBackgroundPaint(Color.white); // 设置数据区背景颜色
		plot.setShadowPaint(Color.decode("#AFAFAF")); // 设置数据区阴影颜色
		plot.setOutlinePaint(Color.white); // 设置边界线条颜色
		plot.setBaseSectionOutlinePaint(Color.decode("#AFAFAF")); // 设置图边界颜色
		plot.setNoDataMessage("对不起,该城市没有对应数据!"); // 当数据为空的时候显示
		plot.setExplodePercent("老年人", 0.3); // 取出某一块,0.3指的是偏离半径值是半径的长度*0.3
		
		plot.setSectionPaint("老年人", Color.decode("#FFFFFF")); // 设定饼区颜色
		plot.setSectionPaint("成年人", Color.decode("#FF7700"));
		plot.setSectionPaint("年轻人", Color.decode("#3399CC"));
		plot.setSectionPaint("小孩子", Color.decode("#FF0000"));
		
		plot.setLabelBackgroundPaint(Color.white); // 设置分类标签的底色
		plot.setLabelOutlinePaint(null);
		plot.setLabelShadowPaint(null); // 分类标签阴影颜色
		plot.setLabelLinkStyle(PieLabelLinkStyle.QUAD_CURVE); // 设置连接线方式,这种属于折线
		plot.setLabelLinkPaint(Color.decode("#AFAFAF")); // 设置连接线的颜色
		plot.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12)); // 设置饼状图图标字体,防止中文乱码
	}

	// 初始化图释样式
	public static void legendStyle(JFreeChart chart)
	{
		LegendTitle legend = chart.getLegend();
		legend.setBackgroundPaint(null); // 设置图释背景颜色
		legend.setBorder(0, 0, 0, 0); // 设置图释边框,顺序依次为top,left,bottom,right,都为0时表示边框为0
		if (legend != null) // 设置图释字体。防止中文乱码
		{
			legend.setItemFont(new Font("微软雅黑", Font.PLAIN, 12));
		}
	}

	@Override
	public String execute() throws Exception
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		JFreeChart chart = createChart(createDataSet());
		ServletOutputStream out = response.getOutputStream();
		ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
		out.flush();
		out.close();
		return null;
	}
}

直方条形图

package com.xuzengqiang.jfreechart;

import java.awt.Color;
import java.awt.Font;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionSupport;

//直方条形图
@SuppressWarnings("all")
public class CategoryAction extends ActionSupport
{

	public static DefaultCategoryDataset createDataSet()
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(2000, "男", "2012年");
		dataset.addValue(3000, "男", "2013年");
		dataset.addValue(4000, "男", "2014年");
		dataset.addValue(2000, "女", "2012年");
		dataset.addValue(2500, "女", "2013年");                                   
		dataset.addValue(3000, "女", "2014年");
		return dataset;
	}

	public static JFreeChart createChart(DefaultCategoryDataset dataset)
	{
		JFreeChart chart = ChartFactory.createBarChart("简单的饼状图--杨山村男女人数", // 如果想要3D,可以createPieChart3D
				"年份", //横坐标名称
				"人数", //纵坐标名称
				dataset,
				PlotOrientation.VERTICAL,
				true, // 是否生成图例
				true, // 是否生成工具(提示)
				false // 是否生成URL链接
				);
		chart.setBackgroundPaint(Color.decode("#FFFFFF")); // 设置背景颜色,为null表示透明
		chart.setBorderVisible(false); // 设置边框线不可见
		
		titleStyle(chart);
		plotStyle(chart);
		legendStyle(chart);
		return chart;
	}

	// 初始化标题样式
	public static void titleStyle(JFreeChart chart)
	{
		TextTitle title = chart.getTitle(); // 设置标题字体,防止中文乱码
		title.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		title.setPaint(Color.decode("#FF7700")); // 设置字体颜色
	}

	// 初始化数据区样式
	public static void plotStyle(JFreeChart chart)
	{
		CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.decode("#FFFFFF")); 
		plot.setRangeGridlinePaint(Color.decode("#3399CC")); //对齐线的颜色
		plot.setDomainGridlinesVisible(false); 
		
		BarRenderer renderer=(BarRenderer)plot.getRenderer();
		renderer.setSeriesPaint(0, Color.decode("#FF7700")); //设置每个柱形的颜色
		renderer.setSeriesPaint(1, Color.decode("#3399CC"));
		renderer.setSeriesVisible(1,true);  //某一个柱形是否显示
		renderer.setItemMargin(0.0);
		plot.setOutlinePaint(null); 
		
		NumberAxis numberAxis=(NumberAxis)plot.getRangeAxis(); 
		numberAxis.setLabelFont(new Font("微软雅黑",Font.PLAIN,12));
		numberAxis.setTickLabelFont(new Font("微软雅黑",Font.PLAIN,12));   
		numberAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits()); //刻度轴刻度设置
		
		CategoryAxis categoryAxis=plot.getDomainAxis();
		categoryAxis.setLabelFont(new Font("微软雅黑",Font.PLAIN,12)); //水平底部字体
		categoryAxis.setTickLabelFont(new Font("微软雅黑",Font.PLAIN,11));
	}

	// 初始化图释样式
	public static void legendStyle(JFreeChart chart)
	{
		LegendTitle legend = chart.getLegend();
		legend.setBackgroundPaint(null); // 设置图释背景颜色
		legend.setBorder(0, 0, 0, 0); // 设置图释边框,顺序依次为top,left,bottom,right,都为0时表示边框为0
		if (legend != null) // 设置图释字体。防止中文乱码
		{
			legend.setItemFont(new Font("微软雅黑", Font.PLAIN, 12));
		}
	}

	@Override
	public String execute() throws Exception
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		JFreeChart chart = createChart(createDataSet());
		ServletOutputStream out = response.getOutputStream();
		ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
		out.flush();
		out.close();
		return null;
	}
}

折线图

package com.xuzengqiang.jfreechart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Ellipse2D;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

import com.opensymphony.xwork2.ActionSupport;

//直方条形图
@SuppressWarnings("all")
public class LineChartAction extends ActionSupport
{

	public static DefaultCategoryDataset createDataSet()
	{
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		dataset.addValue(1000, "男", "2009年");
		dataset.addValue(2000, "男", "2010年");
		dataset.addValue(2100, "男", "2011年");
		dataset.addValue(2000, "男", "2012年");
		dataset.addValue(2500, "男", "2013年");                                   
		dataset.addValue(3000, "男", "2014年");
		return dataset;
	}

	public static JFreeChart createChart(DefaultCategoryDataset dataset)
	{
		JFreeChart chart = ChartFactory.createLineChart("简单的折线图--湖南省人口普查", // 如果想要3D,可以createPieChart3D
				null, //主轴标签
				"人数", //横坐标名称
				dataset,
				PlotOrientation.VERTICAL,
				false, // 是否生成图例
				true, // 是否生成工具(提示)
				false // 是否生成URL链接
				);
		chart.setBackgroundPaint(Color.decode("#FFFFFF")); // 设置背景颜色,为null表示透明
		chart.setBorderVisible(false); // 设置边框线不可见
		
		titleStyle(chart);
		plotStyle(chart);
		return chart;
	}

	// 初始化标题样式
	public static void titleStyle(JFreeChart chart)
	{
		TextTitle title = chart.getTitle(); // 设置标题字体,防止中文乱码
		title.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		title.setPaint(Color.decode("#FF7700")); // 设置字体颜色
		
		TextTitle subtitle=new TextTitle("杨山村男性人口变化");
		subtitle.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		subtitle.setPaint(Color.decode("#3399CC"));
		subtitle.setPosition(RectangleEdge.BOTTOM);  //位于图标下方
		subtitle.setHorizontalAlignment(HorizontalAlignment.RIGHT); //靠右对齐
		chart.addSubtitle(subtitle);  //添加副标题
	}

	// 初始化数据区样式
	public static void plotStyle(JFreeChart chart)
	{
		CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.decode("#FFFFFF")); 
		plot.setRangeGridlinePaint(Color.decode("#3399CC")); //对齐线的颜色
		plot.setDomainGridlinesVisible(false); 
		
		LineAndShapeRenderer renderer=(LineAndShapeRenderer)plot.getRenderer();
		renderer.setDrawOutlines(true);
		renderer.setBaseFillPaint(Color.decode("#3399CC")); //设置转折点中的填充颜色
		renderer.setShapesVisible(Boolean.TRUE);
		renderer.setUseFillPaint(true);
		renderer.setSeriesStroke(0, new BasicStroke(3.0F));
		renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
		renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0));
		
		NumberAxis numberAxis=(NumberAxis)plot.getRangeAxis(); 
		numberAxis.setLabelFont(new Font("微软雅黑",Font.PLAIN,12));
		numberAxis.setTickLabelFont(new Font("微软雅黑",Font.PLAIN,12));   
		
		CategoryAxis categoryAxis=plot.getDomainAxis();
		categoryAxis.setLabelFont(new Font("微软雅黑",Font.PLAIN,12));
		categoryAxis.setTickLabelFont(new Font("微软雅黑",Font.PLAIN,11));
		categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI/6));
	}

	@Override
	public String execute() throws Exception
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		JFreeChart chart = createChart(createDataSet());
		ServletOutputStream out = response.getOutputStream();
		ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
		out.flush();
		out.close();
		return null;
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值