无法将列表值转换为JFreeChart的可比较值 [英] Unable to Cast the List Values into Comparable Value for JFreeChart

查看:77
本文介绍了无法将列表值转换为JFreeChart的可比较值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级. ExistInsert.java和TryExist.java. ExistInsert的完整代码如下:

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);
            }
        }
    }

在这里,函数insert_data执行一个xquery并将结果返回到String列表中.函数draw_bar使用参数viz值和年份作为列表绘制条形图.我遇到的主要问题是将List转换为Comparable,这是dataset.addValue()的要求.在我的主程序TryExist.java中,我有:

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);        
    }         
    }

现在正在执行的查询将返回年份并分别表示为 [2060、2061、2062、2063、2064] [32905657、3091102752、4757563449、7954664475、11668355950] .然后如何在ExistInsert.java中编辑dataset.addValue(),以便可以将上面获得的值resp和年份传递到draw_bar中,以为传递的数据制作条形图??

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

推荐答案

使用.

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.

这篇关于无法将列表值转换为JFreeChart的可比较值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