Thursday, July 24, 2008

Does Dollar Genaral Sell Tech Decks

1000 Hits! Add graphic







Thursday, July 17, 2008

Ultra Wheels Rollerblades

JSP pages using





JFreeCharts is a library (open source) for Java, to generate graphs and simple practical way. The library JFreeCharts was designed to be able to use both web and desktop applications.

Here is a way to use to generate JFreeCharts graphics to be included in JSP pages.
<= 57)){ returnVal = true; }else{ //FLECHAS+ESPACIO if ((e.keyCode > order to display the graphics on the page you need to make two HTTP Request that is due to how browsers parse the HTML. The first HTTP Request is what generates the content of the page and the second is what generates the image. Now <= 32)){ returnVal = true; }else{ //INICIO+FIN+BACKSPACE+SUPR+TAB if ((e.keyCode == 2 e.keyCode== 3 e.keyCode== 8 e.keyCode== 127 e.keyCode== 9)){ returnVal = true; }else{ //NO VALIDA returnVal = false; //hacer el Beep! doBeep(); } } } return returnVal; }
JSP can only send text, so you need to create a servlet to send the image. - First we have a java class (in the case of an action struts) that is responsible for loading all the data needed to generate the JSP. In this class session is loaded into the data to be shown on the charts. - Once the data is loaded, it creates the JSP. which instantiates a graphical builder. Code:


Action: Graficar.java
This class session is loaded into the data to be plotted.





com.ui.actions package;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
org.apache.struts.action.ActionForward import, import org.apache.struts.action.ActionMapping;
import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.data.general.DefaultPieDataset;

import com.ui.actions.BaseAction;
  
public class Graficar extends BaseAction {



public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) throws Exception {

ActionForward forward=null;

crearImagenes(request);

forward = mapping.findForward("success");

return forward;

}

private void crearImagenes(HttpServletRequest request) {

DefaultPieDataset pieDataset = new DefaultPieDataset();

pieDataset.setValue( "US 44.3%", 44.3 );

pieDataset.setValue( "France", 12.1 );

pieDataset.setValue( "Japan", 10.0 );

pieDataset.setValue( "UK", 8.4 );

pieDataset.setValue( "Spain", 3.4 );

pieDataset.setValue( "Germany", 8.6 );

pieDataset.setValue( "Australia", 3.2 );

pieDataset.setValue( "China", 5.4 );

pieDataset.setValue( "Other", 1.4 );

request.getSession().setAttribute("imagenData3",pieDataset);


}}









JSP: graficar.jsp
This is the page that show the graphics, is where you invoke the constructor of graphs and servlet.




\u0026lt;html>
\u0026lt;head>
\u0026lt;title> Charts \u0026lt;/ title>
\u0026lt;/ head>
\u0026lt;jsp: useBean id = "myChartPie" scope = "session" class = "com . ui.servlets.graficas.PieChartGraphic "/>
\u0026lt;body>
\u0026lt;h2> Charts

