jfreechart使用心得
来源:互联网 发布:厦门理工软件学院学费 编辑:程序博客网 时间:2024/05/20 18:15
最近要做一个生成甘特图的web程序,听了网友的建议使用了jfreechart,发现这东西真不是一般的难用啊~~~源代码一大堆,光那一长串的java包就能吓死人,最坑爹的是他没有开发文档,确切的说是没有免费的开发文档……小弟我苦苦钻研了好久,才终于实现了我想要的功能,这里拿来跟大家分享一下~
完整代码如下
//ProgressInfo是存储一个阶段计划的结构
List<ProgressInfo> proList = this.getProgressList(projectid,autoClose);
//根据计划列表数据创建一个任务集的dataset
final TaskSeries s1 = new TaskSeries("日程表");
for(int i=0;i<proList.size();i++)
{
ProgressInfo pi = proList.get(i);
final Task t1 = new Task(pi.getName(), date(pi.getPlanBegin()), date(pi.getPlanEnd()) );
t1.setPercentComplete((float)pi.getPercentage()/100.0);
s1.add(t1);
}
final TaskSeriesCollection dataset = new TaskSeriesCollection();
dataset.add(s1);
//set titles and fonts
ProjectMng pm = new ProjectMng();
ProjectInfo pi = pm.getProjectInfo(projectid, true);
JFreeChart jfc = ChartFactory.createGanttChart(pi.getName(), "项目各阶段详细实施计划", "项目周期", dataset, true, true, true);
//以下部分用来解决中文支持问题,必须对涉及到中文的地方进行字体的显示设置,否则会出现乱码
jfc.getTitle().setFont(new Font("黑体", Font.BOLD, 15));
CategoryPlot plot = jfc.getCategoryPlot();
//纵轴字体
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));
DateAxis da = (DateAxis)plot.getRangeAxis(0);
//设置日期的显示格式
da.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
//横轴框里的标题字体
jfc.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 15));
//横轴列表字体
plot.getDomainAxis().setTickLabelFont(new Font("新宋体", 1, 12));
//横轴小标题字体
plot.getDomainAxis().setLabelFont(new Font("新宋体", 1, 12));
//创建map信息
String imgmap="";
FileOutputStream fop = null;
try { System.out.println("----->begin!!");
ChartRenderingInfo info = new ChartRenderingInfo();
//Generate ImgMap
BarRenderer render = (BarRenderer)plot.getRenderer();
CustomCategoryURLGenerator a = new CustomCategoryURLGenerator();
//strList存储了与任务集对应的链接地址信息
List<String> strList = new ArrayList<String>();
for(int i=0;i<proList.size();i++)
{
ProgressInfo temp = proList.get(i);
strList.add("UpdateProgress.jsp?pid="+temp.getId());
}
a.addURLSeries(strList);
render.setBaseItemURLGenerator(a);
//根据任务条目调整图片高度,使每个任务条的宽度基本恒定,如需生成指定大小的图片同时希望任务条宽度固定,则需对render信息做显示设置
int height = proList.size()*70;
fop = new FileOutputStream(path+"Gantt//gantt"+pi.getId()+".jpg");
//生成甘特图
ChartUtilities.writeChartAsJPEG(fop,1f, jfc, 800, height,info);
//把生成的imgmap以字符串形式返回,也可输出到某一文件中
imgmap = ChartUtilities.getImageMap("test", info);
System.out.println("----->successful!!");
return imgmap;
} catch (IOException e)
{ e.printStackTrace(); return imgmap;}
finally {
try {
fop.close();
} catch (IOException e) { e.printStackTrace(); }
}
注:其中,ProgressInfo,ProjectInfo,ProjectMng为本人自定义的数据结构,作用分别为存储计划信息,存储项目信息,项目管理功能
以上为生成甘特图所需的jpg图片及map信息的过程,接着,只需要在页面中引入图片及map信息即可,
<img src="../Gantt/gantt<%=id%>.jpg" usemap="#gantt">
生成的map文件如下:
<map id="gantt" name="gantt">
<area shape="rect"coords="592,205,756,235" title="2011-5-11- 2011-5-17" alt=""href="UpdateProgress.jsp?id=5"/>
<area shape="rect"coords="373,162,564,192" title="2011-5-3- 2011-5-10" alt=""href="UpdateProgress.jsp?id=4"/>
<area shape="rect"coords="236,120,400,150" title="2011-4-28- 2011-5-4" alt=""href="UpdateProgress.jsp?id=3"/>
<area shape="rect"coords="127,77,209,107" title="2011-4-24- 2011-4-27" alt=""href="UpdateProgress.jsp?id=2"/>
</map>
为了方便大家,顺便把以上代码中用到的头文件给大家介绍下
import java.awt.Font; import java.io.FileOutputStream; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.DateAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.urls.CustomCategoryURLGenerator; import org.jfree.data.gantt.Task; import org.jfree.data.gantt.TaskSeries; import org.jfree.data.gantt.TaskSeriesCollection; 以上就是生成甘特图并添加链接的完整过程,希望能对大家有帮助~~~
- jfreechart使用心得
- jfreechart使用心得
- JFreeChart使用心得与例子
- JFreeChart使用心得与例子
- JFreeChart使用心得与例子
- JFreeChart使用心得与例子
- JFreeChart使用心得与例子
- JFreeChart 心得
- jfreechart使用心得与例子(jsp)
- jsp JFreeChart使用心得与例子
- jsp JFreeChart使用心得与例子
- Jfreechart使用
- Jfreechart使用
- jfreechart使用
- jfreechart使用
- JFreeChart使用
- jfreechart使用
- JFreeChart使用
- 《浅谈android进程》
- Struts2标签实现for循环
- 查看、转移、夺取FSMO角色
- 两个已排序链表的合并
- EPROCESS ;ethread WINDOWS 7结构
- jfreechart使用心得
- HTML5给Web带来9大改变
- linux内核中platform框架中的一个小技巧
- 20110429
- java实现kmp算法
- ARM体系结构与编程学习(九)
- postgress中的系统视图pg_user
- ACE的构建(VC++6.0环境)
- 程序员网站资源宝库