统计图表ChartDirector在Servlet和Struts2中的应用

阅读更多
1、公共配置
a.把ChartDirector.jar放到\webapp\WEB-INF\lib下面,并加入以下包:
	
    	javax.servlet
    	servlet-api
    	2.5
    	jar
    	provided
    
	

b.在web.xml加入以下代码:
	
		GetSessionImage
		ChartDirector.GetSessionImage
	
	
		GetSessionImage
		*.chart
	
	

c.把chartdir.lic文件在\src\main\java下面,其内容如下:
	SXZVFNRN9MZ9L8LGA0E2B1BB
	


2、编写Servlet代码
a.web.xml加入以下代码:
	
		BarChartServlet
		sample.servlet.BarChartServlet
	
	
		BarChartServlet
		/barchart
	
	

b.Servlet代码如下:
	public class BarChartServlet extends HttpServlet {
	
		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			// The data for the bar chart
			double[] data = { 85, 156, 179.5, 211, 123 };

			// The labels for the bar chart
			String[] labels = { "Mon", "Tue", "Wed", "Thu", "Fri" };

			// Create a XYChart object of size 250 x 250 pixels
			XYChart c = new XYChart(250, 250);

			// Set the plotarea at (30, 30) and of size 200 x 190 pixels
			c.setPlotArea(30, 30, 200, 190);

			// Add a bar chart layer using the given data
			c.addBarLayer(data);

			// Set the x axis labels using the given labels
			c.xAxis().setLabels(labels);

			request.getSession().setAttribute("chart1", c.makeChart2(Chart.PNG));
			String chart1URL = "img=chart1&id=" + System.currentTimeMillis();

			// include tool tip for the chart
			String imageMap1 = c.getHTMLImageMap("#", "",
					"title='{xLabel}: US${value}K'");

			response.setContentType("text/html");
			ServletOutputStream out = response.getOutputStream();
			out.println("");
			out.println("");
			out.print("");
			out.println("");
			out.println(imageMap1);
			out.println("");
			out.println("");
			out.println("");
		}
	}
	


3、编写Action代码
a.Action代码如下:
	public class ChartAction extends BaseAction {

		Logger logger = Logger.getLogger(ChartAction.class);

		@Action(value = "/barchart", results = { 
			@Result(name = "success", location = "/index.jsp") 
		})
		public String barchart() {
			// The data for the bar chart
			double[] data = { 85, 156, 179.5, 211, 123 };

			// The labels for the bar chart
			String[] labels = { "Mon", "Tue", "Wed", "Thu", "Fri" };

			// Create a XYChart object of size 250 x 250 pixels
			XYChart c = new XYChart(250, 250);

			// Set the plotarea at (30, 30) and of size 200 x 190 pixels
			c.setPlotArea(30, 30, 200, 190);

			// Add a bar chart layer using the given data
			c.addBarLayer(data);

			// Set the x axis labels using the given labels
			c.xAxis().setLabels(labels);

			getRequest().getSession().setAttribute("chart1",
					c.makeChart2(Chart.PNG));
			String chart1URL = "img=chart1&id=" + System.currentTimeMillis();

			// include tool tip for the chart
			String imageMap1 = c.getHTMLImageMap("#", "",
					"title='{xLabel}: US${value}K'");
			getRequest().setAttribute("chart1URL", chart1URL);
			getRequest().setAttribute("imageMap1", imageMap1);
			
			return SUCCESS;
		}
	}
	

b.index.jsp页面代码如下:
	
${imageMap1}


4、配置说明
关于图片的显示方法可以根据自己的需求。我在Servlet和Action中采用了两种方法。另外提醒一下,如果改用jsp时,记得把getchat.jsp放到webapp下面。

5、显示结果如下:
统计图表ChartDirector在Servlet和Struts2中的应用_第1张图片
  • 统计图表ChartDirector在Servlet和Struts2中的应用_第2张图片
  • 大小: 1.4 KB
  • 查看图片附件

你可能感兴趣的:(Servlet,C,C++,C#,JSP)