jfreeChar入门,详细注释

项目下载地址:http://download.csdn.net/detail/w420372197/4180456

感谢“java web 整合开发 王者归来”这里的代码源自这本书

目录:

一、图形及代码:

1、饼形图

2.1、普通柱形图

2.2、XYDataset柱形图

3、折线图

4、区域图

5、时序图

6、组合图

二web应用jfreeChar(区域图)

三:DataUtils类代码(所有代码中均应用的类,得到随即数据)

一、图形及代码

1、饼形图

package com.helloweenvsfei.jfreechart;

import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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;
import org.jfree.data.general.PieDataset;

public class PieChart {
	public static void main(String[] agrs) throws IOException{
		// 打开一个输出流
		OutputStream outputStream=new FileOutputStream("PieChart.png");
		// 获取数据集对象
		PieDataset dataset = createPieDataset();
		// 创建图形对象
		JFreeChart jfreechart = ChartFactory.createPieChart3D("08年图书销量排行榜",
				dataset, true, true, false);
		// 获得图表区域对象
		PiePlot pieplot = (PiePlot) jfreechart.getPlot();
		// 设置图表区域的标签字体
		pieplot.setLabelFont(new Font("宋体", 0, 12));
		// 设置图表区域无数据时的默认显示文字
		pieplot.setNoDataMessage("没有销售数据");
		// 设置图表区域不是圆形,由于是3D的饼形图,建议设置为false
		pieplot.setCircular(false);
		// 设置图表区域文字与图表区域的间隔距离,0.02表示2%
		pieplot.setLabelGap(0.02D);
		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
				500, 270);
	}
	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static PieDataset createPieDataset() {
		// 创建饼形图数据集对象
		DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
		// 分别图形区域的说明和数据
		defaultpiedataset.setValue("JAVA", DataUtils.getRandomData());
		defaultpiedataset.setValue("C/C++", DataUtils.getRandomData());
		defaultpiedataset.setValue("PHP", DataUtils.getRandomData());
		defaultpiedataset.setValue("JavaScript", DataUtils.getRandomData());
		defaultpiedataset.setValue("Ajax", DataUtils.getRandomData());
		return defaultpiedataset;
	}

}

效果
:

jfreeChar入门,详细注释_第1张图片

2.1、普通柱形图

package com.helloweenvsfei.jfreechart;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class BarChart {
	public static void main(String[] agrs) throws IOException{
		// 打开一个输出流
		OutputStream outputStream=new FileOutputStream("BarChart.png");
		// 获取数据集对象
		CategoryDataset dataset = createDataset();
		// 创建图形对象
		JFreeChart jfreechart = ChartFactory.createBarChart3D("08年图书销售量排名",
				"按季度", "销量", dataset, PlotOrientation.VERTICAL, true, true,
				false);
		// 获得图表区域对象
		CategoryPlot categoryPlot = (CategoryPlot) jfreechart.getPlot();
		// 设置网格线可见
		categoryPlot.setDomainGridlinesVisible(true);
		// 获得x轴对象
		CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
		// 设置x轴显示的分类名称的显示位置,如果不设置则水平显示
		// 设置后,可以斜像显示,但分类角度,图表空间有限时,建议采用
		categoryAxis.setCategoryLabelPositions(CategoryLabelPositions
				.createUpRotationLabelPositions(0.39269908169872414D));
		categoryAxis.setCategoryMargin(0.0D);
		// 获显示图形对象
		BarRenderer3D barRenderer3d = (BarRenderer3D) categoryPlot
				.getRenderer();
		// 设置不显示边框线
		barRenderer3d.setDrawBarOutline(false);
		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
				500, 270);
	}

	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static CategoryDataset createDataset() {
		// 时间维度
		String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" };
		// 分类维度
		String[] category2 = { "JAVA", "C/C++", "PHP" };
		DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
		for (int i = 0; i < category1.length; i++) {
			String category = category1[i];
			for (int j = 0; j < category2.length; j++) {
				String cat = category2[j];
				// 模拟添加数据
				defaultdataset.addValue(DataUtils.getRandomData(), cat, category);
			}
		}
		return defaultdataset;
	}
}

效果:

jfreeChar入门,详细注释_第2张图片


2.2、XYDataset柱形图