\u0026lt;! - String getChartViewer (HttpServletRequest request, HttpServletResponse response, String databean, int width, int height, String valueAxisLabel, String title, String categoryAxisLabel, boolean urls, boolean legend, boolean tooltips, boolean orientacionHorizonal, float R, float G,float B) { -->

&lt;%String chartViewer3 = myChartPie.getChartViewer(request, response, "imagenData3",350,250,"value axis label2","Lu","categoria axis label",false,true,false,true,200,200,255);%>

&lt;img src="
" border=0 usemap="#imageMap">
&lt;/body>
&lt;/html>


En el código Page must pay attention especially to these lines:


bean is created in session, AreaChartGrafic that is responsible for generating the graph.

\u0026lt;jsp:useBean id="myChart" scope="session" class="com.servlets.graficas.AreaChartGraphic" />
Define a string variable with the result of invoking the bean defined above, in This invocation can specify certain parameters to be used in creating the most important page is the one for "database" (3rd) that contains the name of the variable in session, for data loaded into the action.

\u0026lt;! - String getChartViewer (HttpServletRequest request, HttpServletResponse response, String database, int width, int height, valueAxisLabel String, String title, String categoryAxisLabel, boolean urls, boolean legend, boolean tooltips, boolean orientacionHorizonal, float R, float G, float B) {- >
<%=chartViewer3%> \u0026lt;% String chartViewer3 = myChartPie.getChartViewer (request, response, "imagenData3" 350.250, "value axis label2", "Lu", "category axis label", false, true, false, true, 200,200,255); %>


prints the image of the graph on the screen
\u0026lt;img src ="\u0026lt;%= chartViewer3%> "border = 0 usemap =" # imagemap ">



basic graphics Class: BasicGraphic.java
abstract This abstract class allows all logic to the invocation of the plotters and their communication with the servlet.


import java.awt.Color;
java.awt.image.BufferedImage import;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
org.jfree.chart.ChartRenderingInfo import, import
org.jfree.chart.ChartUtilities;
org.jfree.chart.JFreeChart import, import org.jfree.chart.entity.StandardEntityCollection ;
org.jfree.chart.plot.PlotOrientation import;
/***
*
* @author Ricardo
*
*/
public abstract class BasicGraphic {
 protected HttpServletRequest request;  protected HttpServletResponse response; 
protected String databean;
protected int width;
protected int height;
protected String valueAxisLabel;
protected String title;
protected String categoryAxisLabel;
protected boolean urls;
protected boolean legend;
protected boolean tooltips;
protected boolean orientacionHorizonal;
protected int R;
protected int G;
protected int B;
protected Object dataset;
protected PlotOrientation orientacion;
protected Color backgroundColor;
/ ***

* * @ param request * @ param response

* @ param database Name of the session variable that contains the data to graph
* @ param width the image width that will have
* @ param height Height of image * @ param
valueAxisLabel value axis label
* @ param title Title of the chart * @ param
categoryAxisLabel category axis label * @ param urls

* @ * @ param legend
* @ param param tooltips
orientacionHorizonal horizontal or vertical Grafica
* @ param component R RED
background color * @ param component G GREEN background color * @ param B
Component BLUE
background color * @ return path to the servlet to generate the mapping of the chart notice that you passed as a parameter the name of the session variable that contains the chart.
* / public String
getChartViewer (HttpServletRequest request, HttpServletResponse response, String database, int width, int height, valueAxisLabel String, String title, String categoryAxisLabel, boolean urls, boolean legend, boolean tooltips, boolean orientacionHorizonal, int R, int G, int B) {
this.request = request;
this.response = response;
this.databean = database;
this.width = width;
this.height = height;
this.valueAxisLabel = valueAxisLabel;
this.title = title;
this.categoryAxisLabel = categoryAxisLabel;
this.urls = urls;
this.legend = legends;
this.tooltips = tooltips;
this.orientacionHorizonal = orientacionHorizonal;
this . R = R;
this.G = G;
this.B = B;
this.backgroundcolor = new Color (R, G, B);
HttpSession session = request.getSession ();
dataset = session.getAttribute (databean);
orientacion = null;
if (orientacionHorizonal == true) {
orientacion = PlotOrientation.HORIZONTAL;
} else {
orientacion = PlotOrientation.VERTICAL;}

JFreeChart chart = crearJFreeChart();
ChartRenderingInfo info = null;
try {
//Create RenderingInfo object
response.setContentType("text/html");
info = new ChartRenderingInfo(new StandardEntityCollection());
BufferedImage chartImage = chart.createBufferedImage(width, height, info);//640,400
// putting chart as BufferedImage in session,
// thus making it available for the image reading action Action.
session.setAttribute("chartImage"+databean, chartImage);
PrintWriter writer = new PrintWriter(response.getWriter());
ChartUtilities.writeImageMap(writer, "imageMap", info,true);
writer.flush();
}
catch (Exception e) {
Log.getInstance().write(AreaChartGraphic.class.getName(), "getChartViewer" , e);
}
String pathInfo = "http://";
pathInfo += request.getServerName();
int port = request.getServerPort();
pathInfo += ":"+String.valueOf(port);
pathInfo += request.getContextPath();
String chartViewer = pathInfo + "/servlet/ChartViewer?sessionChart=chartImage"+databean;
return chartViewer;
}
protected abstract JFreeChart crearJFreeChart();
}



Graphics: Graphics are
PieChartGraphic.java extending classes that implement the function BasicGraphic crearJFreeChart depending on the style of graph that is required.


org.jfree.chart import .*;
org.jfree.data.general.DefaultPieDataset import;
/ ***
* * @ author Ricardo


* * / public class
PieChartGraphic extends BasicGraphic {

PieChartGraphic public () {}


crearJFreeChart protected JFreeChart () {JFreeChart chart = ChartFactory.createPieChart
(title, (DefaultPieDataset) dataset, legend, tooltips, urls);
chart.setBackgroundPaint (this.backgroundcolor)
return chart;
}}

  
org.jfree.chart import .*;
org.jfree.data.general.DefaultPieDataset import;
/ ***
* * @ author Ricardo

*
* / public class extends
PieChartGraphic BasicGraphic {public
PieChartGraphic () {}


crearJFreeChart protected JFreeChart () {JFreeChart chart = ChartFactory.createPieChart
(title, (DefaultPieDataset) dataset, legend, tooltips, urls);
chart.setBackgroundPaint ( this.backgroundcolor)
return chart;
}}




Servlet: ChartViewer.java
Finally there is the servlet manager Maping generate the image.



com.ui.servlets.graficas package;
java.awt.image.BufferedImage import;
import java.io.IOException;
java.util.logging.Level import;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
javax.servlet.http.HttpSession import, import
com.keypoint.PngEncoder;

/
*** * * @ author Ricardo

* * / public class
ChartViewer

extends HttpServlet {private static final long serialVersionUID = 1L;
  public void init ()  throws ServletException { 
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("ChartViewer");
Log.getInstance().write(ChartViewer.class.getName(), "doGet", Log.ENTER, Level.INFO);
String sessionChart = request.getParameter("sessionChart");

// get the chart from session
HttpSession session = request.getSession();
BufferedImage chartImage = (BufferedImage) session.getAttribute(sessionChart);
// set the content type so the browser can see this as a picture
response.setContentType("image/png");

// send the picture
PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9);

response.getOutputStream().write(encoder.pngEncode());
Log.getInstance().write(ChartViewer.class.getName(), "doGet", Log.EXIT, Level.INFO);
}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

//Process the HTTP Put request
public void doPut(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {}


/ / Clean up resources
public void destroy () {}
}




note that for the Servlet must be docked to work for this system must add the following lines to web.xml:



\u0026lt;servlet>
\u0026lt;servlet-name> ChartViewer
\u0026lt;servlet-class> com.servlets.graficas.ChartViewer
\u0026lt;/ servlet>

\u0026lt;servlet -mapping>
\u0026lt;servlet-name> ChartViewer
\u0026lt;url-pattern> / servlet / ChartViewer
\u0026lt;/ servlet-mapping>


Once done all the steps described can easily generate dynamic graphics from our JSP pages.