jfreechart

简介(百度百科):

JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。
JFreeChart截止2011年2月22日为止的相当不错的java图形解决方案,基本能够解决目前的图形方面的需求。

官网:http://www.jfree.org/jfreechart/

话说可以下载,本人,人品不好吧,官网的下载链接不管用!好在有以前的版本!

JFreeChart可以在java程序和Web程序中,使用,下面以web程序为例介绍:

    首先导入jar包,本人在资料库中会上传相关的jar包;

    下面的例子是我在书上弄到了例子,仅仅学习参考,是Strust2+JFreeChart1.0!

    前台的JSP:

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
                                                                       
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1><font color="blue">请选择喜欢的运动项目</font></h1>
<s:form action="viewResult">
<s:checkbox name="interest" label="足球" fieldValue="football" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="篮球" fieldValue="basketball" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="排球" fieldValue="volleyball" labelposition="left"></s:checkbox>
<s:checkbox name="interest" label="羽毛球" fieldValue="badminton" labelposition="left"></s:checkbox>
<!--
<s:checkboxlist list="#{'computer' : '计算机' , 'math' :  '数学'}" name="interest" label="浪曦" labelposition="top">
</s:checkboxlist>
 -->
                                                                    
                                                                    
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>


注意此代码已经将jar包导入!


struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
                                                
    <constant name="struts.custom.i18n.resources" value="message"></constant>
    <package name="struts2" extends="jfreechart-default">
        <action name="viewResult" class="com.cjg.action.ViewResultAction">
            <result name="success" type="chart">
            <param name="height">600</param>
                <param name="width">800</param>
            </result>
        </action>
    </package>
</struts>



最后是,Action:


package com.cjg.action;
import java.awt.Font;
import java.util.List;
import java.util.Map;
import org.jfree.chart.ChartFactory;
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.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ViewResultAction extends ActionSupport
{
    private JFreeChart chart;
    private List<String> interest;
    public JFreeChart getChart()
    {
        chart = ChartFactory.createBarChart3D("兴趣统计结果", "项目", "结果", this
                .getDataset(), PlotOrientation.VERTICAL, false, false, false);
                                      
        chart.setTitle(new TextTitle("兴趣统计结果",new Font("黑体",Font.BOLD,22)));
                                      
        CategoryPlot plot = (CategoryPlot)chart.getPlot();
                                      
        CategoryAxis categoryAxis = plot.getDomainAxis();
                                      
        categoryAxis.setLabelFont(new Font("宋体",Font.BOLD,22));
                                      
        categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
                                      
        NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
                                      
        numberAxis.setLabelFont(new Font("宋体",Font.BOLD,22));
                                      
        return chart;
    }
    public List<String> getInterest()
    {
        return interest;
    }
    public void setInterest(List<String> interest)
    {
        this.interest = interest;
    }
    @Override
    public String execute() throws Exception
    {
        return SUCCESS;
    }
    @SuppressWarnings("unchecked")
    private void increaseResult(List<String> list)
    {
        ActionContext context = ActionContext.getContext();
        Map map = context.getApplication();
        for (String str : list)
        {
            if (null == map.get(str))
            {
                map.put(str, 1);
            }
            else
            {
                map.put(str, (Integer) map.get(str) + 1);
            }
        }
    }
    @SuppressWarnings("unchecked")
    private CategoryDataset getDataset()
    {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        this.increaseResult(this.getInterest());
        ActionContext context = ActionContext.getContext();
        Map map = context.getApplication();
        dataset.setValue((Integer) map.get("football"), "", "足球");
        dataset.setValue((Integer) map.get("basketball"), "", "篮球");
        dataset.setValue((Integer) map.get("volleyball"), "", "排球");
        dataset.setValue((Integer) map.get("badminton"), "", "羽毛球");
        return dataset;
    }
}



web.xml的配置就不说了,这样你的JFreeChart便可以使用了。