如何用java生成图表

如何用java生成图表,java,apache-poi,jxl,Java,Apache Poi,Jxl,我正在编写一个java代码,从网站中提取一些价格和日期,并将它们保存到excel文件中。这是我的代码: import java.io.File; import java.io.IOException; import java.util.Map; import java.util.TreeMap; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.J

我正在编写一个java代码,从网站中提取一些价格和日期,并将它们保存到excel文件中。这是我的代码:

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import org.jsoup.Jsoup;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class MoisAirfrancee2 {

    private static class DateObject{

        private Double taxes;
        private Double price;
        private Double htPrice;

        public DateObject(Double price, Double htPrice, Double taxes){
            this.taxes = taxes;
            this.price = price;
            this.htPrice = htPrice;
        }

        public Double getTaxes() {
            return taxes;
        }

        public Double getPrice() {
            return price;
        }

        public Double getHtPrice() {
            return htPrice;
        }
    }

    public static void main(String[] args) {

        Map<String, DateObject> prices = new TreeMap<String, DateObject>();
        File f = new File(System.getProperty("user.home") + "\\Desktop\\Test.xls");

        WritableWorkbook myexcel = null;

        try {

            myexcel = Workbook.createWorkbook(f);
            WritableSheet mysheet = myexcel.createSheet("AirFrance Novembre ", 0);

            Response response = Jsoup
                    .connect("http://www.airfrance.fr/vols/paris+tunis")
                    .userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36")
                    .method(Method.GET)
                    .timeout(2000)
                    .execute();

            Document doc = Jsoup
                    .connect("http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1")
                    .cookies(response.cookies())
                    .timeout(2000)
                    .userAgent("Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36")
                    .referrer("http://www.airfrance.fr/vols/paris+tunis").get();

            JSONObject obj = (JSONObject) new JSONParser().parse(doc.text());

            JSONArray dates = (JSONArray) obj.get("days");

            JSONObject dateObject;

            for(Object o : dates){
                if ( o instanceof JSONObject ) {
                    dateObject = ((JSONObject)o);
                    prices.put(dateObject.get("dallasDate").toString(), new DateObject((Double)dateObject.get("price"), (Double)dateObject.get("HTprice"), (Double)dateObject.get("taxes")));
                }
            }

            addLabel(mysheet, 0, 0, "Date");
            addLabel(mysheet, 1, 0, "Prix [€]");
            addLabel(mysheet, 2, 0, "PrixHt [€]");
            addLabel(mysheet, 3, 0, "Taxes [€]");

            int rowIndex = 1;
            DateObject date;

            for (String key : prices.keySet()) {
                date = prices.get(key);
                addLabel(mysheet, 0, rowIndex, key);
                addLabel(mysheet, 1, rowIndex, ""+date.getPrice());
                addLabel(mysheet, 2, rowIndex, ""+date.getHtPrice());
                addLabel(mysheet, 3, rowIndex, ""+date.getTaxes());
                rowIndex++;
            }

        myexcel.write();

        System.out.println("Scraping finished without errors.");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        } finally {
            try {
                myexcel.close();
            } catch (WriteException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void addLabel(WritableSheet sheet, int column, int row, String s)
            throws WriteException, RowsExceededException {
        Label label;
        label = new Label(column, row, s);
        sheet.addCell(label);
    }
}'
导入java.io.File;
导入java.io.IOException;
导入java.util.Map;
导入java.util.TreeMap;
导入org.json.simple.JSONArray;
导入org.json.simple.JSONObject;
导入org.json.simple.parser.JSONParser;
导入org.json.simple.parser.ParseException;
导入org.jsoup.jsoup;
导入org.jsoup.Connection.Method;
导入org.jsoup.Connection.Response;
导入org.jsoup.nodes.Document;
导入jxl.工作簿;
导入jxl.write.Label;
导入jxl.write.WritableSheet;
导入jxl.write.WritableWorkbook;
导入jxl.write.WriteException;
导入jxl.write.biff.rowseceedexception;
公共类MOISAIRFANCEEE2{
私有静态类DateObject{
私人双重征税;
私人双价;
私人双重价格;
公共日期对象(双倍价格、双倍价格、双倍税收){
这个。税=税;
这个价格=价格;
this.htPrice=htPrice;
}
公共双轴{
退税;
}
公开双价{
退货价格;
}
公共双getHtPrice(){
退货价格;
}
}
公共静态void main(字符串[]args){
地图价格=新树地图();
文件f=新文件(System.getProperty(“user.home”)+“\\Desktop\\Test.xls”);
可写工作簿myexcel=null;
试一试{
myexcel=Workbook.createWorkbook(f);
WritableSheet mysheet=myexcel.createSheet(“AirFrance Novenbre”,0);
Response=Jsoup
.连接(“http://www.airfrance.fr/vols/paris+突尼斯)
.userAgent(“Mozilla/5.0(Windows NT 6.3;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/52.0.2743.82 Safari/537.36”)
.method(method.GET)
.超时(2000)
.execute();
文档doc=Jsoup
.连接(“http://www.airfrance.fr/FR/fr/local/vols/getInstantFlexNewCalendar.do?idMonth=10&itineraryNumber=1")
.cookies(response.cookies())
.超时(2000)
.userAgent(“Mozilla/5.0(Windows NT 6.3;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/52.0.2743.82 Safari/537.36”)
.推荐人(”http://www.airfrance.fr/vols/paris+突尼斯);
JSONObject obj=(JSONObject)新建JSONParser().parse(doc.text());
JSONArray日期=(JSONArray)obj.get(“天”);
JSONObject日期对象;
用于(对象o:日期){
if(o JSONObject的实例){
dateObject=((JSONObject)o);
prices.put(dateObject.get(“dallasDate”).toString(),new dateObject((Double)dateObject.get(“price”),(Double)dateObject.get(“HTprice”),(Double)dateObject.get(“taxes”);
}
}
addLabel(mysheet,0,0,“日期”);
addLabel(mysheet,1,0,“Prix[€]”;
addLabel(mysheet,2,0,“PrixHt[€]”;
addLabel(mysheet,3,0,“税费[€]”;
int rowIndex=1;
日期对象日期;
for(字符串键:prices.keySet()){
日期=价格。获取(键);
addLabel(mysheet,0,行索引,键);
addLabel(mysheet,1,rowIndex,“+date.getPrice());
addLabel(mysheet,2,rowIndex,“+date.getHtPrice());
addLabel(mysheet,3,rowIndex,“+date.getTaxes());
rowIndex++;
}
myexcel.write();
System.out.println(“刮平完成,无错误”);
}捕获(IOE异常){
e、 printStackTrace();
}捕获(解析异常){
e、 printStackTrace();
}捕获(写入异常e){
e、 printStackTrace();
}最后{
试一试{
myexcel.close();
}捕获(写入异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
私有静态void addLabel(可写工作表、int列、int行、字符串s)
抛出WriteException,RowsExcepedException{
标签;
标签=新标签(列、行、s);
表。添加单元格(标签);
}
}'

提取这些数据后,我想生成一个图形图表,图表应该是(价格、日期)。

您想在哪里生成图表?在桌面应用程序中?在网站上?你看过/尝试过哪些图书馆?发生了什么?我想在保存数据的同一excel文件中生成它。我尝试了POI Apache,但遇到了一些错误。什么错误?你试了什么?显示您的代码。你知道ApachePOI是否支持生成图表吗?我从它支持的POI3.8版本就读到了。很遗憾,我这里没有代码。你能描述一下工作流程吗?Excel文件是否生成一次,只是偶尔更新?最好手动创建excel文件,将数据写入csv文件并将csv文件导入excel。然后,当csv文件更改时,您可以在文件中创建图表并更新数据。您希望在哪里生成图表?在桌面应用程序中?在网站上?你看过/尝试过哪些图书馆?发生了什么?我想在保存数据的同一excel文件中生成它。我尝试了POI Apache,但遇到了一些错误。什么错误?你试了什么?显示您的代码。你知道ApachePOI是否支持生成图表吗?我从它支持的POI3.8版本就读到了。很遗憾,我这里没有代码。你能描述一下工作流程吗?Excel文件是否生成一次,只是偶尔更新?最好手动创建excel文件,将数据写入csv文件并导入csv文件