package com.helloweenvsfei.jfreechart;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYBarDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class BarChart2 {
	public static void main(String[] agrs) throws IOException{
		// 打开一个输出流
		OutputStream outputStream=new FileOutputStream("BarChart2.png");
		// 获取数据集对象
		IntervalXYDataset dataset = createDataset();
		// 创建图形对象
		JFreeChart jfreechart = ChartFactory.createXYBarChart("08年图书销售量", null,
				false, "销量", dataset, PlotOrientation.VERTICAL, true, false,
				false);
		// 设置背景为白色
		jfreechart.setBackgroundPaint(Color.white);
		// 获得图表区域对象
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		// 设置区域对象背景为灰色
		xyplot.setBackgroundPaint(Color.lightGray);
		// 设置区域对象网格线调为白色
		xyplot.setDomainGridlinePaint(Color.white);
		xyplot.setRangeGridlinePaint(Color.white);
		// 获显示图形对象
		XYBarRenderer xybarrenderer = (XYBarRenderer) xyplot.getRenderer();
		// 设置不显示边框线
		xybarrenderer.setDrawBarOutline(false);

		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
				500, 270);
	}

	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static IntervalXYDataset createDataset() {
		// 创建基本数据
		XYSeries xyseries = new XYSeries("JAVA");
		XYSeries xyseries1 = new XYSeries("PHP");
		for (int i = 1; i <= 12; i++) {
			// 添加数据
			xyseries.add(i,DataUtils.getRandomData());
			xyseries1.add(i,DataUtils.getRandomData());
		}
		XYSeriesCollection xyseriescollection = new XYSeriesCollection();
		xyseriescollection.addSeries(xyseries);
		xyseriescollection.addSeries(xyseries1);
		return new XYBarDataset(xyseriescollection, 0.90000000000000002D);
	}
}
效果:

jfreeChar入门,详细注释_第3张图片
3、折线图

package com.helloweenvsfei.jfreechart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
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.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;

public class LineChart {
	public static void main(String[] agrs) throws IOException{

		// 打开一个输出流
		OutputStream outputStream=new FileOutputStream("LineChart.png");

		// 获取数据集对象
		CategoryDataset dataset = createDataset();
		// 创建图形对象
		JFreeChart jfreechart = ChartFactory.createLineChart("08年图书销售量",
				null, "销售量", dataset, PlotOrientation.VERTICAL, false, true,
				false);
		// 设置图表的子标题
		jfreechart.addSubtitle(new TextTitle("按月份"));
		// 创建一个标题
		TextTitle texttitle = new TextTitle("日期: " + new Date());
		// 设置标题字体
		texttitle.setFont(new Font("黑体", 0, 10));
		// 设置标题向下对齐
		texttitle.setPosition(RectangleEdge.BOTTOM);
		// 设置标题向右对齐
		texttitle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
		// 添加图表的子标题
		jfreechart.addSubtitle(texttitle);
		// 设置图表的背景色为白色
		jfreechart.setBackgroundPaint(Color.white);
		// 获得图表区域对象
		CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
		categoryplot.setBackgroundPaint(Color.lightGray);
		categoryplot.setRangeGridlinesVisible(false);
		// 获显示线条对象
		LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
				.getRenderer();
		lineandshaperenderer.setBaseShapesVisible(true);
		lineandshaperenderer.setDrawOutlines(true);
		lineandshaperenderer.setUseFillPaint(true);
		lineandshaperenderer.setBaseFillPaint(Color.white);
		// 设置折线加粗
		lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F));
		lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
		// 设置折线拐点
		lineandshaperenderer.setSeriesShape(0,
				new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));
		
		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
				500, 270);
	}
	
	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static CategoryDataset createDataset() {
		DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
		for (int i = 1; i <= 12; i++) {
			defaultdataset.addValue(DataUtils.getRandomData(), "JAVA", i + "月");
			defaultdataset.addValue(DataUtils.getRandomData(), "PHP", i + "月");

		}
		return defaultdataset;
	}

}

效果 :

jfreeChar入门,详细注释_第4张图片

4、区域图

package com.helloweenvsfei.jfreechart;

import java.awt.Color;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;

