= ServletActionContext.getResponse();
OutputStream os = response.getOutputStream();
ChartUtilities.writeChartAsPNG(os, chart, width, height);
os.flush();
}
}
JFreeChart在Webwork中的应用(4)
时间:2010-12-17
创建JFreeChart的action类。
package pawpaw.test;
import java.awt.Insets;
import java.awt.Font;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.servlet.*;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.util.Rotation;
import com.opensymphony.xwork.ActionSupport;
/*
*<p>Description: 输出一条斜线chart</p>
* author: pawpaw
* @version 1.0 12/15/2003
*/
public class ViewModerationChartAction extends ActionSupport
{
private JFreeChart chart;
public String execute() throws Exception
{
// 创建chart文件数据集
XYSeries dataSeries = new XYSeries(null);
for (int i = 0; i <= 100; i++)
{
dataSeries.add(i, RandomUtils.nextInt());
}
XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
ValueAxis xAxis = new NumberAxis("Raw Marks");
//x轴坐标
ValueAxis yAxis = new NumberAxis("Moderated Marks");
//y轴坐标
// 设置chart的样式
chart =new JFreeChart(
"Moderation Function",
// 图表标题
JFreeChart.DEFAULT_TITLE_FONT,
new XYPlot( xyDataset, xAxis, yAxis,
new StandardXYItemRenderer(StandardXYItemRenderer.LINES)),
// 数据集 false //是否生成URL链接 );
chart.setBackgroundPaint(java.awt.Color.white);
//设置图片的背景色
Font font = new Font("黑体",Font.CENTER_BASELINE,20);
//设置图片标题的字体和大小
TextTitle _title = new TextTitle(title);
_title.setFont(font);
chart.setTitle(_title);
return super.SUCCESS;
}
public JFreeChart getChart()
{
return chart;
}
}
小结:
这只是一个简单的例子,如果你想深入了解的话,可以参考Webwork文档和jfreechart网站。 |