View Javadoc

1   /*
2    * $Id: ChartResult.java 454455 2006-10-09 18:49:38Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import org.apache.struts2.ServletActionContext;
21  import com.opensymphony.xwork2.ActionInvocation;
22  import com.opensymphony.xwork2.Result;
23  import org.jfree.chart.ChartUtilities;
24  import org.jfree.chart.JFreeChart;
25  
26  import javax.servlet.http.HttpServletResponse;
27  import java.io.OutputStream;
28  
29  
30  /***
31   * A custom Result type for chart data. Built on top of
32   * <a href="http://www.jfree.org/jfreechart/" target="_blank">JFreeChart</a>. When executed
33   * this Result will write the given chart as a PNG to the servlet output stream.
34   *
35   */
36  public class ChartResult implements Result {
37  
38  	private static final long serialVersionUID = -6484761870055986612L;
39  	
40  	JFreeChart chart;
41      boolean chartSet = false;
42      private int height;
43      private int width;
44  
45      public ChartResult() {
46      	super();
47      }
48      
49      public ChartResult(JFreeChart chart, int height, int width) {
50      	setChart(chart);
51      	this.height = height;
52      	this.width = width;
53      }
54  
55      /***
56       * Sets the JFreeChart to use.
57       *
58       * @param chart a JFreeChart object.
59       */
60      public ChartResult setChart(JFreeChart chart) {
61          this.chart = chart;
62          chartSet = true;
63          return this;
64      }
65  
66      /***
67       * Sets the chart height.
68       *
69       * @param height the height of the chart in pixels.
70       */
71      public ChartResult setHeight(int height) {
72          this.height = height;
73          return this;
74      }
75  
76      /***
77       * Sets the chart width.
78       *
79       * @param width the width of the chart in pixels.
80       */
81      public ChartResult setWidth(int width) {
82          this.width = width;
83          return this;
84      }
85  
86      /***
87       * Executes the result. Writes the given chart as a PNG to the servlet output stream.
88       *
89       * @param invocation an encapsulation of the action execution state.
90       * @throws Exception if an error occurs when creating or writing the chart to the servlet output stream.
91       */
92      public void execute(ActionInvocation invocation) throws Exception {
93          JFreeChart chart = null;
94  
95          if (chartSet) {
96              chart = this.chart;
97          } else {
98              chart = (JFreeChart) invocation.getStack().findValue("chart");
99          }
100 
101         if (chart == null) {
102             throw new NullPointerException("No chart found");
103         }
104 
105         HttpServletResponse response = ServletActionContext.getResponse();
106         OutputStream os = response.getOutputStream();
107         ChartUtilities.writeChartAsPNG(os, chart, width, height);
108         os.flush();
109     }
110 }