public class AreaChart {
	public static void main(String[] agrs) throws IOException{
		// 打开一个输出流
		OutputStream outputStream=new FileOutputStream("AreaChart.png");
		
		// 获取数据集对象
		CategoryDataset dataset = createDataset();
		// 创建图形对象
		JFreeChart jfreechart = ChartFactory.createAreaChart("08年图书销售量排名",
				"按季度", "销量", dataset, PlotOrientation.VERTICAL, true, true,
				false);
		// 获得图表区域对象
		CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
		//设置前景色为50%透明
		categoryplot.setForegroundAlpha(0.5F);
		//设置X轴偏移量
		categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
		//设置北景色为浅灰色
		categoryplot.setBackgroundPaint(Color.lightGray);
		//设置显示网格线
		categoryplot.setDomainGridlinesVisible(true);
		//设置网格线为白色
		categoryplot.setDomainGridlinePaint(Color.white);
		//设置显示边界线
		categoryplot.setRangeGridlinesVisible(true);
		//设置显示边界线为白色
		categoryplot.setRangeGridlinePaint(Color.white);
		//获得X轴对象
		CategoryAxis categoryaxis = categoryplot.getDomainAxis();
		//设置X轴标签位置为45度角
		categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
		//设置与X轴的补白为0
		categoryaxis.setLowerMargin(0.0D);
		categoryaxis.setUpperMargin(0.0D);
		
		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
				500, 270);

	}
	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static CategoryDataset createDataset() {
		// 时间维度
		String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" };
		// 分类维度
		String[] category2 = { "JAVA", "C/C++", "PHP" };
		DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
		for (int i = 0; i < category1.length; i++) {
			String category = category1[i];
			for (int j = 0; j < category2.length; j++) {
				String cat = category2[j];
				// 模拟添加数据
				defaultdataset.addValue(DataUtils.getRandomData(), cat, category);
			}
		}
		return defaultdataset;
	}


}

效果:

jfreeChar入门,详细注释_第5张图片

5、时序图

package com.helloweenvsfei.jfreechart;

import java.awt.BasicStroke;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
public class TimeChart {
	public static void main(String[] agrs) throws IOException{
		// 打开一个输出流
		OutputStream outputStream=new FileOutputStream("TimeChart.png");
		// 获取数据集对象
		XYDataset dataset = createDataset();
		// 创建图形对象
		JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图","POLO历史价格", "价格", dataset, false, false, false);
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		//获得折线显示对象
		XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
		//设置不显示折线
		xylineandshaperenderer.setBaseShapesVisible(false);
		//设置折线的粗细
		xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(0.5F, 1, 1, 5F, new float[] { 5F, 10F }, 0.0F));
		xyplot.setRenderer(xylineandshaperenderer);
		
		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
				500, 270);
	}

	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static XYDataset createDataset() {
		//创建时序对象
		TimeSeries timeseries = new TimeSeries("JAVA");
		//初始化日期为1990年1月1日
		Day day = new Day(1, 1, 1990);
		double d = 100000D;
		//添加1000条数据
		for (int i = 0; i < 4000; i++)
			try {
				//随机取出100附近数
				d = (d + Math.random()) - 0.5D;
				//添加到时序对象
				timeseries.add(day, new Double(d));
				//之后的一天
				day = (Day) day.next();
			} catch (SeriesException seriesexception) {

			}
		//返回XYDataset数据集
		return new TimeSeriesCollection(timeseries);
	}
}

效果:

jfreeChar入门,详细注释_第6张图片

6、组合图

