Integrating JfreeChart Java Chart in OAF (Oracle Application Framework)
As recently. I got a requirement to create a rich UI in Oracle Application Framework (OAF) which includes Chart, GIF Images etc.
Instead on create normal OAF Chart. I tried embedding an external chart into Oracle Self Service pages.
On my successful POC. I embedded the Jfreechart (JAVA Chart) into OAF pages as shown below.
This blog will help you understand to add the Jfreechart chart in OAF Pages using servlet.
Before starting with the Integration. One should know about jfreechart. One and only java chart which is available for free of cost.
Follow the link http://www.jfree.org/jfreechart/ for understanding Jfreechart and you can also find some example of creating jfreechart http://www.jfree.org/jfreechart/samples.html
Now we will start with the Integration (Adding Chart to OAF Pages)
Steps
- Download the jfreechart jar from the following link https://sourceforge.net/projects/jfreechart/files/
- For this. I have used jfreechart version 12 (jfreechart-1.0.12.jar)
- Import the following jar into Application and Jdev (If you want to run from Local). Make sure the library are import properly into Oracle Application. Dont forgot the Bounce the Server.
<library path="/u01/appldev/DEV4/apps/apps_st/comn/java/lib/gnujaxp.jar" />
<library path="/u01/appldev/DEV4/apps/apps_st/comn/java/lib/iText-2.1.3.jar" />
<library path="/u01/appldev/DEV4/apps/apps_st/comn/java/lib/jcommon-1.0.15.jar" />
<library path="/u01/appldev/DEV4/apps/apps_st/comn/java/lib/jfreechart-1.0.12.jar" />
<library path="/u01/appldev/DEV4/apps/apps_st/comn/java/lib/jfreechart-1.0.12-experimental.jar" />
<library path="/u01/appldev/DEV4/apps/apps_st/comn/java/lib/jfreechart-1.0.12-swt.jar" />
<library path="/u01/appldev/DEV4/apps/apps_st/comn/java/lib/swtgraphics2d.jar" />
- Create a Jfreechart wrapper which will generate the Chart Images.
public class XXCUSTXXRingChartCO
{
public XXCUSTXXRingChartCO()
{
}
public static JFreeChart SummaryRingChart(DefaultPieDataset dataset,
int width, int height,
Color[] colors,
String title) throws IOException
{
JFreeChart chart = null;
if (dataset != null)
{
OutputStream out = null;
// Calling the Custom Plot
Double value1 = 0.0;
Double value2 = 0.0;
if (dataset.getValue(0) != null)
{
value1 = dataset.getValue(0).doubleValue();
}
if (dataset.getValue(1) != null)
{
value2 = dataset.getValue(1).doubleValue();
}
Double angle = ((3.6 * (((value1 * 100) / (value1 + value2)) / 2)));
chart = ChartFactory.createRingChart(title, dataset, true, true, false);
if (chart != null)
{
// Customizing the Chart
chart.setAntiAlias(true);
chart.setBorderVisible(false);
chart.setBackgroundPaint(null);
ChartChangeEvent event = new ChartChangeEvent(chart);
chart.setBackgroundImageAlpha(0.0F);
event = new ChartChangeEvent(chart);
RingPlot plot = (RingPlot)chart.getPlot();
if (plot != null)
{
@SuppressWarnings("unchecked")
List<String> keys = dataset.getKeys();
Iterator<String> iterator = keys.iterator();
int index = 0;
while (iterator.hasNext())
{
plot.setSectionPaint(iterator.next(), colors[index]);
index++;
}
if (width <= 0)
{
width = 450;
}
if (height <= 0)
{
height = 450;
}
plot.setLabelFont(new Font("Verdana", Font.BOLD, 11));
plot.setSectionDepth(0.36999999999999998D);
plot.setInteriorGap(0);
plot.setStartAngle(angle);
plot.setLabelBackgroundPaint(null);
plot.setLabelOutlinePaint(null);
plot.setSeparatorsVisible(true);
plot.setLabelShadowPaint(null);
plot.setSeparatorPaint(new Color(102, 102, 102));
plot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD);
plot.setLabelPaint(new Color(102, 102, 102));
float lineWidth = 1.4f;
float dash[] =
{ 5.0f };
float dot[] =
{ lineWidth };
plot.setLabelLinkStroke(new BasicStroke(lineWidth,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 2.0f,
dot, 0.0f));
plot.setLabelGap(0.00001);
plot.setOutlinePaint(new Color(0, 0, 0, 0));
plot.setOutlineVisible(false);
plot.setShadowPaint(null);
// Plot Definition
plot.setSectionOutlinesVisible(true);
plot.setBackgroundPaint(new Color(255, 255, 255, 0));
plot.setBackgroundImageAlpha(0.0f);
plot.setBackgroundAlpha(0.0F);
plot.setCircular(true);
plot.setNoDataMessage("No Graph available");
plot.setIgnoreZeroValues(true);
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(" ({2})"));
plot.setLabelBackgroundPaint(null);
}
}
}
return chart;
}
public static JFreeChart createBarChart(CategoryDataset dataset)
{
// create the chart...
JFreeChart chart = // chart title
// domain axis label
// range axis label
// data
// orientation
// include legend
// tooltips?
// URLs?
ChartFactory.createBarChart("", "Category", "Units", dataset,
PlotOrientation.VERTICAL, true, true, false);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setAntiAlias(true);
chart.setBorderVisible(false);
chart.setBackgroundPaint(null);
ChartChangeEvent event = new ChartChangeEvent(chart);
chart.setBackgroundImageAlpha(0.0F);
// get a reference to the plot for further customisation...
CategoryPlot plot = (CategoryPlot)chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
plot.setBackgroundPaint(new Color(255, 255, 255, 0));
plot.setBackgroundImageAlpha(0.0f);
plot.setBackgroundAlpha(0.0F);
// ******************************************************************
// More than 150 demo applications are included with the JFreeChart
// Developer Guide...for more information, see:
//
// > http://www.object-refinery.com/jfreechart/guide.html
//
// ******************************************************************
// set the range axis to display integers only...
final NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
BarRenderer renderer = (BarRenderer)plot.getRenderer();
renderer.setDrawBarOutline(false);
// set up gradient paints for series...
GradientPaint gp0 =
new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(87, 50,
94));
GradientPaint gp1 =
new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(202, 66,
63));
GradientPaint gp2 =
new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(65, 105,
225));
GradientPaint gp3 =
new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(30, 144,
255));
GradientPaint gp4 =
new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(0, 191,
255));
renderer.setSeriesPaint(0, gp0);
renderer.setSeriesPaint(1, gp1);
renderer.setSeriesPaint(2, gp2);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI /
6.0));
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
}
- Create a Servlet call as follow
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import xx.oracle.apps.per.xx.chart.ui.webui.XXCUSTXXRingChartCO;
public class XXCUSTXXTenantSummaryChartServlet extends HttpServlet
{
/*private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
protected void processChart(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
String pParam1 = request.getParameter("Param1");
String pParam2 = request.getParameter("Param2");
String pParam3 = request.getParameter("Param3");
String pParam4 = request.getParameter("Param4");
String pParam5 = request.getParameter("Param5");
String pProjectName = request.getParameter("ProjectName");
if ((pParam1 == null) || ("".equals(pParam1.trim())))
{
pParam1 = "0";
}
if ((pParam2 == null) || ("".equals(pParam2.trim())))
{
pParam2 = "0";
}
if ((pParam3 == null) || ("".equals(pParam3.trim())))
{
pParam3 = "0";
}
if ((pParam4 == null) || ("".equals(pParam4.trim())))
{
pParam4 = "0";
}
if ((pParam5 == null) || ("".equals(pParam5.trim())))
{
pParam5 = "0";
}
response.setContentType("image/png");
if ((pProjectName != null) || (!"".equals(pProjectName.trim())))
{
pProjectName = pProjectName + " Project Summary";
}
OutputStream os = response.getOutputStream();
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("Initation Phase", new Double(pParam1));
defaultpiedataset.setValue("Design Phase", new Double(pParam2));
defaultpiedataset.setValue("Mobilization Phase", new Double(pParam3));
defaultpiedataset.setValue("Delivery Phase", new Double(pParam4));
defaultpiedataset.setValue("Close-out Phase", new Double(pParam5));
//defaultpiedataset.setValue("Param3", new Double(pParam3));
Color[] colors =
{ new Color(87, 50, 94), new Color(202, 66, 63), new Color(65, 105, 225),
new Color(30, 144, 255),
new Color(0, 191, 255) }; //new Color(18, 101, 160)
JFreeChart jfreechart =
XXCUSTXXRingChartCO.SummaryRingChart(defaultpiedataset, 350, 350,
colors, pProjectName);
if (jfreechart != null)
{
ChartUtilities.writeChartAsPNG(os, jfreechart, 350, 350, null, true, 0);
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
System.out.println("processChart doGet");
processChart(request, response);
}
}
- Register the Servlet call in web.xml as follow.
<servlet-mapping>
<servlet-name>xxcustxxdbsummarychartservlet</servlet-name>
<url-pattern>/xxcustxxdbsummarychartservlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>xxcustxxtenantsummarychartservlet</servlet-name>
<servlet-class>xx.oracle.apps.per.rdd.chart.ui.servlet.XXCUSTXXTenantSummaryChartServlet</servlet-class>
</servlet>
- Call the servlet in the processRequest and set the image source to the image as follow.
OAImageBean XXChartImageBean =
(OAImageBean)webBean.findChildRecursive("XXChartImage");
if(XXChartImageBean!=null)
{
RDDDBBarChartImageBean.setSource("xxcustxxdbsummarychartservlet?Param1=" +
param1 + "&Param2=" + param2 + "" +
"&Param3=" + param3 + "" + "&Param4=" +
param4 + "" + "&Param5=" + param5 + "" +
"&ProjectName=" + pProjectName);
}
- Compile and run. Enjoy :)
Excellent post.. very helpful..!! All the best.!!
ReplyDelete