Unable to Cast the List Values into Comparable Value for JFreeChart

这一生的挚爱 提交于 2019-12-11 06:27:25

问题


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.?


回答1:


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.



来源:https://stackoverflow.com/questions/18420501/unable-to-cast-the-list-values-into-comparable-value-for-jfreechart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!