最直接方法,先构造一个dataset,然后构造chart,然后就可以生成图片,或者生成数据流用servlet显示出来。

解决方案 »

  1.   

    1.构造对象TimeSeries
    2.使用TimeSeries对象的add(param1, param2)方法设置值
      其中参数一为季度时间;参数二为当前季度对应的数值
    3.使用JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(p1,p2...p7);构造jfreechart对象
      其中1:标题; 2:X轴标题; 3:Y轴标题; 4:new TimeSeriesCollection(TimeSeries对象)
      5:true; 6:true; 7:false有了JFC对象,后边估计我就不用再多说了!
      

  2.   

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import java.awt.Color;
    import java.io.*;import org.jfree.chart.ChartFactory;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.renderer.category.BarRenderer3D;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.chart.axis.ValueAxis;
    import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
    import org.jfree.chart.axis.AxisLocation;
    import org.jfree.data.general.DatasetUtilities;import com.TCProject.database.OperateDB;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;public class StatStore extends Action{
    public ActionForward execute(ActionMapping actionMapping,
    ActionForm actionForm, HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    response.setHeader("cache-control","no-cache"); 
            response.setHeader("cache-control","no-store");
        response.setDateHeader("expires", 0); 
        response.setHeader("pragma","no-cache"); 
    OperateDB db = new OperateDB();
    PreparedStatement stam = null;
    PreparedStatement stam1 = null;
    ResultSet rs = null;
    ResultSet rs1 = null;
    //Vector list= new Vector();
    int count=0;
    int i=0;
    int j=0;
    int k=0;

    String startdate = request.getParameter("startdate");
    String enddate = request.getParameter("enddate");
    String sql = "";
    try{
    sql="select count(*) from T_STORE where 1=1";
    if (startdate != null && !startdate.equals("")&&!startdate.equals("null") && enddate != null
    && !enddate.equals("")&& !enddate.equals("null")) {
    sql += " and TO_CHAR(POST_TIME,'YYYY-MM-DD')>='"
    + startdate
    + "' and TO_CHAR(POST_TIME,'YYYY-MM-DD')<='"
    + enddate + "'";
    }
        stam = db.prepareStatement(sql);
        rs = stam.executeQuery(); 
        while (rs.next()) {
         count=rs.getInt(1);
        
        }
    }catch (SQLException e) {
    throw e;
    }  finally {
    if (rs != null)
    rs.close();
    if (stam != null)
    stam.close();
    if (db != null)
    db.close(); }
     
    String[] rowKeys = { "订单数量", "总金额" };
    String  columnKeys[]= new String[count];
    double[][] data = new double[2][count];
    OperateDB db1 = new OperateDB();
     
    try {  
      
       sql="select STORE_ID ,STORE_NAME  from T_STORE where 1=1";
      if (startdate != null && !startdate.equals("")&&!startdate.equals("null") && enddate != null
    && !enddate.equals("")&& !enddate.equals("null")) {
    sql += " and TO_CHAR(POST_TIME,'YYYY-MM-DD')>='"
    + startdate
    + "' and TO_CHAR(POST_TIME,'YYYY-MM-DD')<='"
    + enddate + "'";
    }
      
       stam = db1.prepareStatement(sql);
       rs = stam.executeQuery();
    while (rs.next()) {  
              columnKeys[i++]= rs.getString(2) ;
       sql="select count(ORDER_ID) , sum(ALL_PRICE) from BUS_LINE_ITEM where STORE_ID='"+rs.getInt(1)+"'";
       OperateDB db2 = new OperateDB();
       try{       
       stam1 = db2.prepareStatement(sql);
    rs1 = stam1.executeQuery();
       while (rs1.next()) {   
        data[0][j++]=rs1.getDouble(1);
        data[1][k++]=rs1.getDouble(2);  
     
       } 
       }catch (SQLException e) {
    throw e;
       }finally {
    if (rs1!= null)
    rs1.close();
    if (stam1!= null)
    stam1.close();
    if (db2!= null)
    db2.close();
    }
       
    }


    } catch (SQLException e) {
    throw e;
    } finally {
    if (rs != null)
    rs.close();
    if (stam!= null)
    stam.close();
    if (db1!= null)
    db1.close();
    }

    JFreeChart chart=null;
    CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
    rowKeys, columnKeys, data);

    chart = ChartFactory.createBarChart3D("商家统计图", null,
    null, dataset, PlotOrientation.VERTICAL, true, false, false);

    //System.out.println(storestatus);
    chart.setBackgroundPaint(Color.WHITE);
    CategoryPlot plot = chart.getCategoryPlot();
    //CategoryAxis domainAxis = plot.getDomainAxis();
    //domainAxis.setVisible(false);
    //plot.setDomainAxis(domainAxis);
    ValueAxis rangeAxis = plot.getRangeAxis();
    //设置最高的一个 Item 与图片顶端的距离
    rangeAxis.setUpperMargin(0.15);
    //设置最低的一个 Item 与图片底端的距离
    rangeAxis.setLowerMargin(0.15);
    plot.setRangeAxis(rangeAxis);
    BarRenderer3D renderer = new BarRenderer3D();
    renderer.setBaseOutlinePaint(Color.BLACK);
    //设置 Wall 的颜色<BR>sx
    renderer.setWallPaint(Color.gray);
    //设置订单数量,总金额代表的柱的颜色
    renderer.setSeriesPaint(0, new Color(0, 0, 255));
    //renderer.setSeriesPaint(1, new Color(0, 100, 255));
    renderer.setSeriesPaint(1, Color.GREEN);
    //设置每个地区所包含的平行柱的之间距离
    renderer.setItemMargin(0.1);
    //显示每个柱的数值,并修改该数值的字体属性<BR>
    renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setItemLabelsVisible(true);
    plot.setRenderer(renderer);
    //设置柱的透明度<BR>
    plot.setForegroundAlpha(0.6f);
    //设置显示位置<BR>
    plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    //String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,null, session);
    response.reset();
    response.setContentType("image/jpeg;charset=GBK");
    OutputStream os = response.getOutputStream();
    ChartUtilities.writeChartAsJPEG(os, 100, chart, 500, 300, null);
    return actionMapping.findForward("success");
    }}
      

  3.   

    http://www.java2s.com/Code/Java/Chart/JFreeChartLineChartDemo5showingtheuseofacustomdrawingsupplier.htm季度在自己输入点的时候,只输入它不就行了么