package com.helloweenvsfei.jfreechart;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

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.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class CombinedChart {
	public static void main(String[] agrs) throws IOException{

		// 打开一个输出流
		OutputStream outputStream=new FileOutputStream("CombinedChart.png");
		// 创建第一个数据集
		CategoryDataset categorydataset = createDataset();
		NumberAxis numberaxis = new NumberAxis("销量");
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
		CategoryPlot categoryplot = new CategoryPlot(categorydataset, null,
				numberaxis, lineandshaperenderer);
		categoryplot.setDomainGridlinesVisible(true);
		// 创建第二个数据集
		CategoryDataset categorydataset1 = createDataset();
		NumberAxis numberaxis1 = new NumberAxis("销量");
		BarRenderer barrenderer = new BarRenderer();
		barrenderer
				.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
		CategoryPlot categoryplot1 = new CategoryPlot(categorydataset1, null,
				numberaxis1, barrenderer);
		categoryplot1.setDomainGridlinesVisible(true);
		// 创建组合数据集
		CategoryAxis categoryaxis = new CategoryAxis();
		CombinedDomainCategoryPlot combineddomaincategoryplot = new CombinedDomainCategoryPlot(
				categoryaxis);
		combineddomaincategoryplot.add(categoryplot, 2);
		combineddomaincategoryplot.add(categoryplot1, 1);
		// 创建图表对象
		JFreeChart jfreechart = new JFreeChart("组合图", null,
				combineddomaincategoryplot, true);

		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(outputStream, jfreechart,
				500, 270);
	}

	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static CategoryDataset createDataset() {
		// 时间维度
		String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" };
		// 分类维度
		String[] category2 = { "JAVA", "C/C++", "PHP" };
		DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
		for (int i = 0; i < category1.length; i++) {
			String category = category1[i];
			for (int j = 0; j < category2.length; j++) {
				String cat = category2[j];
				// 模拟添加数据
				defaultdataset.addValue(DataUtils.getRandomData(), cat,
						category);
			}
		}
		return defaultdataset;
	}

}
效果:

jfreeChar入门,详细注释_第7张图片

二、web应用jfreeChar

以区域图为例,首先web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>AreaChartServlet</servlet-name>
    <servlet-class>com.helloweenvsfei.jfreechart.AreaChartServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AreaChartServlet</servlet-name>
    <url-pattern>/servlet/AreaChartServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

然后修改流 :

1,response.setContentType("image/png");

2,ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfreechart,500, 270);最后代码如下:

package com.helloweenvsfei.jfreechart;

import java.awt.Color;
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.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;

public class AreaChartServlet extends HttpServlet {

	private static final long serialVersionUID = 5852523271721922574L;
	public AreaChartServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 设置返回类型为图片
		response.setContentType("image/png");
		// 获取数据集对象
		CategoryDataset dataset = createDataset();
		// 创建图形对象
		JFreeChart jfreechart = ChartFactory.createAreaChart("08年图书销售量排名",
				"按季度", "销量", dataset, PlotOrientation.VERTICAL, true, true,
				false);
		// 获得图表区域对象
		CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
		//设置前景色为50%透明
		categoryplot.setForegroundAlpha(0.5F);
		//设置X轴偏移量
		categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
		//设置北景色为浅灰色
		categoryplot.setBackgroundPaint(Color.lightGray);
		//设置显示网格线
		categoryplot.setDomainGridlinesVisible(true);
		//设置网格线为白色
		categoryplot.setDomainGridlinePaint(Color.white);
		//设置显示边界线
		categoryplot.setRangeGridlinesVisible(true);
		//设置显示边界线为白色
		categoryplot.setRangeGridlinePaint(Color.white);
		//获得X轴对象
		CategoryAxis categoryaxis = categoryplot.getDomainAxis();
		//设置X轴标签位置为45度角
		categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
		//设置与X轴的补白为0
		categoryaxis.setLowerMargin(0.0D);
		categoryaxis.setUpperMargin(0.0D);
		
		// 将图表已数据流的方式返回给客户端
		ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfreechart,500, 270);

	}
	/**
	 * 返回数据集
	 * 
	 * @return
	 */
	private static CategoryDataset createDataset() {
		// 时间维度
		String[] category1 = { "第一季度", "第二季度", "第三季度", "第四季度" };
		// 分类维度
		String[] category2 = { "JAVA", "C/C++", "PHP" };
		DefaultCategoryDataset defaultdataset = new DefaultCategoryDataset();
		for (int i = 0; i < category1.length; i++) {
			String category = category1[i];
			for (int j = 0; j < category2.length; j++) {
				String cat = category2[j];
				// 模拟添加数据
				defaultdataset.addValue(DataUtils.getRandomData(), cat, category);
			}
		}
		return defaultdataset;
	}


}
三:DataUtils类代码

package com.helloweenvsfei.jfreechart;

import java.util.Random;

public class DataUtils {
	private static Random random = new Random();
	private static final int MAX_NUMBER = 100;

	/**
	 * 	随机在0到100间取数
	 * @return 
	 */
	public static int getRandomData() {
		return random.nextInt(MAX_NUMBER);
	}
}



你可能感兴趣的:(java,String,jfreechart,Class,图形,dataset)