HomeJava Using oracle graph/bi graph without datacontrol bindings
Using oracle graph/bi graph without datacontrol bindings
Written by Aminur Rashid
Friday, 28 August 2009
Oracle had this excellent graph utility in the earlier releases of Jdeveloper. It was Bi-graph, which user can use to represent the data in graphical forms. This has been replaced in JDeveloper 11g by DVT component. However, the new Faces DVT component forces user to use Faces components, which may not be a requirement always. Neverthless, both the components (old BI graph and latest DVT components) are closely tied to the Datacontrols. What if user has Hibernate at the server side and want to use these components. The writeup, descibes one of the different ways, to show the data using BI Graph.
Idea is to create an oracle.dss.thin.beans.graph.ThinGraph object in the background, set the data from any datasource (dummy data in this example), export the graph as image, and show in the JSP
Idea GraphUtil.java creates a Graph object. The visual properties required by Graph will be provided by an xml file. Jdeveloper provides a wizard to edit the xml. This however can be edited without Jdeveloper, if user is aware of the elements required:
package com.aminur.graph.legacy.model.util;
import java.awt.Dimension;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import oracle.dss.dataView.Dataview;
import oracle.dss.thin.beans.graph.ThinGraph;
/*
* @author Aminur Rashid (aminur ^ aminurrashid.com)
* Utility method that generates graph.
* Currently uses the dummy data but can be customized as per
* project requirements to fetch data from any of the VO/DAO class.
* It creates a oracle.dss.graph.Graph object and exports it to an Image.
*/
public class GraphUtil {
private ThinGraph graph1 = new ThinGraph();
private static List dummyData = null;
public GraphUtil() {
try {
graph1.readXML(this.getClass().getClassLoader().getResourceAsStream("BIGraphDef.xml"),
Dataview.RESET_XML_PROPERTIES);
graph1.setImageSize(new Dimension(600,500));
} catch (Exception e) {
e.printStackTrace();
}
}
public void getGraphImageInStream (OutputStream o, String gType){
if (gType==null || !gType.equals("Line"))
graph1.setGraphType(ThinGraph.BAR_VERT_CLUST );
else{
graph1.setGraphType(ThinGraph.LINE_VERT_ABS);
}
// = OrderCountDAO.getOrderStats();
// fetch from real Dao,VO, ??
List ls = fetchDummyData();
reSetGraph(ls);
graph1.exportToGIF(o);
}
private void reSetGraph(List ls)
{
graph1.setTabularData(ls);
}
public static List fetchDummyData(){
if(dummyData==null){
dummyData = new ArrayList();
java.util.Date dt = Calendar.getInstance().getTime();
String masterSystemId = "SLS (12)";
createDummyData(dummyData, 12, masterSystemId);
masterSystemId = "DWP (5)";
createDummyData(dummyData, 5, masterSystemId);
masterSystemId = "OAT (6)";
createDummyData(dummyData, 6, masterSystemId);
masterSystemId = "Portal (7)";
createDummyData(dummyData, 7, masterSystemId);
}
return dummyData;
}
private static void createDummyData(List dataVector, int unique, String masterSystemid)
{
for(int k = 0; k < 4; k++)
{
Object rows[] = new Object[3];
rows[0] = Integer.valueOf(k);
rows[1] = masterSystemid;
if(k == 3)
rows[2] = Integer.valueOf(k * unique);
else
rows[2] = Integer.valueOf(k * unique * 2);
dataVector.add(((Object) (rows)));
}
}
}
BIGraphDef.xml
Image Servlet: Helper Servlet to write GraphImage to a HttpServletResponse servlet
PS: With the new DVT components, user may need to use oracle.adf.view.faces.bi.component.graph.UIGraph instead of ThinGraph and may have to set all the properties directly to the UIGraph instance instead of using the xml.