java

位置:IT落伍者 >> java >> 浏览文章

在Struts中用JFreeChart生成图表


发布日期:2024年06月10日
 
在Struts中用JFreeChart生成图表

类中方法

引入必要的包

import javaxservlethttpHttpSession;

import javaxservlethttpHttpServletRequest;

import orgjfreechartJFreeChart;

import orgjfreechartChartFactory;

import orgjfreedatageneralDefaultPieDataset;

import orgjfreechartplotPiePlot;

import orgjfreechartChartRenderingInfo;

import orgjfreechartentityStandardEntityCollection;import orgjfreechartservletServletUtilities;int n = orgCourseListsize();

//List的长度int[] Num = new int[n];

//存放人数String[] orgName = new String[n];

//存放机构名称String title = 参加人数统计图;

for (int i = ;

i < n; i++) {RepVteachStatic temp = new RepVteachStatic();

//临时存放对象视情况可变RepVteachStatic是一个DTO(数据传输对象)temp = (RepVteachStatic) orgCourseListget(i);

//取得List中的对象int personNum = repVteachStaticServicegetOrgCoursePersonNum(temp);

//查询出参加人数Num[i] = personNum;

orgName[i] = tempgetOrg_name();

}getOrgCoursePieChart(Num orgName title);

//用三个参数调用图表生成方法

图表生成方法

public void getOrgCoursePieChart(int[] Num String[] orgName String title) {

DefaultPieDataset data = new DefaultPieDataset();

for (int i = n = Numlength; i < n; i++) {

datasetValue(orgName[i] Num[i]);

}

JFreeChart chart = ChartFactorycreatePieChartD(title data truefalse false);

chartsetBackgroundPaint(javaawtColorgreen);

ChartRenderingInfo info = new ChartRenderingInfo(

new StandardEntityCollection());

try {

HttpSession session = ActionContextgetActionContext()getRequest()getSession();

//取session对象视情况取法会有变化

HttpServletRequest request = ActionContextgetActionContext()getRequest();

//取request对象视情况取法会有变化

String filename = ServletUtilitiessaveChartAsPNG(chart info session);

graphURL = requestgetContextPath()

+ /servlet/DisplayChart?filename= + filename;

}catch (Exception e) {}}

页面

<img src=<bean:writename=repVteachStaticBean property=graphURL/> >               

上一篇:在JBuilder 9上使用Struts 1.1的实现

下一篇:spring中bean的注入方式的选择