求动态甘特图

小弟最近在项目中要用到甘特图,我用的是struts1.2,就是想从数据库查询出来的值生成甘特图,在网上看了不少没太明白,其中JFreeChart做的 都是自己写的固定数据,我把它改成查出来的值,总出异常,说参数异常
Task t=new Task(taskName, date(1, Calendar.SEPTEMBER, 2008), date(2, Calendar.NOVEMBER, 2009));向这种形式就可以,但我改成
Task t=new Task(taskName, date(day, month, year), date(day1, month1, year1));
其中给的参数taskName就是任务名,其余的都是int型的
函数如下
private static Date date(final int day,final int month,final int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Date result = calendar.getTime();
return result;
}
哪位大侠能给点提示,不胜感激,有实例最好,急等,由于小弟没有多少分只能给这点分了
[b]问题补充:[/b]
首先 谢谢你的回答 !~
这是我的代码 还有异常 你在帮忙看看
package com.sample.web.action;

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;
/**

  • start */ import java.awt.Color; import java.util.Calendar; import java.util.Date; import java.util.List; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.JFreeChart; import org.jfree.chart.entity.StandardEntityCollection; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.data.category.IntervalCategoryDataset; import org.jfree.data.gantt.Task; import org.jfree.data.gantt.TaskSeries; import org.jfree.data.gantt.TaskSeriesCollection;

/**

  • end */ import com.base.web.action.BaseAction; import com.edgdo.Constants; import com.edgdo.service.ProjectService; import com.edgdo.vo.TaskVO; import com.sample.web.form.ExamReportForm;

public class ExamReportAction extends BaseAction{
List list=null;
protected final ActionForward process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
ExamReportForm actForm = (ExamReportForm)form;
String forward ="init";
if(actForm.isEventInitial()){
Object object=this.getService(Constants.PROJECT_SERVICE, request);
ProjectService projectService=(ProjectService) object;
list=projectService.getCheckProject(actForm.getProjectId());
IntervalCategoryDataset dataset = createSampleDataset();

        JFreeChart chart = ChartFactory.createGanttChart(
            "Gantt Chart Demo",  // chart title
            "Task",              // domain axis label
            "Date",              // range axis label
            dataset,             // data
            true,                // include legend
            true,                // tooltips
            false                // urls
        );
       CategoryPlot plot = (CategoryPlot) chart.getPlot();
  //      plot.getDomainAxis().setMaxCategoryLabelWidthRatio(10.0f);
       CategoryItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);

         ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
          //If the last parameter is null, the chart is a "one time"-chart and will be deleted after the first serving.
          //If the last parameter is a session object, the chart remains until session time out.
          String filename = ServletUtilities.saveChartAsPNG(chart, 400 ,300, info,request.getSession());
          actForm.setGraphURL(this.getDefaultReportGraphURL(request)+filename);

    }
    return mapping.findForward(forward);
}

/**
 * Creates a sample dataset for a Gantt chart, using sub-tasks.  In general, you won't
 * hard-code the dataset in this way - it's done here so that the demo is self-contained.
 *
 * @return The dataset.
 */
private IntervalCategoryDataset createSampleDataset() {
    TaskSeries s1 = new TaskSeries("hello");
    for(int i=0;i<list.size();i++){
        TaskVO taskVO=(TaskVO) list.get(i);
        String taskName=taskVO.getCTASKNAME();
        String []s=taskVO.getCBEGTIMESTR().split("-");
        int year=Integer.parseInt(s[0]);
        int month=Integer.parseInt(s[1]);
        int day=Integer.parseInt(s[2]);
        String []ss=taskVO.getCENDTIMESTR().split("-");
        int year1=Integer.parseInt(ss[0]);
        int month1=Integer.parseInt(ss[1]);
        int day1=Integer.parseInt(ss[2]);

        Task t=new Task(taskName, date(day,month, year), date(day1,month1, year1));
         t.setPercentComplete(1.00);
         s1.add(t);
    }

    TaskSeriesCollection collection = new TaskSeriesCollection();
    collection.add(s1);

    return collection;
}

/**
 * Utility method for creating <code>Date</code> objects.
 *
 * @param day  the date.
 * @param month  the month.
 * @param year  the year.
 *
 * @return a date.
 */
private static Date date(final int day,final int month,final int year) {

    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    Date result = calendar.getTime();
    return result;

}

}

异常如下
[ERROR] 2009-04-23 14:37:43,515 method:org.apache.jsp.WEB_002dINF.jsp.GlobalError_jsp._jspService(GlobalError_jsp.java:66)
Error Occured: Requires start <= end.
java.lang.IllegalArgumentException: Requires start <= end.
at org.jfree.data.time.SimpleTimePeriod.(SimpleTimePeriod.java:78)
at org.jfree.data.time.SimpleTimePeriod.(SimpleTimePeriod.java:91)
at org.jfree.data.gantt.Task.(Task.java:102)
at com.sample.web.action.ExamReportAction.createSampleDataset(ExamReportAction.java:92)
at com.sample.web.action.ExamReportAction.process(ExamReportAction.java:46)
at com.base.web.action.BaseAction.execute(BaseAction.java:339)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at org.apache.struts.chain.commands.servlet.PerformForward.handleAsForward(PerformForward.java:113)
at org.apache.struts.chain.commands.servlet.PerformForward.perform(PerformForward.java:96)
at org.apache.struts.chain.commands.AbstractPerformForward.execute(AbstractPerformForward.java:54)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at com.base.web.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:63)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
[b]问题补充:[/b]
恩 这个问题确实解决了 居然是参数给反了 哎。。
那我还有个问题 就是我想控制一下甘特图的格式 在哪里设置啊 例如加个边框什么的
[b]问题补充:[/b]
好的 我试试 谢谢你这么热心解决 以后有不懂的在来问你 :idea:

3个回答

哦,
[ERROR] 2009-04-23 14:37:43,515 method:org.apache.jsp.WEB_002dINF.jsp.GlobalError_jsp._jspService(GlobalError_jsp.java:66)
Error Occured: Requires start <= end.
这个意思是说任务开始时间必须小于等于结束时间,你的两个date参数传反了吧,哈哈
这里
[code="java"] int year=Integer.parseInt(s[0]);
Task t=new Task(taskName, date(day,month, year), date(day1,month1, year1)); [/code]
改成
[code="java"] int year=Integer.parseInt(s[0]);
Task t=new Task(taskName, date(day1,month1, year1), date(day,month, year)); [/code]
试试

你说改成第二种就出错么?我这么试的没见报错啊!
[code="java"]

import java.util.Calendar;
import java.util.Date;

public class Test
{
public void main(String [] args)
{
int i = 0;
int j = 1;
int k = 2;
Task t1 = new Task("", date(1,2,4),date(0,2,3));
Task t2 = new Task("", date(i,j,k), date(i,j,k));

}

private static Date date(final int day,final int month,final int year) 
{
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, day);
    Date result = calendar.getTime();
    return result;
} 

}
class Task
{
public Task(String name, Date date1,Date date2)
{

}

}
[/code]
你看看是不是这样?

好多setMethod()都是啊
[code="java"]
setAntiAlias(boolean flag) 文字边界模糊
setBackgroundImage(Image image) 背景
setBackgroundImageAlignment(int alignment)
setBackgroundImageAlpha(float alpha) 背景透明度(0f-1f)
setBackgroundPaint(Paint paint) 背景色
setBorderPaint(Paint paint) 边界线条颜色
setBorderVisible(boolean visible) 边界线条是否可见
setBorder...
//等等 ,呵呵
[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