Software used JDeveloper 10.1.3.2
JfreeChart 1.0.13
JDeveloper 10g doesn’t have any graph component. We can integrate and use BI Bean components with JDeveloper. But, BI Bean has very limited graph components. [We have faced to
implement line curve with BI Bean].
implement line curve with BI Bean].
As a solution we have used JFreeChart with ADF to create an ADF/JSF page. This article is showing very basic steps to implement that kind of page. Apart from that I’ll show here how to attach and show the graph component in one single JSF page.
First,
Attach Jcommon-1.0.16.jar and Jfreechart-1.0.13 files in your project’s library.
For attaching the created chart in the same JSF page you need to create two servlets.
Create the first one as follows ->
package view;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.*;
import javax.servlet.http.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class chartServlet1 extends HttpServlet {
private static final String CONTENT_TYPE =
"text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
OutputStream out = response.getOutputStream();
try {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(10.0, "S1", "C1");
dataset.addValue(4.0, "S1", "C2");
dataset.addValue(15.0, "S1", "C3");
dataset.addValue(14.0, "S1", "C4");
dataset.addValue(-5.0, "S2", "C1");
dataset.addValue(-7.0, "S2", "C2");
dataset.addValue(14.0, "S2", "C3");
dataset.addValue(-3.0, "S2", "C4");
dataset.addValue(6.0, "S3", "C1");
dataset.addValue(17.0, "S3", "C2");
dataset.addValue(-12.0, "S3", "C3");
dataset.addValue(7.0, "S3", "C4");
dataset.addValue(7.0, "S4", "C1");
dataset.addValue(15.0, "S4", "C2");
dataset.addValue(11.0, "S4", "C3");
dataset.addValue(0.0, "S4", "C4");
dataset.addValue(-8.0, "S5", "C1");
dataset.addValue(-6.0, "S5", "C2");
dataset.addValue(10.0, "S5", "C3");
dataset.addValue(-9.0, "S5", "C4");
dataset.addValue(9.0, "S6", "C1");
dataset.addValue(8.0, "S6", "C2");
dataset.addValue(null, "S6", "C3");
dataset.addValue(6.0, "S6", "C4");
dataset.addValue(-10.0, "S7", "C1");
dataset.addValue(9.0, "S7", "C2");
dataset.addValue(7.0, "S7", "C3");
dataset.addValue(7.0, "S7", "C4");
dataset.addValue(11.0, "S8", "C1");
dataset.addValue(13.0, "S8", "C2");
dataset.addValue(9.0, "S8", "C3");
dataset.addValue(9.0, "S8", "C4");
dataset.addValue(-3.0, "S9", "C1");
dataset.addValue(7.0, "S9", "C2");
dataset.addValue(11.0, "S9", "C3");
dataset.addValue(-10.0, "S9", "C4");
JFreeChart chart =
ChartFactory.createBarChart("Bar Chart", "Category", "Value",
dataset, PlotOrientation.VERTICAL,
true, true, false);
response.setContentType("image/png");
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
} catch (Exception e) {
System.err.println(e.toString());
} finally {
out.close();
}
}
}
Now create the second one as follows ->
package view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletDemo2 extends HttpServlet {
private static final String CONTENT_TYPE =
"text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = new PrintWriter(response.getWriter());
try {
//String param = request.getParameter("chart");
response.setContentType("text/html");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("
JFreeChart Servlet Demo
");
out.println("
");
out.println("Please choose a chart type:");
out.println("
");
out.println("
");
out.println("
"\" BORDER=1 WIDTH=400 HEIGHT=300/>");
out.println("");
out.println("");
out.flush();
out.close();
} catch (Exception e) {
System.err.println(e.toString());
} finally {
out.close();
}
}
}
Always use the doGet() method in the servlet to write JFreechart code.
Now create the JSF/ADF page and write the following code ->
Now, run the page and it will be displayed like this ->
2 comments :
something wrong in servlet2? need form?
nice written
Post a Comment