各位大侠,紧急求助,JfreeChart怎么做呀???

解决方案 »

  1.   

    http://download.csdn.net/source/992378
      

  2.   

    package com.pro_crm.jfreechart;import java.awt.Color;
    import java.awt.Font;
    import java.awt.GradientPaint;
    import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.http.HttpSession;
    import javax.servlet.jsp.JspWriter;import org.jfree.chart.ChartRenderingInfo;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.entity.StandardEntityCollection;
    import org.jfree.chart.servlet.ServletUtilities;
    import org.jfree.chart.title.TextTitle;public abstract class JfreeChart { protected int width = 540;// 图象宽度 protected int height = 380; // 图象高度 protected String chartTitle;// 图表标题 protected String subTitle;// 副标题 protected String mapName;// map名称 protected String contextPath = "";// 容器路径 protected String servletURI = "/servlet/DisplayChart";// 映射路径 protected JspWriter out;// out实例 protected HttpSession session;// Session实例 protected JFreeChart jfreeChart;// 图表实例 private String fileName; private String graphURL; public void setChartTitle(String chartTitle) {
    this.chartTitle = chartTitle;
    } public void setContextPath(String contextPath) {
    this.contextPath = contextPath;
    } public void setHeight(int height) {
    this.height = height;
    } public void setSession(HttpSession session) {
    this.session = session;
    } public void setSubtitle(String subtitle) {
    this.subTitle = subtitle;
    } public void setWidth(int width) {
    this.width = width;
    } public void setMapName(String mapName) {
    this.mapName = mapName;
    } public void setOut(JspWriter out) {
    this.out = out;
    } private ChartRenderingInfo info = new ChartRenderingInfo(
    new StandardEntityCollection()); // 定义绘图区
    protected abstract void definePlot() throws Exception; // 生成图片
    protected abstract String draw() throws Exception; // 定义图片
    protected void defineJfreeChart() {
    // 设置背景颜色
    jfreeChart.setBackgroundPaint(new GradientPaint(0, 0, Color.YELLOW, 0,
    height, Color.GREEN, false));
    TextTitle title = jfreeChart.getTitle();
    title.setFont(new Font("汉真广标", Font.BOLD, 21));
    title.setPaint(Color.RED);
    if (subTitle.length() > 0) {
    jfreeChart.addSubtitle(new TextTitle("——" + subTitle));
    }
    } // 绘制普通图表
    protected String drawCommon() {
    // 设置图片名称的前缀
    ServletUtilities.setTempFilePrefix("Pro_Crm_Chart_");
    try {
    // 生成统计图片并返回图片名称
    fileName = ServletUtilities.saveChartAsPNG(jfreeChart, width,
    height, info, session);
    } catch (IOException e) {
    e.printStackTrace();
    }
    graphURL = contextPath + servletURI + "?filename=" + fileName;
    return graphURL;
    } // 绘制热点图表
    protected String drawHotspot(boolean useOverLibForToolTips) {
    // 设置图片名称的前缀
    ServletUtilities.setTempFilePrefix("Pro_Crm_Chart_");
    PrintWriter printWriter = new PrintWriter(out);
    try {
    // 生成统计图片并返回图片名称
    fileName = ServletUtilities.saveChartAsPNG(jfreeChart, width,
    height, info, session);
    // 向JSP页输出统计图的Map信息
    ChartUtilities.writeImageMap(printWriter, mapName, info,
    useOverLibForToolTips);
    } catch (IOException e) {
    e.printStackTrace();
    }
    graphURL = contextPath + servletURI + "?filename=" + fileName;
    return graphURL;
    }}
      

  3.   

    package com.pro_crm.jfreechart;import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GradientPaint;import org.jfree.chart.ChartFactory;
    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.chart.renderer.category.BarRenderer;
    import org.jfree.data.category.DefaultCategoryDataset;//柱形图
    public class Histogram extends JfreeChart {
    private String xTitle;// X轴名称 private String yTitle;// Y轴名称 private String[] cutline;// 图例名称 private String[] category;// 统计分类 private Double[][] data;// 绘图数据 private CategoryPlot plot;// 绘图区实例 private String picName; public String getPicName() {
    return picName;
    } public void setPicName(String picName) {
    this.picName = picName;
    } public void setCategory(String[] category) {
    this.category = category;
    } public void setCutline(String[] cutline) {
    this.cutline = cutline;
    } public void setData(Double[][] data) {
    this.data = data;
    } public void setXTitle(String title) {
    xTitle = title;
    } public void setYTitle(String title) {
    yTitle = title;
    } // 定义分类轴
    private void defineX() {
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 12));// 设置坐标轴标题字体
    domainAxis.setLabelPaint(Color.BLACK);// 设置坐标轴标题颜色
    domainAxis.setTickMarkStroke(new BasicStroke(1.6f));// 设置坐标标记大小
    domainAxis.setTickMarkPaint(Color.BLACK);// 设置坐标标记颜色
    } // 定义数值轴
    private void defineY() {
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 12));// 设置坐标轴标题字体
    rangeAxis.setLabelPaint(Color.BLACK);// 设置坐标轴标题颜色
    rangeAxis.setTickMarkStroke(new BasicStroke(1.6f));// 设置坐标标记大小
    rangeAxis.setTickMarkPaint(Color.BLACK);// 设置坐标标记颜色
    } // 定义柱形
    private void defineRenderer() {
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);// 设置是否绘制柱形的轮廓线,默认值为true
    // 设置中间柱形的填充色
    if (cutline.length % 2 == 1) {
    Color upColor = Color.MAGENTA;
    Color downColor = Color.BLUE;
    GradientPaint gp = new GradientPaint(0, 0, upColor.brighter(), 0,
    height, downColor.darker());
    renderer.setSeriesPaint(cutline.length / 2, gp);
    }
    } // 定义绘图区
    protected void definePlot() throws Exception {
    plot = jfreeChart.getCategoryPlot(); plot.setBackgroundPaint(Color.WHITE);// 设置绘图区的背景颜色 plot.setBackgroundAlpha(0.9f);// 设置绘图区的透明度 plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true
    plot.setRangeGridlinePaint(Color.RED);// 设置水平方向背景线颜色 plot.setDomainGridlinesVisible(true);// 设置是否显示垂直方向背景线,默认值为false
    plot.setDomainGridlinePaint(Color.RED);// 设置垂直方向背景线颜色
    } // 生成图片
    public String draw() throws Exception {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (int m = 0; m < cutline.length; m++) {
    for (int n = 0; n < category.length; n++) {
    dataset.addValue(data[m][n], cutline[m], category[n]);
    }
    } jfreeChart = ChartFactory.createBarChart3D(chartTitle, // 图表标题
    xTitle, // X轴标题
    yTitle, // Y轴标题
    dataset, // 绘图数据集
    PlotOrientation.VERTICAL, // 柱形图绘制方向
    true, // 是否显示图例
    true, // 在柱形上是否提供热点信息
    false // 在柱形上是否提供热点链接
    ); this.defineJfreeChart();
    this.definePlot();
    this.defineX();
    this.defineY();
    this.defineRenderer(); // return this.drawHotspot(false);
    return drawCommon();
    }}
    网上多的是代码,lz自己去找找吧
      

  4.   

    Action的调用:package com.pro_crm.jfreechart.action;import java.util.Iterator;
    import java.util.List;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.action.DynaActionForm;
    import org.apache.struts.actions.DispatchAction;import com.pro_crm.bean.ProService;
    import com.pro_crm.bean.ProSubOrder;
    import com.pro_crm.jfreechart.Caky;
    import com.pro_crm.jfreechart.Histogram;
    import com.pro_crm.jfreechart.Linear;
    import com.pro_crm.right.service.RightServerDao;
    import com.pro_crm.stat.service.StatServerDao;@SuppressWarnings("unchecked")
    public class StatjfreechartAction extends DispatchAction { private Caky caky;// 饼图
    private Linear linear;// 折线图
    private Histogram histogram;// 柱形图
    private RightServerDao rightServer;
    private StatServerDao statServerDao; public void setStatServerDao(StatServerDao statServerDao) {
    this.statServerDao = statServerDao;
    } public void setHistogram(Histogram histogram) {
    this.histogram = histogram;
    } public void setRightServer(RightServerDao rightServer) {
    this.rightServer = rightServer;
    } public void setLinear(Linear linear) {
    this.linear = linear;
    } public void setCaky(Caky caky) {
    this.caky = caky;
    } public ActionForward clientJfreeChart(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) {
    try {
    List<ProSubOrder> list = (List) request.getSession().getAttribute(
    "clientInfo");
    caky.setChartTitle("客户贡献百分比统计");
    String[] cutline = new String[list.size()];
    double[] data = new double[list.size()];
    int i = 0;
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    ProSubOrder subOrder = (ProSubOrder) iterator.next();
    cutline[i] = subOrder.getProOrder().getClientName();
    data[i] = (double) subOrder.getSubOrderPrice();
    i++;
    }
    caky.setCutline(cutline);
    caky.setData(data);
    caky.setContextPath(request.getContextPath());
    caky.setMapName("clientInfo");
    caky.setSubtitle(request.getSession().getAttribute("clientYear")
    .toString());
    caky.setRelatingPage("../../login.jsp");
    caky.setSession(request.getSession());
    String graphURL = caky.draw();
    request.getSession().setAttribute("graphURL", graphURL);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return mapping.findForward("jfreechart");
    } public ActionForward dictJfreeChart(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    DynaActionForm statjfreechartForm = (DynaActionForm) form;
    try {
    String style = "clientLevelLabel";
    if (statjfreechartForm.getString("queryStyle").equals("按等级")) {
    style = "clientLevelLabel";
    } else if (statjfreechartForm.getString("queryStyle")
    .equals("按信用度")) {
    style = "clientCredit";
    } else if (statjfreechartForm.getString("queryStyle")
    .equals("按满意度")) {
    style = "clientSatisfy";
    }
    String hql = "select " + style + ",count(" + style
    + ")as clientcount" + " from ProClient group by " + style;
    List<ProSubOrder> list = (List) rightServer.queryRightByHQL(hql,
    new Object[] {});
    String[] cutline = new String[list.size()];
    double[] data = new double[list.size()];
    int i = 0;
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    Object[] obj = (Object[]) iterator.next();
    if (obj[0] != null) {
    if (!obj[0].toString().equals("")) {
    cutline[i] = obj[0].toString();
    data[i] = Double.parseDouble(obj[1].toString());
    i++;
    }
    }
    }
    caky.setChartTitle("客户构成分析百分比统计");
    caky.setCutline(cutline);
    caky.setData(data);
    caky.setContextPath(request.getContextPath());
    caky.setMapName("dictInfo");
    caky.setSubtitle(statjfreechartForm.getString("queryStyle"));
    caky.setRelatingPage("../../login.jsp");
    caky.setSession(request.getSession());
    String graphURL = caky.draw();
    request.getSession().setAttribute("graphURL", graphURL);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return mapping.findForward("jfreechart");
    } public ActionForward lostJfreeChart(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    try {
    List<ProSubOrder> list = (List) request.getSession().getAttribute(
    "clientInfo");
    List listYear = (List) request.getSession().getAttribute(
    "clientYearInfo");
    String[] cutline = new String[list.size()];
    String[] category = new String[listYear.size()];
    double[][] data = new double[cutline.length][category.length];
    // 加载客户贡献名称
    int i = 0;
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    ProSubOrder subOrder = (ProSubOrder) iterator.next();
    cutline[i] = subOrder.getProOrder().getClientName();
    i++;
    }
    // 加载客户贡献时间
    for (int j = 0; j < listYear.size(); j++) {
    category[j] = listYear.get(j).toString();
    }
    // 加载客户贡献金额
    for (int m = 0; m < cutline.length; m++) {
    for (int n = 0; n < category.length; n++) {
    List result = (List) statServerDao.queryProClientByInfo(
    cutline[m], category[n]);
    String str = "0";
    if (result.size() > 0) {
    Object[] obj = (Object[]) result.get(0);
    str = obj[3].toString();
    }
    data[m][n] = Double.parseDouble(str);
    }
    }
    linear.setChartTitle("客户贡献走势图");
    linear.setSubtitle("走势图");
    linear.setXTitle("统计时间");
    linear.setYTitle("净收益(单位:元)");
    linear.setCutline(cutline);
    linear.setCategory(category);
    linear.setData(data);
    linear.setContextPath(request.getContextPath());
    linear.setSession(request.getSession());
    String graphURL = linear.draw();
    request.getSession().setAttribute("graphURL", graphURL);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return mapping.findForward("jfreechart");
    } public ActionForward serverJfreeChart(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) {
    DynaActionForm statjfreechartForm = (DynaActionForm) form;
    try {
    List list = (List) request.getSession().getAttribute("serverInfo");
    String[] cutline = new String[list.size()];
    String[] category = null;
    Double[][] data = null;
    String time = "统计时间";
    if (statjfreechartForm.getString("serverYear").equals("全部")) {
    List listYear = (List) rightServer.queryRightByHQL(
    "select distinct serviceCreateDate from ProService",
    new Object[] {});
    category = new String[listYear.size()];
    for (int i = 0; i < listYear.size(); i++) {
    category[i] = listYear.get(i).toString().substring(0, 4);
    }
    data = new Double[cutline.length][category.length];
    int i = 0;
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    ProService server = (ProService) iterator.next();
    cutline[i] = server.getServiceType();
    i++;
    }
    String createDate = null;
    String createType = null;
    for (int m = 0; m < cutline.length; m++) {
    for (int n = 0; n < category.length; n++) {
    createDate = category[n];
    createType = cutline[m];
    String hql = "select count(serviceType) from ProService where serviceCreateDate like '%"
    + createDate
    + "%' and serviceType ='"
    + createType + "'";
    List result = (List) rightServer.queryRightByHQL(hql,
    new Object[] {});
    String str = "0";
    if (result.size() > 0) {
    str = result.get(0).toString();
    }
    data[m][n] = Double.valueOf(str);
    }
    }
    } else {
    String createDate = statjfreechartForm.getString("serverYear");// 统计时间;
    time += createDate;
    category = new String[] { "第1季度", "第2季度", "第3季度", "第4季度" };
    data = new Double[cutline.length][category.length];
    int i = 0;
    for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    ProService server = (ProService) iterator.next();
    cutline[i] = server.getServiceType();
    i++;
    }
    String createType = null;
    int[] index = new int[] { 1, 4, 7, 10 };
    for (int m = 0; m < cutline.length; m++) {
    for (int n = 0; n < category.length; n++) {
    createType = cutline[m];
    String hql = "select count(serviceCreateDate) from ProService where "
    + "date_format(serviceCreateDate,'%Y-%c-%d %h:%i:%s')>date_format('"
    + createDate
    + "-"
    + index[n]
    + "-1"
    + "','%Y-%c-%d %h:%i:%s')"
    + " and date_format(serviceCreateDate,'%Y-%c-%d %h:%i:%s')<date_format('"
    + createDate
    + "-"
    + (index[n] + 2)
    + "-30"
    + "','%Y-%c-%d %h:%i:%s') and serviceType ='"
    + createType
    + "' and serviceCreateDate like '%"
    + createDate + "%'";
    List result = (List) rightServer.queryRightByHQL(hql,
    new Object[] {});
    String str = "0";
    if (result.size() > 0) {
    str = result.get(0).toString();
    }
    data[m][n] = Double.valueOf(str);
    }
    }
    }
    histogram.setChartTitle("客户服务分析分类统计");
    histogram.setSubtitle("客户服务统计");
    histogram.setXTitle(time);
    histogram.setYTitle("服务数量");
    histogram.setCutline(cutline);
    histogram.setCategory(category);
    histogram.setData(data);
    histogram.setContextPath(request.getContextPath());
    histogram.setMapName("serverInfo");
    histogram.setSession(request.getSession());
    String graphURL = histogram.draw();
    request.getSession().setAttribute("graphURL", graphURL);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return mapping.findForward("jfreechart");
    }
    }