JFreeChart 入门
要使用JFreeChart ,首先要将JFreeChart-版本号.jar,jcommon-版本号.jar放入CLASSPATH,对于web工程,则放入WEB-INF/lib下,并在CLASSPATH加入这两个jar包。
JFreeChart的使用分为三个步骤,一,准备数据集,即要在图形中显示的数据;2,通过ChartFactory产生JFreeChart对象;3,对产生的JFreeChart对象进行修饰。如产生饼图的示例代码如下:
// 数据准备
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Java", new Double(43.2));
data.setValue("Visual Basic", new Double(1.0));
data.setValue("C/C++", new Double(17.5));
data.setValue("Python", new Double(40.0));
// 生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart("", data, true, true,
false);
// 对图形进行修饰
// 增加标题和副标题,要对字体进行设置,不然会乱码 Font font = new Font("宋体", Font.BOLD, 16);
TextTitle title = new TextTitle("主标题", font);
TextTitle subtitle = new TextTitle("副标题", new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(title);
// 设置图中标签的字体
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.BOLD, 16));
// 设置图片说明/图例的字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 16));
//将图片写入输出流,可以是文件、网络输出流
OutputStream out = new FileOutputStream(new File("E://test.jpg"));
ChartUtilities.writeChartAsJPEG(out, chart, 400, 300);
在servlet中使用JFreeChart
在servlet中使用JFreeChart,只需将生成JFreeChart对象通过ChartUtilities写入到HttpServletResponse即可。完整的示例代码如下:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 数据准备
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Java", new Double(43.2));
data.setValue("Visual Basic", new Double(1.0));
data.setValue("C/C++", new Double(17.5));
data.setValue("Python", new Double(40.0));
// 生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart("", data, true, true,
false);
// 对图形进行修饰
// 增加标题和副标题,要对字体进行设置,不然会乱码
Font font = new Font("宋体", Font.BOLD, 16);
TextTitle title = new TextTitle("主标题", font);
TextTitle subtitle = new TextTitle("副标题", new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(title);
// 设置图中标签的字体
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.BOLD, 16));
// 设置图片说明/图例的字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 16));
//将图片写入输出流
ChartUtilities.writeChartAsJPEG(resp.getOutputStream(), chart, 400, 300);
}
JFreeChart与struts2集成
JFreeChart与struts2除了要将JFreeChart-版本号.jar,jcommon-版本号.jar放入lib和CLASSPATH外,还要将struts2-jfreechart-plugin-版本号.jar加入lib和CLASSPATH.
编写Action,示例代码如下:
public class JFreeChartAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 5602329386267968880L;
// 这个是写死的,名字必须为chart
private JFreeChart chart;
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
@Override
public String execute() throws Exception {
// 数据准备
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Java", new Double(43.2));
data.setValue("Visual Basic", new Double(1.0));
data.setValue("C/C++", new Double(17.5));
data.setValue("Python", new Double(40.0));
// 生成JFreeChart对象
JFreeChart chart = ChartFactory.createPieChart("", data, true, true,
false);
// 对图形进行修饰
// 增加标题和副标题,要对字体进行设置,不然会乱码
Font font = new Font("宋体", Font.BOLD, 16);
TextTitle title = new TextTitle("主标题", font);
TextTitle subtitle = new TextTitle("副标题", new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(title);
// 设置图中标签的字体
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.BOLD, 16));
// 设置图片说明/图例的字体
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 16));
return SUCCESS;
}
在struts.xml中增加如下配置
<package name="jFreeChartDemonstration" extends="struts-default"
namespace="/jfreechart">
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>
</result-types>
<action name="JFreeChartAction" class="org.test.jfreechart.action.JFreeChartAction">
<result type="chart">
// 图片的宽和高
<param name="width">400</param>
<param name="height">300</param>
// 这里支持png,和jpg两种
<param name="type">jpg</param>
</result>
</action>
</package>
也可以将action和result-types放到其他package中,这个根据具体项目情况来定。
然后就可以通过地址http://127.0.0.1:8080/{webpath}/jfreechart/JFreeChartAction.action访问到生成的图片了。