1

I have two classes viz. ExistInsert.java and TryExist.java . The complete code for ExistInsert is given below:

package tryexist;
import java.util.ArrayList;
import java.util.List;
import org.exist.xmldb.XQueryService;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;

public class ExistInsert {
    public  static String URI = "xmldb:exist://localhost:8899/exist/xmlrpc";
    public  static String driver = "org.exist.xmldb.DatabaseImpl";
    public static List mylist = new ArrayList();

    public  List insert_data(String xquery){
        try{

    Class c1 = Class.forName(driver);
    Database database=(Database) c1.newInstance();
    String collectionPath= "/db";
    DatabaseManager.registerDatabase(database);

    Collection col=DatabaseManager.getCollection(URI+collectionPath);
    XQueryService service = (XQueryService) col.getService("XQueryService","1.0");
    service.setProperty("indent", "yes");
    ResourceSet result = service.query(xquery); 
    ResourceIterator i = result.getIterator();    

    while(i.hasMoreResources()){
        Resource r =i.nextResource();
        mylist.add(((String)r.getContent()));        
    }
        }
        catch(Exception e){
            System.out.println(e);
        }
        return mylist;
        }

    public void draw_bar(List values, List years ){
        try{
            //DefaultPieDataset data = new DefaultPieDataset();
           DefaultCategoryDataset dataset = new DefaultCategoryDataset();

            for(int j=0;j<values.size();j++){
                dataset.addValue();
            }
            //JFreeChart chart = ChartFactory.createPieChart("TEST PEICHART", data, true, true, Locale.ENGLISH);

            JFreeChart chart2 = ChartFactory.createLineChart("Assets", "X","Y",dataset , PlotOrientation.VERTICAL, true, true, true);
            ChartFrame frame = new ChartFrame("TEST", chart2);
            frame.setVisible(true);
            frame.setSize(500, 500);

        }
            catch(Exception e){
                System.out.println(e);
            }
        }
    }

Here the function insert_data executes a xquery and return the result into list of String. The function draw_bar draws a barchart using the arguments viz values and years as list. The main problem I faced was converting the List into the Comparable, which is the requirement of dataset.addValue() . In my main program TryExist.java I have:

package tryexist;
import java.util.ArrayList;
import java.util.List;
public class Tryexist {      
    public static void main(String[] args) throws Exception{
        ExistInsert exist = new ExistInsert();
        String query = "Some query Here"
        List resp = exist.insert_data(query);
        List years = new ArrayList();
        for (int i=2060;i<=2064;i++){
            years.add(i);
        }
        System.out.println(years);
        System.out.println(resp);        
        exist.draw_bar(resp,years);        
    }         
    }

Now executing query returns years and resp as [2060, 2061, 2062, 2063, 2064] and [32905657, 3091102752, 4756935449, 7954664475, 11668355950] respectively. Then How do I edit dataset.addValue() in ExistInsert.java so that I can pass above obtained values resp and years into draw_bar to make a bar diagram for the data passed.?

Mahadeva
  • 1,584
  • 4
  • 23
  • 56
  • 1
    The two addValue methods [documented here](http://www.jfree.org/jfreechart/api/gjdoc/org/jfree/data/category/DefaultCategoryDataset.html) don't match the signature you specified. What type of value does your List contain. Why don't you use generic, type-safe collections (`List` instead of `List`), and why don't you respect the Java naming conventions? Why do you *want* to pass as second and third argument? i.e. what are thr rows and columns of the table? – JB Nizet Aug 24 '13 at 16:20

1 Answers1

2

A complete example using DefaultCategoryDataset, BarChartDemo1, is included in the distribution and illustrated below. Click on the class name to see the source code. The example uses instances of String as column and row keys, but any Comparable can by used, as discussed here.

image

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Yes I had read the program and the example shows clearly that Strings are passed as the Comparable in argument of addvalue(), but when I try to parse my Year values as String, it generated error, ie i wanted to pass dataset.addValue(values[j],"Years",years[j] in String), but throws error like integer cannot be converted into string. What am I doing Wrong here? – Mahadeva Aug 26 '13 at 03:10
  • Please edit your question to include an [sscce](http://sscce.org/) that shows the problem and error you describe. – trashgod Aug 26 '13 at 10:06
  • I edited the code and made more clear. Please check and suggest me. – Mahadeva Aug 26 '13 at 20:11
  • Your example is incomplete; please read [linked article](http://sscce.org/). Also consider using `List` and `Double.valueOf(). – trashgod Aug 26 '13 at 20:38
  • I tried using Double but failed. Error is same saying can't convert string to number or double. I have updated question including the classes i made. What am i doing wrong here? – Mahadeva Aug 27 '13 at 05:40