February 6, 2013

Creating GraphDataModel object for pieGraph



When creating a dvt:pieGraph, an object of GraphDataModel class needs to be passed as input to it.

The below code helps to understand how to create GraphDataModel object.

In the below code, I am taking data from database.

    public static GraphDataModel getGraphDataModel(String table,                                                                                                     String cust_id) throws Exception {     
  String[] columnLabels = { "" };
        String[] columnNames = getColumnNames(table);//User defind Method, returns all column names of the given table
        int size = columnNames.length;
        if (ignoreFields != null)
            size = size - ignoreFields.size();
        Object[][] data2 = new Object[1][size];
        String[] seriesLabels = new String[size];

        Connection con = createConnection(); //Method to return connection object
        Statement stmt = con.createStatement();
                StringBuilder query = new StringBuilder("SELECT * FROM " + table+ "where cust_id="+cust_id);
              
        System.out.println("Query of graph: " + query);
        ResultSet rs = stmt.executeQuery(query);
        if (rs.next()) {
            for (int i = 0, j = 0; i < columnNames.length && j < size; i++) {
                if ("cust_id".equals(columnNames[i]))
                    continue;
                seriesLabels[j] = columnNames[i];
                data2[0][j] = new Double(rs.getDouble(i + 1));
                j++;
            }
        }
        rs.close();
        stmt.close();
        con.close();

        oracle.dss.dataView.LocalXMLDataSource ds =
            new oracle.dss.dataView.LocalXMLDataSource(columnLabels,
                                                       seriesLabels, data2);
        GraphDataModel graphData =
            new oracle.adf.view.faces.bi.model.GraphDataModel();
        graphData.setDataSource(ds);
        return graphData;}